Implement a first version of shared heap feature (#3789)

ps. https://github.com/bytecodealliance/wasm-micro-runtime/issues/3546
This commit is contained in:
WenLY1
2024-09-14 10:51:42 +08:00
committed by GitHub
parent 27b69176c1
commit 92852f3719
15 changed files with 826 additions and 22 deletions

View File

@ -0,0 +1,8 @@
# Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (LIB_SHARED_HEAP ${CMAKE_CURRENT_LIST_DIR})
add_definitions (-DWASM_ENABLE_SHARED_HEAP=1)
include_directories(${LIB_SHARED_HEAP_DIR})
file (GLOB source_all ${LIB_SHARED_HEAP}/*.c)
set (LIB_SHARED_HEAP_SOURCE ${source_all})

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_common.h"
#include "bh_log.h"
#include "wasm_export.h"
#include "../interpreter/wasm.h"
#include "../common/wasm_runtime_common.h"
/* clang-format off */
#define validate_native_addr(addr, size) \
wasm_runtime_validate_native_addr(module_inst, addr, size)
#define module_shared_malloc(size, p_native_addr) \
wasm_runtime_shared_heap_malloc(module_inst, size, p_native_addr)
#define module_shared_free(offset) \
wasm_runtime_shared_heap_free(module_inst, offset)
/* clang-format on */
static uint32
shared_malloc_wrapper(wasm_exec_env_t exec_env, uint32 size)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
return (uint32)module_shared_malloc((uint64)size, NULL);
}
static void
shared_free_wrapper(wasm_exec_env_t exec_env, void *ptr)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
if (!validate_native_addr(ptr, (uint64)sizeof(uint32)))
return;
module_shared_free(addr_native_to_app(ptr));
}
/* clang-format off */
#define REG_NATIVE_FUNC(func_name, signature) \
{ #func_name, func_name##_wrapper, signature, NULL }
/* clang-format on */
static NativeSymbol native_symbols_shared_heap[] = {
REG_NATIVE_FUNC(shared_malloc, "(i)i"),
REG_NATIVE_FUNC(shared_free, "(*)"),
};
uint32
get_lib_shared_heap_export_apis(NativeSymbol **p_shared_heap_apis)
{
*p_shared_heap_apis = native_symbols_shared_heap;
return sizeof(native_symbols_shared_heap) / sizeof(NativeSymbol);
}

View File

@ -1402,6 +1402,83 @@ wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
}
}
#if WASM_ENABLE_SHARED_HEAP != 0
static void
attach_shared_heap_visitor(void *node, void *heap)
{
WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
wasm_runtime_attach_shared_heap_internal(module_inst, heap);
}
static void
detach_shared_heap_visitor(void *node, void *heap)
{
WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
(void)heap;
wasm_runtime_detach_shared_heap_internal(module_inst);
}
bool
wasm_cluster_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
WASMSharedHeap *heap)
{
WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
if (module_inst->module_type == Wasm_Module_Bytecode) {
if (((WASMModuleInstance *)module_inst)->e->shared_heap) {
LOG_WARNING("A shared heap is already attached");
return false;
}
}
else if (module_inst->module_type == Wasm_Module_AoT) {
// TODO
}
if (exec_env == NULL) {
/* Maybe threads have not been started yet. */
return wasm_runtime_attach_shared_heap_internal(module_inst, heap);
}
else {
WASMCluster *cluster;
cluster = wasm_exec_env_get_cluster(exec_env);
bh_assert(cluster);
os_mutex_lock(&cluster->lock);
traverse_list(&cluster->exec_env_list, attach_shared_heap_visitor,
heap);
os_mutex_unlock(&cluster->lock);
}
return true;
}
void
wasm_cluster_detach_shared_heap(WASMModuleInstanceCommon *module_inst)
{
WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
if (exec_env == NULL) {
/* Maybe threads have not been started yet. */
wasm_runtime_detach_shared_heap_internal(module_inst);
}
else {
WASMCluster *cluster;
cluster = wasm_exec_env_get_cluster(exec_env);
bh_assert(cluster);
os_mutex_lock(&cluster->lock);
traverse_list(&cluster->exec_env_list, detach_shared_heap_visitor,
NULL);
os_mutex_unlock(&cluster->lock);
}
}
#endif
#if WASM_ENABLE_MODULE_INST_CONTEXT != 0
struct inst_set_context_data {
void *key;

View File

@ -11,6 +11,9 @@
#include "wasm_export.h"
#include "../interpreter/wasm.h"
#include "../common/wasm_runtime_common.h"
#if WASM_ENABLE_SHARED_HEAP != 0
#include "../common/wasm_memory.h"
#endif
#ifdef __cplusplus
extern "C" {
@ -167,6 +170,15 @@ wasm_cluster_set_context(WASMModuleInstanceCommon *module_inst, void *key,
bool
wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env);
#if WASM_ENABLE_SHARED_HEAP != 0
bool
wasm_cluster_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
WASMSharedHeap *heap);
void
wasm_cluster_detach_shared_heap(WASMModuleInstanceCommon *module_inst);
#endif
#if WASM_ENABLE_DEBUG_INTERP != 0
#define WAMR_SIG_TRAP (5)
#define WAMR_SIG_STOP (19)