Skip to content

Commit a5188f5

Browse files
authored
Add checks to avoid wasm_runtime_malloc memory with size 0 (#507)
In some platforms, allocating memory with size 0 may return NULL but not an empty memory block, which causes runtime load, instantiate or execute wasm/aot file failed. We add checks to try to avoid allocating memory in runtime if the size is 0. And in wasm_runtime_malloc/free, output warning if allocate memory with size 0 and free memory with NULL ptr. Also fix some coding style issues, fix handle riscv32 ilp32d issue, and fix several wasm-c-api issues. Signed-off-by: Wenyong Huang <[email protected]>
1 parent efd6489 commit a5188f5

File tree

20 files changed

+240
-131
lines changed

20 files changed

+240
-131
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,9 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
11171117
uint64 size, text_offset;
11181118

11191119
size = sizeof(void*) * (uint64)module->func_count;
1120-
if (!(module->func_ptrs = loader_malloc
1121-
(size, error_buf, error_buf_size))) {
1120+
if (size > 0
1121+
&& !(module->func_ptrs = loader_malloc
1122+
(size, error_buf, error_buf_size))) {
11221123
return false;
11231124
}
11241125

@@ -1158,8 +1159,9 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
11581159
}
11591160

11601161
size = sizeof(uint32) * (uint64)module->func_count;
1161-
if (!(module->func_type_indexes = loader_malloc
1162-
(size, error_buf, error_buf_size))) {
1162+
if (size > 0
1163+
&& !(module->func_type_indexes = loader_malloc
1164+
(size, error_buf, error_buf_size))) {
11631165
return false;
11641166
}
11651167

@@ -1498,7 +1500,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
14981500

14991501
/* Allocate memory for relocation groups */
15001502
size = sizeof(AOTRelocationGroup) * (uint64)group_count;
1501-
if (!(groups = loader_malloc(size, error_buf, error_buf_size))) {
1503+
if (size > 0
1504+
&& !(groups = loader_malloc(size, error_buf, error_buf_size))) {
15021505
goto fail;
15031506
}
15041507

@@ -2065,8 +2068,9 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
20652068

20662069
/* Allocate memory for function pointers */
20672070
size = (uint64)module->func_count * sizeof(void *);
2068-
if (!(module->func_ptrs =
2069-
loader_malloc(size, error_buf, error_buf_size))) {
2071+
if (size > 0
2072+
&& !(module->func_ptrs =
2073+
loader_malloc(size, error_buf, error_buf_size))) {
20702074
goto fail2;
20712075
}
20722076

@@ -2085,8 +2089,9 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
20852089

20862090
/* Allocation memory for function type indexes */
20872091
size = (uint64)module->func_count * sizeof(uint32);
2088-
if (!(module->func_type_indexes =
2089-
loader_malloc(size, error_buf, error_buf_size))) {
2092+
if (size > 0
2093+
&& !(module->func_type_indexes =
2094+
loader_malloc(size, error_buf, error_buf_size))) {
20902095
goto fail3;
20912096
}
20922097
for (i = 0; i < comp_data->func_count; i++)
@@ -2135,7 +2140,8 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
21352140
return module;
21362141

21372142
fail3:
2138-
wasm_runtime_free(module->func_ptrs);
2143+
if (module->func_ptrs)
2144+
wasm_runtime_free(module->func_ptrs);
21392145
fail2:
21402146
if (module->memory_count > 0)
21412147
wasm_runtime_free(module->memories);

core/iwasm/aot/aot_runtime.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ memories_deinstantiate(AOTModuleInstance *module_inst)
176176
wasm_runtime_free(memory_inst->heap_handle.ptr);
177177
}
178178

179-
if (memory_inst->heap_data.ptr) {
179+
if (memory_inst->memory_data.ptr) {
180180
#ifndef OS_ENABLE_HW_BOUND_CHECK
181181
wasm_runtime_free(memory_inst->memory_data.ptr);
182182
#else
@@ -202,7 +202,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
202202
uint32 bytes_of_last_page, bytes_to_page_end;
203203
uint32 heap_offset = num_bytes_per_page *init_page_count;
204204
uint64 total_size;
205-
uint8 *p, *global_addr;
205+
uint8 *p = NULL, *global_addr;
206206
#ifdef OS_ENABLE_HW_BOUND_CHECK
207207
uint8 *mapped_mem;
208208
uint64 map_size = 8 * (uint64)BH_GB;
@@ -321,7 +321,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
321321

322322
#ifndef OS_ENABLE_HW_BOUND_CHECK
323323
/* Allocate memory */
324-
if (!(p = runtime_malloc(total_size, error_buf, error_buf_size))) {
324+
if (total_size > 0
325+
&& !(p = runtime_malloc(total_size, error_buf, error_buf_size))) {
325326
return NULL;
326327
}
327328
#else
@@ -420,7 +421,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
420421
wasm_runtime_free(memory_inst->heap_handle.ptr);
421422
fail1:
422423
#ifndef OS_ENABLE_HW_BOUND_CHECK
423-
wasm_runtime_free(memory_inst->memory_data.ptr);
424+
if (memory_inst->memory_data.ptr)
425+
wasm_runtime_free(memory_inst->memory_data.ptr);
424426
#else
425427
os_munmap(mapped_mem, map_size);
426428
#endif
@@ -504,7 +506,8 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
504506
}
505507

506508
/* Copy memory data */
507-
bh_assert(memory_inst->memory_data.ptr);
509+
bh_assert(memory_inst->memory_data.ptr
510+
|| memory_inst->memory_data_size == 0);
508511

509512
/* Check memory data */
510513
/* check offset since length might negative */
@@ -526,9 +529,11 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
526529
return false;
527530
}
528531

529-
bh_memcpy_s((uint8*)memory_inst->memory_data.ptr + base_offset,
530-
memory_inst->memory_data_size - base_offset,
531-
data_seg->bytes, length);
532+
if (memory_inst->memory_data.ptr) {
533+
bh_memcpy_s((uint8*)memory_inst->memory_data.ptr + base_offset,
534+
memory_inst->memory_data_size - base_offset,
535+
data_seg->bytes, length);
536+
}
532537
}
533538

534539
return true;
@@ -543,6 +548,9 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module,
543548
uint64 total_size =
544549
((uint64)module->import_func_count + module->func_count) * sizeof(void*);
545550

551+
if (module->import_func_count + module->func_count == 0)
552+
return true;
553+
546554
/* Allocate memory */
547555
if (!(module_inst->func_ptrs.ptr = runtime_malloc
548556
(total_size, error_buf, error_buf_size))) {
@@ -562,7 +570,8 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module,
562570
}
563571

564572
/* Set defined function pointers */
565-
memcpy(func_ptrs, module->func_ptrs, module->func_count * sizeof(void*));
573+
bh_memcpy_s(func_ptrs, sizeof(void*) * module->func_count,
574+
module->func_ptrs, sizeof(void*) * module->func_count);
566575
return true;
567576
}
568577

@@ -575,6 +584,9 @@ init_func_type_indexes(AOTModuleInstance *module_inst, AOTModule *module,
575584
uint64 total_size =
576585
((uint64)module->import_func_count + module->func_count) * sizeof(uint32);
577586

587+
if (module->import_func_count + module->func_count == 0)
588+
return true;
589+
578590
/* Allocate memory */
579591
if (!(module_inst->func_type_indexes.ptr =
580592
runtime_malloc(total_size, error_buf, error_buf_size))) {
@@ -586,9 +598,8 @@ init_func_type_indexes(AOTModuleInstance *module_inst, AOTModule *module,
586598
for (i = 0; i < module->import_func_count; i++, func_type_index++)
587599
*func_type_index = module->import_funcs[i].func_type_index;
588600

589-
memcpy(func_type_index, module->func_type_indexes,
590-
module->func_count * sizeof(uint32));
591-
601+
bh_memcpy_s(func_type_index, sizeof(uint32) * module->func_count,
602+
module->func_type_indexes, sizeof(uint32) * module->func_count);
592603
return true;
593604
}
594605

@@ -1688,9 +1699,11 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
16881699
if (!(memory_data = wasm_runtime_malloc((uint32)total_size))) {
16891700
return false;
16901701
}
1691-
bh_memcpy_s(memory_data, (uint32)total_size,
1692-
memory_data_old, total_size_old);
1693-
wasm_runtime_free(memory_data_old);
1702+
if (memory_data_old) {
1703+
bh_memcpy_s(memory_data, (uint32)total_size,
1704+
memory_data_old, total_size_old);
1705+
wasm_runtime_free(memory_data_old);
1706+
}
16941707
}
16951708

16961709
memset(memory_data + total_size_old,

core/iwasm/common/wasm_c_api.c

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void
2323
wasm_instance_delete_internal(wasm_instance_t *);
2424

2525
static void *
26-
malloc_internal(size_t size)
26+
malloc_internal(uint64 size)
2727
{
2828
void *mem = NULL;
2929

@@ -47,8 +47,7 @@ malloc_internal(size_t size)
4747
/* Vectors */
4848
#define INIT_VEC(vector_p, func_prefix, size) \
4949
do { \
50-
vector_p = malloc_internal(sizeof(*(vector_p))); \
51-
if (!vector_p) { \
50+
if (!(vector_p = malloc_internal(sizeof(*(vector_p))))) { \
5251
goto failed; \
5352
} \
5453
func_prefix##_new_uninitialized(vector_p, size); \
@@ -75,7 +74,8 @@ malloc_internal(size_t size)
7574
static inline void
7675
generic_vec_init_data(Vector *out, size_t num_of_elems, size_t size_of_elem)
7776
{
78-
if (!bh_vector_init(out, num_of_elems, size_of_elem)) {
77+
/* size 0 is meaningless for a elemment */
78+
if (!size_of_elem || !bh_vector_init(out, num_of_elems, size_of_elem)) {
7979
out->data = NULL;
8080
out->max_elems = 0;
8181
out->num_elems = 0;
@@ -99,7 +99,7 @@ wasm_byte_vec_copy(wasm_byte_vec_t *out, const wasm_byte_vec_t *src)
9999

100100
bh_assert(out && src);
101101

102-
generic_vec_init_data((Vector *)out, src->size, src->size_of_elem);
102+
generic_vec_init_data((Vector *)out, src->size, sizeof(wasm_byte_t));
103103
if (!out->data) {
104104
goto failed;
105105
}
@@ -187,7 +187,7 @@ wasm_engine_new_internal(mem_alloc_type_t type,
187187
goto failed;
188188
}
189189

190-
#if BH_DEBUG == 1
190+
#if BH_DEBUG != 0
191191
bh_log_set_verbose_level(5);
192192
#else
193193
bh_log_set_verbose_level(3);
@@ -459,7 +459,7 @@ wasm_valtype_vec_copy(wasm_valtype_vec_t *out, const wasm_valtype_vec_t *src)
459459

460460
bh_assert(out && src);
461461

462-
generic_vec_init_data((Vector *)out, src->size, src->size_of_elem);
462+
generic_vec_init_data((Vector *)out, src->size, sizeof(wasm_valtype_t *));
463463
if (!out->data) {
464464
goto failed;
465465
}
@@ -1116,33 +1116,41 @@ results_to_argv(const wasm_val_t *results,
11161116
static void
11171117
native_func_trampoline(wasm_exec_env_t exec_env, uint64 *argv)
11181118
{
1119-
wasm_val_t *params = NULL;
1120-
wasm_val_t *results = NULL;
1119+
wasm_val_t *params = NULL, *results = NULL;
11211120
uint32 argc = 0;
11221121
const wasm_func_t *func = NULL;
11231122
wasm_trap_t *trap = NULL;
1124-
1125-
bh_assert(argv);
1123+
size_t param_count, result_count;
11261124

11271125
func = wasm_runtime_get_function_attachment(exec_env);
11281126
bh_assert(func);
11291127

1130-
params = malloc_internal(wasm_func_param_arity(func) * sizeof(wasm_val_t));
1131-
if (!params) {
1132-
goto failed;
1133-
}
1128+
param_count = wasm_func_param_arity(func);
1129+
if (param_count) {
1130+
if (!argv) {
1131+
goto failed;
1132+
}
11341133

1135-
results =
1136-
malloc_internal(wasm_func_result_arity(func) * sizeof(wasm_val_t));
1137-
if (!results) {
1138-
goto failed;
1134+
if (!(params = malloc_internal(param_count * sizeof(wasm_val_t)))) {
1135+
goto failed;
1136+
}
1137+
1138+
/* argv -> const wasm_val_t params[] */
1139+
if (!(argc = argv_to_params(
1140+
argv, wasm_functype_params(wasm_func_type(func)), params))) {
1141+
goto failed;
1142+
}
11391143
}
11401144

1141-
/* argv -> const wasm_val_t params[] */
1142-
argc =
1143-
argv_to_params(argv, wasm_functype_params(wasm_func_type(func)), params);
1144-
if (wasm_func_param_arity(func) && !argc) {
1145-
goto failed;
1145+
result_count = wasm_func_result_arity(func);
1146+
if (result_count) {
1147+
if (!argv) {
1148+
goto failed;
1149+
}
1150+
1151+
if (!(results = malloc_internal(result_count * sizeof(wasm_val_t)))) {
1152+
goto failed;
1153+
}
11461154
}
11471155

11481156
if (func->with_env) {
@@ -1164,16 +1172,17 @@ native_func_trampoline(wasm_exec_env_t exec_env, uint64 *argv)
11641172
}
11651173
}
11661174

1167-
/* there is no result or there is an exception */
1168-
if (trap || !wasm_func_result_arity(func)) {
1175+
if (argv) {
11691176
memset(argv, 0, wasm_func_param_arity(func) * sizeof(uint64));
11701177
}
11711178

1172-
/* wasm_val_t results[] -> argv */
1173-
argc = results_to_argv(results,
1174-
wasm_functype_results(wasm_func_type(func)), argv);
1175-
if (wasm_func_result_arity(func) && !argc) {
1176-
goto failed;
1179+
/* there is no trap and there is return values */
1180+
if (!trap && result_count) {
1181+
/* wasm_val_t results[] -> argv */
1182+
if (!(argc = results_to_argv(
1183+
results, wasm_functype_results(wasm_func_type(func)), argv))) {
1184+
goto failed;
1185+
}
11771186
}
11781187

11791188
failed:
@@ -1503,8 +1512,7 @@ wasm_func_call(const wasm_func_t *func,
15031512
/* a parameter list and a return value list */
15041513
uint32 *argv = NULL;
15051514
WASMFunctionInstanceCommon *func_comm_rt = NULL;
1506-
size_t param_count = 0;
1507-
size_t result_count = 0;
1515+
size_t param_count, result_count, alloc_count;
15081516

15091517
bh_assert(func && func->func_type && func->inst_comm_rt);
15101518

@@ -1527,17 +1535,18 @@ wasm_func_call(const wasm_func_t *func,
15271535

15281536
param_count = wasm_func_param_arity(func);
15291537
result_count = wasm_func_result_arity(func);
1530-
argv = malloc_internal(
1531-
sizeof(uint64)
1532-
* (param_count > result_count ? param_count : result_count));
1533-
if (!argv) {
1534-
goto failed;
1538+
alloc_count = (param_count > result_count) ? param_count : result_count;
1539+
if (alloc_count) {
1540+
if (!(argv = malloc_internal(sizeof(uint64) * alloc_count))) {
1541+
goto failed;
1542+
}
15351543
}
15361544

15371545
/* copy parametes */
1538-
argc = params_to_argv(params, wasm_functype_params(wasm_func_type(func)),
1539-
wasm_func_param_arity(func), argv);
1540-
if (wasm_func_param_arity(func) && !argc) {
1546+
if (param_count
1547+
&& !(argc = params_to_argv(params,
1548+
wasm_functype_params(wasm_func_type(func)),
1549+
wasm_func_param_arity(func), argv))) {
15411550
goto failed;
15421551
}
15431552

@@ -1548,9 +1557,10 @@ wasm_func_call(const wasm_func_t *func,
15481557
}
15491558

15501559
/* copy results */
1551-
argc = argv_to_results(argv, wasm_functype_results(wasm_func_type(func)),
1552-
wasm_func_result_arity(func), results);
1553-
if (wasm_func_result_arity(func) && !argc) {
1560+
if (result_count
1561+
&& !(argc = argv_to_results(
1562+
argv, wasm_functype_results(wasm_func_type(func)),
1563+
wasm_func_result_arity(func), results))) {
15541564
goto failed;
15551565
}
15561566

@@ -2734,7 +2744,7 @@ wasm_extern_vec_copy(wasm_extern_vec_t *out, const wasm_extern_vec_t *src)
27342744
size_t i = 0;
27352745
bh_assert(out && src);
27362746

2737-
generic_vec_init_data((Vector *)out, src->size, src->size_of_elem);
2747+
generic_vec_init_data((Vector *)out, src->size, sizeof(wasm_extern_t *));
27382748
if (!out->data) {
27392749
goto failed;
27402750
}

0 commit comments

Comments
 (0)