Skip to content

Commit 09eb858

Browse files
add realloc wrapper, fix pthread_join overwrite issue (#605)
1 parent dfe52ab commit 09eb858

File tree

9 files changed

+113
-2
lines changed

9 files changed

+113
-2
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,43 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
15531553
return (uint32)(addr - (uint8*)memory_inst->memory_data.ptr);
15541554
}
15551555

1556+
uint32
1557+
aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr,
1558+
uint32 size, void **p_native_addr)
1559+
{
1560+
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
1561+
uint8 *addr = NULL;
1562+
1563+
if (!memory_inst) {
1564+
aot_set_exception(module_inst, "uninitialized memory");
1565+
return 0;
1566+
}
1567+
1568+
if (memory_inst->heap_handle.ptr) {
1569+
addr =
1570+
mem_allocator_realloc(memory_inst->heap_handle.ptr,
1571+
(uint8*)memory_inst->memory_data.ptr + ptr,
1572+
size);
1573+
}
1574+
1575+
/* Only support realloc in WAMR's app heap */
1576+
1577+
if (!addr) {
1578+
if (memory_inst->heap_handle.ptr
1579+
&& mem_allocator_is_heap_corrupted(memory_inst->heap_handle.ptr)) {
1580+
aot_set_exception(module_inst, "app heap corrupted");
1581+
}
1582+
else {
1583+
aot_set_exception(module_inst, "out of memory");
1584+
}
1585+
return 0;
1586+
}
1587+
1588+
if (p_native_addr)
1589+
*p_native_addr = addr;
1590+
return (uint32)(addr - (uint8*)memory_inst->memory_data.ptr);
1591+
}
1592+
15561593
void
15571594
aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
15581595
{

core/iwasm/aot/aot_runtime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ uint32
498498
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
499499
void **p_native_addr);
500500

501+
uint32
502+
aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr,
503+
uint32 size, void **p_native_addr);
504+
501505
void
502506
aot_module_free(AOTModuleInstance *module_inst, uint32 ptr);
503507

core/iwasm/common/wasm_runtime_common.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,23 @@ wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
13781378
return 0;
13791379
}
13801380

1381+
uint32
1382+
wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
1383+
uint32 size, void **p_native_addr)
1384+
{
1385+
#if WASM_ENABLE_INTERP != 0
1386+
if (module_inst->module_type == Wasm_Module_Bytecode)
1387+
return wasm_module_realloc((WASMModuleInstance*)module_inst, ptr,
1388+
size, p_native_addr);
1389+
#endif
1390+
#if WASM_ENABLE_AOT != 0
1391+
if (module_inst->module_type == Wasm_Module_AoT)
1392+
return aot_module_realloc((AOTModuleInstance*)module_inst, ptr,
1393+
size, p_native_addr);
1394+
#endif
1395+
return 0;
1396+
}
1397+
13811398
void
13821399
wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
13831400
{

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,41 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
17411741
return (uint32)(addr - memory->memory_data);
17421742
}
17431743

1744+
uint32
1745+
wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
1746+
void **p_native_addr)
1747+
{
1748+
WASMMemoryInstance *memory = module_inst->default_memory;
1749+
uint8 *addr = NULL;
1750+
1751+
if (!memory) {
1752+
wasm_set_exception(module_inst, "uninitialized memory");
1753+
return 0;
1754+
}
1755+
1756+
if (memory->heap_handle) {
1757+
addr = mem_allocator_realloc(memory->heap_handle,
1758+
memory->memory_data + ptr, size);
1759+
}
1760+
1761+
/* Only support realloc in WAMR's app heap */
1762+
1763+
if (!addr) {
1764+
if (memory->heap_handle
1765+
&& mem_allocator_is_heap_corrupted(memory->heap_handle)) {
1766+
wasm_set_exception(module_inst, "app heap corrupted");
1767+
}
1768+
else {
1769+
wasm_set_exception(module_inst, "out of memory");
1770+
}
1771+
return 0;
1772+
}
1773+
if (p_native_addr)
1774+
*p_native_addr = addr;
1775+
1776+
return (uint32)(addr - memory->memory_data);
1777+
}
1778+
17441779
void
17451780
wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
17461781
{

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ uint32
328328
wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
329329
void **p_native_addr);
330330

331+
uint32
332+
wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
333+
void **p_native_addr);
334+
331335
void
332336
wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr);
333337

core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ pthread_join_wrapper(wasm_exec_env_t exec_env, uint32 thread,
648648
}
649649

650650
if (retval_offset != 0)
651-
*retval = (void*)ret;
651+
*(uint32*)retval = (uint32)(uintptr_t)ret;
652652

653653
return join_ret;
654654
}

core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ wasm_runtime_get_llvm_stack(wasm_module_inst_t module);
2828
void
2929
wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
3030

31+
uint32
32+
wasm_runtime_module_realloc(wasm_module_inst_t module, uint32 ptr,
33+
uint32 size, void **p_native_addr);
34+
3135
#define get_module_inst(exec_env) \
3236
wasm_runtime_get_module_inst(exec_env)
3337

@@ -704,6 +708,14 @@ calloc_wrapper(wasm_exec_env_t exec_env, uint32 nmemb, uint32 size)
704708
return ret_offset;
705709
}
706710

711+
static uint32
712+
realloc_wrapper(wasm_exec_env_t exec_env, uint32 ptr, uint32 new_size)
713+
{
714+
wasm_module_inst_t module_inst = get_module_inst(exec_env);
715+
716+
return wasm_runtime_module_realloc(module_inst, ptr, new_size, NULL);
717+
}
718+
707719
static void
708720
free_wrapper(wasm_exec_env_t exec_env, void *ptr)
709721
{
@@ -1092,6 +1104,7 @@ static NativeSymbol native_symbols_libc_builtin[] = {
10921104
REG_NATIVE_FUNC(strncmp, "(**~)i"),
10931105
REG_NATIVE_FUNC(strncpy, "(**~)i"),
10941106
REG_NATIVE_FUNC(malloc, "(i)i"),
1107+
REG_NATIVE_FUNC(realloc, "(ii)i"),
10951108
REG_NATIVE_FUNC(calloc, "(ii)i"),
10961109
REG_NATIVE_FUNC(strdup, "($)i"),
10971110
/* clang may introduce __strdup */

doc/pthread_library.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ To build this C program into WebAssembly app with libc-builtin, you can use this
5757

5858
You can also build this program with WASI, but we need to make some changes to wasi-sysroot:
5959

60-
1. disable malloc/free of wasi if the wasi-sdk version is smaller than wasi-sdk-12.0 (not include 12.0), as they don't support shared memory:
60+
1. disable malloc/free of wasi, as they are not atomic operations:
6161
``` bash
6262
/opt/wasi-sdk/bin/llvm-ar -d /opt/wasi-sdk/share/wasi-sysroot/lib/wasm32-wasi/libc.a dlmalloc.o
6363
```

wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ strncmp
4040
strncpy
4141
malloc
4242
calloc
43+
realloc
4344
strdup
4445
free
4546
atoi

0 commit comments

Comments
 (0)