Skip to content

Commit

Permalink
fix some issues
Browse files Browse the repository at this point in the history
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
  • Loading branch information
WenLY1 committed Sep 10, 2024
1 parent a878b4b commit ee0426a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 27 deletions.
35 changes: 17 additions & 18 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *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 true;
return false;
}
((WASMModuleInstance *)module_inst)->e->shared_heap = heap;
}
Expand All @@ -277,18 +277,17 @@ wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
return true;
}

bool
void
wasm_runtime_detach_shared_heap(WASMModuleInstanceCommon *module_inst)
{
#if WASM_ENABLE_THREAD_MGR != 0
wasm_cluster_detach_shared_heap(module_inst);
return true;
#else
return wasm_runtime_detach_shared_heap_internal(module_inst);
wasm_runtime_detach_shared_heap_internal(module_inst);
#endif
}

bool
void
wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst)
{
if (module_inst->module_type == Wasm_Module_Bytecode) {
Expand All @@ -297,8 +296,6 @@ wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst)
else if (module_inst->module_type == Wasm_Module_AoT) {
// TODO
}

return true;
}

bool
Expand Down Expand Up @@ -336,10 +333,14 @@ bool
is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst_comm,
uint8 *addr, uint32 bytes)
{
WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
|| module_inst_comm->module_type == Wasm_Module_AoT);
WASMSharedHeap *heap = module_inst->e->shared_heap;
WASMSharedHeap *heap = NULL;

if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
heap = ((WASMModuleInstance *)module_inst_comm)->e->shared_heap;
}
else if (module_inst_comm->module_type == Wasm_Module_AoT) {
// TODO
}

if (heap && addr >= heap->base_addr
&& addr + bytes <= heap->base_addr + heap->size
Expand Down Expand Up @@ -402,13 +403,11 @@ shared_heap_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
return NULL;
}

if (is_app_addr_in_shared_heap(module_inst, memory->is_memory64, ptr, 1)) {
if (memory->is_memory64) {
addr = heap->base_addr + (ptr - heap->start_off_mem64);
}
else {
addr = heap->base_addr + (ptr - heap->start_off_mem32);
}
if (memory->is_memory64) {
addr = heap->base_addr + (ptr - heap->start_off_mem64);
}
else {
addr = heap->base_addr + (ptr - heap->start_off_mem32);
}

return addr;
Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/common/wasm_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ bool
wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
void *shared_heap);

bool
void
wasm_runtime_detach_shared_heap(WASMModuleInstanceCommon *module_inst);
bool
void
wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst);

uint64
Expand Down
60 changes: 53 additions & 7 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,6 @@ typedef enum RunningMode {
Mode_Multi_Tier_JIT,
} RunningMode;

typedef enum {
/* heap type */
Shared_heap_alloc_With_Pool = 0,
Shared_heap_alloc_With_MMAP,
} shared_heap_alloc_type_t;

/* WASM runtime initialize arguments */
typedef struct RuntimeInitArgs {
mem_alloc_type_t mem_alloc_type;
Expand Down Expand Up @@ -232,7 +226,6 @@ typedef struct RuntimeInitArgs {
uint32_t llvm_jit_size_level;
/* Segue optimization flags for LLVM JIT */
uint32_t segue_flags;

/**
* If enabled
* - llvm-jit will output a jitdump file for `perf inject`
Expand Down Expand Up @@ -327,6 +320,12 @@ typedef enum {
WASM_LOG_LEVEL_VERBOSE = 4
} log_level_t;

#if WASM_ENABLE_SHARED_HEAP != 0
typedef struct SharedHeapInitArgs {
uint32 size;
} SharedHeapInitArgs;
#endif

/**
* Initialize the WASM runtime environment, and also initialize
* the memory allocator with system allocator, which calls os_malloc
Expand Down Expand Up @@ -2117,6 +2116,53 @@ wasm_runtime_detect_native_stack_overflow_size(wasm_exec_env_t exec_env,
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_is_underlying_binary_freeable(const wasm_module_t module);

#if WASM_ENABLE_SHARED_HEAP != 0
/**
* Create a shared heap
* @param init_args the initialization arguments
* @param error_buf buffer to output the error info if failed
* @param error_buf_size the size of the error buffer
*/
WASM_RUNTIME_API_EXTERN WASMSharedHeap *
wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args, char *error_buf,
uint32 error_buf_size);

/**
* Attach a shared heap to a module instance
* @param module_inst the module instance
* @param shared_heap the shared heap
*/
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
void *shared_heap);

/**
* Detach a shared heap from a module instance
* @param module_inst the module instance
*/
WASM_RUNTIME_API_EXTERN void
wasm_runtime_detach_shared_heap(WASMModuleInstanceCommon *module_inst);

/**
* Allocate memory from a shared heap
* @param module_inst the module instance
* @param size required memory size
* @param p_native_addr native address of allocated memory
*/
WASM_RUNTIME_API_EXTERN uint64
wasm_runtime_shared_heap_malloc(WASMModuleInstanceCommon *module_inst,
uint64 size, void **p_native_addr);

/**
* Free the memory allocated from shared heap
* @param module_inst the module instance
* @param ptr the offset in wasm app
*/
WASM_RUNTIME_API_EXTERN void
wasm_runtime_shared_heap_free(WASMModuleInstanceCommon *module_inst,
uint64 ptr);
#endif

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit ee0426a

Please sign in to comment.