mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2024-11-26 07:21:54 +00:00
add realloc wrapper, fix pthread_join overwrite issue (#605)
This commit is contained in:
parent
dfe52ab42f
commit
09eb858a02
|
@ -1553,6 +1553,43 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
|
|||
return (uint32)(addr - (uint8*)memory_inst->memory_data.ptr);
|
||||
}
|
||||
|
||||
uint32
|
||||
aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr,
|
||||
uint32 size, void **p_native_addr)
|
||||
{
|
||||
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
|
||||
uint8 *addr = NULL;
|
||||
|
||||
if (!memory_inst) {
|
||||
aot_set_exception(module_inst, "uninitialized memory");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (memory_inst->heap_handle.ptr) {
|
||||
addr =
|
||||
mem_allocator_realloc(memory_inst->heap_handle.ptr,
|
||||
(uint8*)memory_inst->memory_data.ptr + ptr,
|
||||
size);
|
||||
}
|
||||
|
||||
/* Only support realloc in WAMR's app heap */
|
||||
|
||||
if (!addr) {
|
||||
if (memory_inst->heap_handle.ptr
|
||||
&& mem_allocator_is_heap_corrupted(memory_inst->heap_handle.ptr)) {
|
||||
aot_set_exception(module_inst, "app heap corrupted");
|
||||
}
|
||||
else {
|
||||
aot_set_exception(module_inst, "out of memory");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (p_native_addr)
|
||||
*p_native_addr = addr;
|
||||
return (uint32)(addr - (uint8*)memory_inst->memory_data.ptr);
|
||||
}
|
||||
|
||||
void
|
||||
aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
|
||||
{
|
||||
|
|
|
@ -498,6 +498,10 @@ uint32
|
|||
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
|
||||
void **p_native_addr);
|
||||
|
||||
uint32
|
||||
aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr,
|
||||
uint32 size, void **p_native_addr);
|
||||
|
||||
void
|
||||
aot_module_free(AOTModuleInstance *module_inst, uint32 ptr);
|
||||
|
||||
|
|
|
@ -1378,6 +1378,23 @@ wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
|
|||
return 0;
|
||||
}
|
||||
|
||||
uint32
|
||||
wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
|
||||
uint32 size, void **p_native_addr)
|
||||
{
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_inst->module_type == Wasm_Module_Bytecode)
|
||||
return wasm_module_realloc((WASMModuleInstance*)module_inst, ptr,
|
||||
size, p_native_addr);
|
||||
#endif
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_inst->module_type == Wasm_Module_AoT)
|
||||
return aot_module_realloc((AOTModuleInstance*)module_inst, ptr,
|
||||
size, p_native_addr);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
|
||||
{
|
||||
|
|
|
@ -1741,6 +1741,41 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
|
|||
return (uint32)(addr - memory->memory_data);
|
||||
}
|
||||
|
||||
uint32
|
||||
wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
|
||||
void **p_native_addr)
|
||||
{
|
||||
WASMMemoryInstance *memory = module_inst->default_memory;
|
||||
uint8 *addr = NULL;
|
||||
|
||||
if (!memory) {
|
||||
wasm_set_exception(module_inst, "uninitialized memory");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (memory->heap_handle) {
|
||||
addr = mem_allocator_realloc(memory->heap_handle,
|
||||
memory->memory_data + ptr, size);
|
||||
}
|
||||
|
||||
/* Only support realloc in WAMR's app heap */
|
||||
|
||||
if (!addr) {
|
||||
if (memory->heap_handle
|
||||
&& mem_allocator_is_heap_corrupted(memory->heap_handle)) {
|
||||
wasm_set_exception(module_inst, "app heap corrupted");
|
||||
}
|
||||
else {
|
||||
wasm_set_exception(module_inst, "out of memory");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (p_native_addr)
|
||||
*p_native_addr = addr;
|
||||
|
||||
return (uint32)(addr - memory->memory_data);
|
||||
}
|
||||
|
||||
void
|
||||
wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
|
||||
{
|
||||
|
|
|
@ -328,6 +328,10 @@ uint32
|
|||
wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
|
||||
void **p_native_addr);
|
||||
|
||||
uint32
|
||||
wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
|
||||
void **p_native_addr);
|
||||
|
||||
void
|
||||
wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr);
|
||||
|
||||
|
|
|
@ -648,7 +648,7 @@ pthread_join_wrapper(wasm_exec_env_t exec_env, uint32 thread,
|
|||
}
|
||||
|
||||
if (retval_offset != 0)
|
||||
*retval = (void*)ret;
|
||||
*(uint32*)retval = (uint32)(uintptr_t)ret;
|
||||
|
||||
return join_ret;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,10 @@ wasm_runtime_get_llvm_stack(wasm_module_inst_t module);
|
|||
void
|
||||
wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
|
||||
|
||||
uint32
|
||||
wasm_runtime_module_realloc(wasm_module_inst_t module, uint32 ptr,
|
||||
uint32 size, void **p_native_addr);
|
||||
|
||||
#define get_module_inst(exec_env) \
|
||||
wasm_runtime_get_module_inst(exec_env)
|
||||
|
||||
|
@ -704,6 +708,14 @@ calloc_wrapper(wasm_exec_env_t exec_env, uint32 nmemb, uint32 size)
|
|||
return ret_offset;
|
||||
}
|
||||
|
||||
static uint32
|
||||
realloc_wrapper(wasm_exec_env_t exec_env, uint32 ptr, uint32 new_size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||
|
||||
return wasm_runtime_module_realloc(module_inst, ptr, new_size, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
free_wrapper(wasm_exec_env_t exec_env, void *ptr)
|
||||
{
|
||||
|
@ -1092,6 +1104,7 @@ static NativeSymbol native_symbols_libc_builtin[] = {
|
|||
REG_NATIVE_FUNC(strncmp, "(**~)i"),
|
||||
REG_NATIVE_FUNC(strncpy, "(**~)i"),
|
||||
REG_NATIVE_FUNC(malloc, "(i)i"),
|
||||
REG_NATIVE_FUNC(realloc, "(ii)i"),
|
||||
REG_NATIVE_FUNC(calloc, "(ii)i"),
|
||||
REG_NATIVE_FUNC(strdup, "($)i"),
|
||||
/* clang may introduce __strdup */
|
||||
|
|
|
@ -57,7 +57,7 @@ To build this C program into WebAssembly app with libc-builtin, you can use this
|
|||
|
||||
You can also build this program with WASI, but we need to make some changes to wasi-sysroot:
|
||||
|
||||
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:
|
||||
1. disable malloc/free of wasi, as they are not atomic operations:
|
||||
``` bash
|
||||
/opt/wasi-sdk/bin/llvm-ar -d /opt/wasi-sdk/share/wasi-sysroot/lib/wasm32-wasi/libc.a dlmalloc.o
|
||||
```
|
||||
|
|
|
@ -40,6 +40,7 @@ strncmp
|
|||
strncpy
|
||||
malloc
|
||||
calloc
|
||||
realloc
|
||||
strdup
|
||||
free
|
||||
atoi
|
||||
|
|
Loading…
Reference in New Issue
Block a user