From 09eb858a0227715a63eb677050ee088849462d18 Mon Sep 17 00:00:00 2001 From: Xu Jun <693788454@qq.com> Date: Fri, 9 Apr 2021 15:27:12 +0800 Subject: [PATCH] add realloc wrapper, fix pthread_join overwrite issue (#605) --- core/iwasm/aot/aot_runtime.c | 37 +++++++++++++++++++ core/iwasm/aot/aot_runtime.h | 4 ++ core/iwasm/common/wasm_runtime_common.c | 17 +++++++++ core/iwasm/interpreter/wasm_runtime.c | 35 ++++++++++++++++++ core/iwasm/interpreter/wasm_runtime.h | 4 ++ .../lib-pthread/lib_pthread_wrapper.c | 2 +- .../libc-builtin/libc_builtin_wrapper.c | 13 +++++++ doc/pthread_library.md | 2 +- .../share/defined-symbols.txt | 1 + 9 files changed, 113 insertions(+), 2 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 50b22435..74a71032 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -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) { diff --git a/core/iwasm/aot/aot_runtime.h b/core/iwasm/aot/aot_runtime.h index 4c461d65..99052393 100644 --- a/core/iwasm/aot/aot_runtime.h +++ b/core/iwasm/aot/aot_runtime.h @@ -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); diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 97d93997..aad71e1a 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -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) { diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index d32ac995..360958cb 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -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) { diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index ccde47e4..3d7d47c6 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -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); diff --git a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c index 0930a978..0dd1faa9 100644 --- a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c +++ b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c @@ -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; } diff --git a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c index 195c55ad..b2701bcc 100644 --- a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c +++ b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c @@ -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 */ diff --git a/doc/pthread_library.md b/doc/pthread_library.md index 927300c5..882bb59d 100644 --- a/doc/pthread_library.md +++ b/doc/pthread_library.md @@ -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 ``` diff --git a/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt b/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt index 08a37846..332aedb7 100644 --- a/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt +++ b/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt @@ -40,6 +40,7 @@ strncmp strncpy malloc calloc +realloc strdup free atoi