Merge main into dev/socket

This commit is contained in:
Wenyong Huang
2022-09-10 22:12:43 +08:00
74 changed files with 1478 additions and 646 deletions

View File

@ -135,6 +135,10 @@
#define WASM_ENABLE_LIBC_EMCC 0
#endif
#ifndef WASM_ENABLE_LIB_RATS
#define WASM_ENABLE_LIB_RATS 0
#endif
#ifndef WASM_ENABLE_LIB_PTHREAD
#define WASM_ENABLE_LIB_PTHREAD 0
#endif
@ -272,6 +276,30 @@
#define BH_ENABLE_GC_VERIFY 0
#endif
/* Enable global heap pool if heap verification is enabled */
#if BH_ENABLE_GC_VERIFY != 0
#define WASM_ENABLE_GLOBAL_HEAP_POOL 1
#endif
/* Global heap pool */
#ifndef WASM_ENABLE_GLOBAL_HEAP_POOL
#define WASM_ENABLE_GLOBAL_HEAP_POOL 0
#endif
#ifndef WASM_ENABLE_SPEC_TEST
#define WASM_ENABLE_SPEC_TEST 0
#endif
/* Global heap pool size in bytes */
#ifndef WASM_GLOBAL_HEAP_SIZE
#if WASM_ENABLE_SPEC_TEST != 0
/* Spec test requires more heap pool size */
#define WASM_GLOBAL_HEAP_SIZE (300 * 1024 * 1024)
#else
#define WASM_GLOBAL_HEAP_SIZE (10 * 1024 * 1024)
#endif
#endif
/* Max app number of all modules */
#define MAX_APP_INSTALLATIONS 3
@ -363,10 +391,6 @@
#endif
#define BLOCK_ADDR_CONFLICT_SIZE 2
#ifndef WASM_ENABLE_SPEC_TEST
#define WASM_ENABLE_SPEC_TEST 0
#endif
/* Default max thread num per cluster. Can be overwrite by
wasm_runtime_set_max_thread_num */
#define CLUSTER_MAX_THREAD_NUM 4

View File

@ -412,18 +412,27 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
if (init_page_count == max_page_count && init_page_count == 1) {
/* If only one page and at most one page, we just append
the app heap to the end of linear memory, enlarge the
num_bytes_per_page, and don't change the page count*/
num_bytes_per_page, and don't change the page count */
heap_offset = num_bytes_per_page;
num_bytes_per_page += heap_size;
if (num_bytes_per_page < heap_size) {
set_error_buf(error_buf, error_buf_size,
"memory size must be at most 65536 pages (4GiB)");
"failed to insert app heap into linear memory, "
"try using `--heap_size=0` option");
return NULL;
}
}
else if (heap_size > 0) {
if (module->aux_heap_base_global_index != (uint32)-1
&& module->aux_heap_base < num_bytes_per_page * init_page_count) {
if (init_page_count == max_page_count && init_page_count == 0) {
/* If the memory data size is always 0, we resize it to
one page for app heap */
num_bytes_per_page = heap_size;
heap_offset = 0;
inc_page_count = 1;
}
else if (module->aux_heap_base_global_index != (uint32)-1
&& module->aux_heap_base
< num_bytes_per_page * init_page_count) {
/* Insert app heap before __heap_base */
aux_heap_base = module->aux_heap_base;
bytes_of_last_page = aux_heap_base % num_bytes_per_page;
@ -464,13 +473,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
}
init_page_count += inc_page_count;
max_page_count += inc_page_count;
if (init_page_count > 65536) {
if (init_page_count > DEFAULT_MAX_PAGES) {
set_error_buf(error_buf, error_buf_size,
"memory size must be at most 65536 pages (4GiB)");
"failed to insert app heap into linear memory, "
"try using `--heap_size=0` option");
return NULL;
}
if (max_page_count > 65536)
max_page_count = 65536;
else if (init_page_count == DEFAULT_MAX_PAGES) {
num_bytes_per_page = UINT32_MAX;
init_page_count = max_page_count = 1;
}
if (max_page_count > DEFAULT_MAX_PAGES)
max_page_count = DEFAULT_MAX_PAGES;
}
LOG_VERBOSE("Memory instantiate:");
@ -487,6 +501,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
total_size = (uint64)num_bytes_per_page * max_page_count;
}
#endif
bh_assert(total_size <= UINT32_MAX);
#ifndef OS_ENABLE_HW_BOUND_CHECK
/* Allocate memory */
@ -502,9 +517,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
* both i and memarg.offset are u32 in range 0 to 4G
* so the range of ea is 0 to 8G
*/
if (total_size >= UINT32_MAX
|| !(p = mapped_mem =
os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
if (!(p = mapped_mem =
os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
return NULL;
}
@ -529,15 +543,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
* again here */
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
if (total_size > UINT32_MAX)
total_size = UINT32_MAX;
memory_inst->module_type = Wasm_Module_AoT;
memory_inst->num_bytes_per_page = num_bytes_per_page;
memory_inst->cur_page_count = init_page_count;
memory_inst->max_page_count = max_page_count;
memory_inst->memory_data_size = (uint32)total_size;
/* Init memory info */
memory_inst->memory_data.ptr = p;
memory_inst->memory_data_end.ptr = p + (uint32)total_size;
memory_inst->memory_data_size = (uint32)total_size;
/* Initialize heap info */
memory_inst->heap_data.ptr = p + heap_offset;
@ -561,21 +578,19 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
}
if (total_size > 0) {
if (sizeof(uintptr_t) == sizeof(uint64)) {
memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
}
else {
memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
memory_inst->mem_bound_check_16bytes.u32[0] =
(uint32)total_size - 16;
}
#if UINTPTR_MAX == UINT64_MAX
memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
#else
memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16;
#endif
}
#if WASM_ENABLE_SHARED_MEMORY != 0
@ -1470,7 +1485,8 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
#if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
if (!aot_alloc_frame(exec_env, function->func_index)) {
wasm_runtime_free(argv1);
if (argv1 != argv1_buf)
wasm_runtime_free(argv1);
return false;
}
#endif
@ -1479,9 +1495,6 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
func_type, NULL, NULL, argv1, argc, argv);
if (!ret || aot_get_exception(module_inst)) {
if (argv1 != argv1_buf)
wasm_runtime_free(argv1);
if (clear_wasi_proc_exit_exception(module_inst))
ret = true;
else
@ -1499,8 +1512,11 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
#if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
aot_free_frame(exec_env);
#endif
if (!ret)
if (!ret) {
if (argv1 != argv1_buf)
wasm_runtime_free(argv1);
return ret;
}
/* Get extra result values */
switch (func_type->types[func_type->param_count]) {
@ -1529,9 +1545,9 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets,
sizeof(uint32) * cell_num);
if (argv1 != argv1_buf)
wasm_runtime_free(argv1);
return true;
}
else {
@ -2034,26 +2050,29 @@ aot_get_native_addr_range(AOTModuleInstance *module_inst, uint8 *native_ptr,
bool
aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
{
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
uint32 num_bytes_per_page, cur_page_count, max_page_count;
uint32 total_page_count, total_size_old, heap_size;
uint64 total_size;
uint8 *memory_data_old, *heap_data_old, *memory_data, *heap_data;
AOTMemoryInstance *memory = aot_get_default_memory(module_inst);
uint8 *memory_data_old, *memory_data_new, *heap_data_old;
uint32 num_bytes_per_page, heap_size, total_size_old;
uint32 cur_page_count, max_page_count, total_page_count;
uint64 total_size_new;
bool ret = true;
if (!memory_inst)
if (!memory)
return false;
num_bytes_per_page = memory_inst->num_bytes_per_page;
cur_page_count = memory_inst->cur_page_count;
max_page_count = memory_inst->max_page_count;
total_page_count = cur_page_count + inc_page_count;
total_size_old = memory_inst->memory_data_size;
total_size = (uint64)num_bytes_per_page * total_page_count;
heap_size = (uint32)((uint8 *)memory_inst->heap_data_end.ptr
- (uint8 *)memory_inst->heap_data.ptr);
memory_data_old = (uint8 *)memory_inst->memory_data.ptr;
heap_data_old = (uint8 *)memory_inst->heap_data.ptr;
heap_data_old = (uint8 *)memory->heap_data.ptr;
heap_size = (uint32)((uint8 *)memory->heap_data_end.ptr
- (uint8 *)memory->heap_data.ptr);
memory_data_old = (uint8 *)memory->memory_data.ptr;
total_size_old =
(uint32)((uint8 *)memory->memory_data_end.ptr - memory_data_old);
num_bytes_per_page = memory->num_bytes_per_page;
cur_page_count = memory->cur_page_count;
max_page_count = memory->max_page_count;
total_page_count = inc_page_count + cur_page_count;
total_size_new = num_bytes_per_page * (uint64)total_page_count;
if (inc_page_count <= 0)
/* No need to enlarge memory */
@ -2064,94 +2083,103 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
return false;
}
if (total_size >= UINT32_MAX) {
return false;
bh_assert(total_size_new <= 4 * (uint64)BH_GB);
if (total_size_new > UINT32_MAX) {
/* Resize to 1 page with size 4G-1 */
num_bytes_per_page = UINT32_MAX;
total_page_count = max_page_count = 1;
total_size_new = UINT32_MAX;
}
#if WASM_ENABLE_SHARED_MEMORY != 0
if (memory_inst->is_shared) {
/* For shared memory, we have reserved the maximum spaces during
instantiate, only change the cur_page_count here */
memory_inst->cur_page_count = total_page_count;
if (memory->is_shared) {
memory->num_bytes_per_page = UINT32_MAX;
memory->cur_page_count = total_page_count;
memory->max_page_count = max_page_count;
memory->memory_data_size = (uint32)total_size_new;
return true;
}
#endif
if (heap_size > 0) {
if (mem_allocator_is_heap_corrupted(memory_inst->heap_handle.ptr)) {
if (mem_allocator_is_heap_corrupted(memory->heap_handle.ptr)) {
wasm_runtime_show_app_heap_corrupted_prompt();
return false;
}
}
if (!(memory_data =
wasm_runtime_realloc(memory_data_old, (uint32)total_size))) {
if (!(memory_data = wasm_runtime_malloc((uint32)total_size))) {
if (!(memory_data_new =
wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) {
if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) {
return false;
}
if (memory_data_old) {
bh_memcpy_s(memory_data, (uint32)total_size, memory_data_old,
total_size_old);
bh_memcpy_s(memory_data_new, (uint32)total_size_new,
memory_data_old, total_size_old);
wasm_runtime_free(memory_data_old);
}
}
memset(memory_data + total_size_old, 0,
(uint32)total_size - total_size_old);
memory_inst->cur_page_count = total_page_count;
memory_inst->memory_data_size = (uint32)total_size;
memory_inst->memory_data.ptr = memory_data;
memory_inst->memory_data_end.ptr = memory_data + total_size;
memset(memory_data_new + total_size_old, 0,
(uint32)total_size_new - total_size_old);
if (heap_size > 0) {
if (mem_allocator_migrate(memory_inst->heap_handle.ptr,
if (mem_allocator_migrate(memory->heap_handle.ptr,
(char *)heap_data_old
+ (memory_data - memory_data_old),
heap_size)) {
+ (memory_data_new - memory_data_old),
heap_size)
!= 0) {
/* Don't return here as memory->memory_data is obsolete and
must be updated to be correctly used later. */
ret = false;
}
}
heap_data = memory_data + (heap_data_old - memory_data_old);
memory_inst->heap_data.ptr = heap_data;
memory_inst->heap_data_end.ptr = heap_data + heap_size;
memory->heap_data.ptr = memory_data_new + (heap_data_old - memory_data_old);
memory->heap_data_end.ptr = (uint8 *)memory->heap_data.ptr + heap_size;
memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = total_page_count;
memory->max_page_count = max_page_count;
memory->memory_data.ptr = memory_data_new;
memory->memory_data_end.ptr = memory_data_new + (uint32)total_size_new;
memory->memory_data_size = (uint32)total_size_new;
#if UINTPTR_MAX == UINT64_MAX
memory->mem_bound_check_1byte.u64 = total_size_new - 1;
memory->mem_bound_check_2bytes.u64 = total_size_new - 2;
memory->mem_bound_check_4bytes.u64 = total_size_new - 4;
memory->mem_bound_check_8bytes.u64 = total_size_new - 8;
memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
#else
memory->mem_bound_check_1byte.u32[0] = (uint32)total_size_new - 1;
memory->mem_bound_check_2bytes.u32[0] = (uint32)total_size_new - 2;
memory->mem_bound_check_4bytes.u32[0] = (uint32)total_size_new - 4;
memory->mem_bound_check_8bytes.u32[0] = (uint32)total_size_new - 8;
memory->mem_bound_check_16bytes.u32[0] = (uint32)total_size_new - 16;
#endif
if (sizeof(uintptr_t) == sizeof(uint64)) {
memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
}
else {
memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16;
}
return ret;
}
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
bool
aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
{
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
uint32 num_bytes_per_page, cur_page_count, max_page_count;
uint32 total_page_count;
uint64 total_size;
AOTMemoryInstance *memory = aot_get_default_memory(module_inst);
uint32 num_bytes_per_page, total_size_old;
uint32 cur_page_count, max_page_count, total_page_count;
uint64 total_size_new;
if (!memory_inst)
if (!memory)
return false;
num_bytes_per_page = memory_inst->num_bytes_per_page;
cur_page_count = memory_inst->cur_page_count;
max_page_count = memory_inst->max_page_count;
total_page_count = cur_page_count + inc_page_count;
total_size = (uint64)num_bytes_per_page * total_page_count;
num_bytes_per_page = memory->num_bytes_per_page;
cur_page_count = memory->cur_page_count;
max_page_count = memory->max_page_count;
total_size_old = num_bytes_per_page * cur_page_count;
total_page_count = inc_page_count + cur_page_count;
total_size_new = num_bytes_per_page * (uint64)total_page_count;
if (inc_page_count <= 0)
/* No need to enlarge memory */
@ -2162,21 +2190,29 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
return false;
}
bh_assert(total_size_new <= 4 * (uint64)BH_GB);
if (total_size_new > UINT32_MAX) {
/* Resize to 1 page with size 4G-1 */
num_bytes_per_page = UINT32_MAX;
total_page_count = max_page_count = 1;
total_size_new = UINT32_MAX;
}
#ifdef BH_PLATFORM_WINDOWS
if (!os_mem_commit(memory_inst->memory_data_end.ptr,
num_bytes_per_page * inc_page_count,
if (!os_mem_commit(memory->memory_data_end.ptr,
(uint32)total_size_new - total_size_old,
MMAP_PROT_READ | MMAP_PROT_WRITE)) {
return false;
}
#endif
if (os_mprotect(memory_inst->memory_data_end.ptr,
num_bytes_per_page * inc_page_count,
if (os_mprotect(memory->memory_data_end.ptr,
(uint32)total_size_new - total_size_old,
MMAP_PROT_READ | MMAP_PROT_WRITE)
!= 0) {
#ifdef BH_PLATFORM_WINDOWS
os_mem_decommit(memory_inst->memory_data_end.ptr,
num_bytes_per_page * inc_page_count);
os_mem_decommit(memory->memory_data_end.ptr,
(uint32)total_size_new - total_size_old);
#endif
return false;
}
@ -2184,25 +2220,19 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
/* The increased pages are filled with zero by the OS when os_mmap,
no need to memset it again here */
memory_inst->cur_page_count = total_page_count;
memory_inst->memory_data_size = (uint32)total_size;
memory_inst->memory_data_end.ptr =
(uint8 *)memory_inst->memory_data.ptr + (uint32)total_size;
memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = total_page_count;
memory->max_page_count = max_page_count;
memory->memory_data_size = (uint32)total_size_new;
memory->memory_data_end.ptr =
(uint8 *)memory->memory_data.ptr + (uint32)total_size_new;
memory->mem_bound_check_1byte.u64 = total_size_new - 1;
memory->mem_bound_check_2bytes.u64 = total_size_new - 2;
memory->mem_bound_check_4bytes.u64 = total_size_new - 4;
memory->mem_bound_check_8bytes.u64 = total_size_new - 8;
memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
if (sizeof(uintptr_t) == sizeof(uint64)) {
memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
}
else {
memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16;
}
return true;
}
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */

View File

@ -53,6 +53,17 @@ void __subsf3();
void __truncdfsf2();
void __floatunsisf();
void __fixunsdfsi();
void __floatdisf();
void __floatdidf();
void __fixdfdi();
void __ltsf2();
void __gesf2();
void __eqdf2();
void __nedf2();
void __ltsf2();
void __nesf2();
void __unordsf2();
void __fixunssfsi();
#else
void __ac_push_13_to_13();
void __ac_push_13_to_14();
@ -162,6 +173,17 @@ static SymbolMap target_sym_map[] = {
REG_SYM(__truncdfsf2),
REG_SYM(__floatunsisf),
REG_SYM(__fixunsdfsi),
REG_SYM(__floatdisf),
REG_SYM(__floatdidf),
REG_SYM(__fixdfdi),
REG_SYM(__ltsf2),
REG_SYM(__gesf2),
REG_SYM(__eqdf2),
REG_SYM(__nedf2),
REG_SYM(__ltsf2),
REG_SYM(__nesf2),
REG_SYM(__unordsf2),
REG_SYM(__fixunssfsi),
#else
REG_SYM(__ac_push_13_to_13),
REG_SYM(__ac_push_13_to_14),

View File

@ -97,8 +97,24 @@ static SymbolMap target_sym_map[] = {
/* clang-format off */
REG_COMMON_SYMBOLS
/* compiler-rt symbols that come from compiler(e.g. gcc) */
#if __ARM_ARCH != 6
REG_SYM(__adddf3),
REG_SYM(__addsf3),
REG_SYM(__divdf3),
REG_SYM(__extendsfdf2),
REG_SYM(__fixdfsi),
REG_SYM(__floatsidf),
REG_SYM(__floatsisf),
REG_SYM(__floatunsidf),
REG_SYM(__floatunsisf),
REG_SYM(__muldf3),
REG_SYM(__mulsf3),
REG_SYM(__subdf3),
REG_SYM(__subsf3),
REG_SYM(__truncdfsf2),
REG_SYM(__unorddf2),
REG_SYM(__unordsf2),
#endif
/* clang-format on */
REG_SYM(__aeabi_d2iz),
REG_SYM(__aeabi_d2lz),
@ -133,26 +149,19 @@ static SymbolMap target_sym_map[] = {
REG_SYM(__aeabi_uldivmod),
REG_SYM(__ashldi3),
REG_SYM(__clzsi2),
REG_SYM(__divdf3),
REG_SYM(__divdi3),
REG_SYM(__divsi3),
REG_SYM(__eqdf2),
REG_SYM(__eqsf2),
REG_SYM(__extendsfdf2),
REG_SYM(__fixdfdi),
REG_SYM(__fixdfsi),
REG_SYM(__fixsfdi),
REG_SYM(__fixunsdfdi),
REG_SYM(__fixunsdfsi),
REG_SYM(__fixunssfdi),
REG_SYM(__floatdidf),
REG_SYM(__floatdisf),
REG_SYM(__floatsidf),
REG_SYM(__floatsisf),
REG_SYM(__floatundidf),
REG_SYM(__floatundisf),
REG_SYM(__floatunsidf),
REG_SYM(__floatunsisf),
REG_SYM(__gedf2),
REG_SYM(__gesf2),
REG_SYM(__gtdf2),
@ -164,21 +173,14 @@ static SymbolMap target_sym_map[] = {
REG_SYM(__ltsf2),
REG_SYM(__moddi3),
REG_SYM(__modsi3),
REG_SYM(__muldf3),
REG_SYM(__muldi3),
REG_SYM(__mulsf3),
REG_SYM(__nedf2),
REG_SYM(__nesf2),
REG_SYM(__subdf3),
REG_SYM(__subsf3),
REG_SYM(__truncdfsf2),
REG_SYM(__udivdi3),
REG_SYM(__udivmoddi4),
REG_SYM(__udivsi3),
REG_SYM(__umoddi3),
REG_SYM(__umodsi3),
REG_SYM(__unorddf2),
REG_SYM(__unordsf2),
};
static void
@ -375,7 +377,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
if (error_buf != NULL)
snprintf(error_buf, error_buf_size,
"Load relocation section failed: "
"invalid relocation type %d.",
"invalid relocation type %" PRId32 ".",
reloc_type);
return false;
}

View File

@ -107,9 +107,6 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
the actual main function. Directly calling main function
may cause exception thrown. */
if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) {
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_runtime_start_debug_instance(exec_env);
#endif
return wasm_runtime_call_wasm(exec_env, func, 0, NULL);
}
#endif /* end of WASM_ENABLE_LIBC_WASI */
@ -191,10 +188,6 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
(uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
}
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_runtime_start_debug_instance(exec_env);
#endif
ret = wasm_runtime_call_wasm(exec_env, func, argc1, argv1);
if (ret && func_type->result_count > 0 && argc > 0 && argv)
/* copy the return value */
@ -240,113 +233,10 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
}
#if WASM_ENABLE_MULTI_MODULE != 0
static WASMModuleInstance *
get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
const char *sub_module_name)
{
WASMSubModInstNode *node =
bh_list_first_elem(parent_module_inst->sub_module_inst_list);
while (node && strcmp(node->module_name, sub_module_name)) {
node = bh_list_elem_next(node);
}
return node ? node->module_inst : NULL;
}
static bool
parse_function_name(char *orig_function_name, char **p_module_name,
char **p_function_name)
{
if (orig_function_name[0] != '$') {
*p_module_name = NULL;
*p_function_name = orig_function_name;
return true;
}
/**
* $module_name$function_name\0
* ===>
* module_name\0function_name\0
* ===>
* module_name
* function_name
*/
char *p1 = orig_function_name;
char *p2 = strchr(p1 + 1, '$');
if (!p2) {
LOG_DEBUG("can not parse the incoming function name");
return false;
}
*p_module_name = p1 + 1;
*p2 = '\0';
*p_function_name = p2 + 1;
return strlen(*p_module_name) && strlen(*p_function_name);
}
#endif
/**
* Implementation of wasm_application_execute_func()
*/
static bool
resolve_function(WASMModuleInstanceCommon *module_inst, const char *name,
WASMFunctionInstanceCommon **out_func,
WASMModuleInstanceCommon **out_module_inst)
{
WASMFunctionInstanceCommon *target_func = NULL;
WASMModuleInstanceCommon *target_inst = NULL;
#if WASM_ENABLE_MULTI_MODULE != 0
char *function_name = NULL;
char *orig_name = NULL;
char *sub_module_name = NULL;
uint32 length = (uint32)(strlen(name) + 1);
orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
if (!orig_name) {
goto LEAVE;
}
strncpy(orig_name, name, length);
if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
goto LEAVE;
}
LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
if (sub_module_name) {
target_inst = (WASMModuleInstanceCommon *)get_sub_module_inst(
(WASMModuleInstance *)module_inst, sub_module_name);
if (!target_inst) {
LOG_DEBUG("can not find a sub module named %s", sub_module_name);
goto LEAVE;
}
}
else {
target_inst = module_inst;
}
#else
const char *function_name = name;
target_inst = module_inst;
#endif
target_func =
wasm_runtime_lookup_function(target_inst, function_name, NULL);
#if WASM_ENABLE_MULTI_MODULE != 0
LEAVE:
if (orig_name)
wasm_runtime_free(orig_name);
#endif
*out_func = target_func;
*out_module_inst = target_inst;
return target_func;
}
union ieee754_float {
float f;
@ -393,7 +283,6 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
int32 argc, char *argv[])
{
WASMFunctionInstanceCommon *target_func;
WASMModuleInstanceCommon *target_inst;
WASMType *type = NULL;
WASMExecEnv *exec_env = NULL;
uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
@ -408,13 +297,14 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
bh_assert(argc >= 0);
LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
if (!resolve_function(module_inst, name, &target_func, &target_inst)) {
if (!(target_func =
wasm_runtime_lookup_function(module_inst, name, NULL))) {
snprintf(buf, sizeof(buf), "lookup function %s failed", name);
wasm_runtime_set_exception(module_inst, buf);
goto fail;
}
module_type = target_inst->module_type;
module_type = module_inst->module_type;
type = wasm_runtime_get_function_type(target_func, module_type);
if (!type) {
@ -446,7 +336,7 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
#endif
total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
if ((!(argv1 = runtime_malloc((uint32)total_size, target_inst, NULL, 0)))) {
if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
goto fail;
}
@ -618,10 +508,6 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
goto fail;
}
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_runtime_start_debug_instance(exec_env);
#endif
if (!wasm_runtime_call_wasm(exec_env, target_func, argc1, argv1)) {
goto fail;
}

View File

@ -75,8 +75,17 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
void
wasm_runtime_memory_destroy()
{
if (memory_mode == MEMORY_MODE_POOL)
mem_allocator_destroy(pool_allocator);
if (memory_mode == MEMORY_MODE_POOL) {
#if BH_ENABLE_GC_VERIFY == 0
(void)mem_allocator_destroy(pool_allocator);
#else
int ret = mem_allocator_destroy(pool_allocator);
if (ret != 0) {
/* Memory leak detected */
exit(-1);
}
#endif
}
memory_mode = MEMORY_MODE_UNKNOWN;
}
@ -167,3 +176,12 @@ wasm_runtime_free(void *ptr)
{
wasm_runtime_free_internal(ptr);
}
bool
wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
{
if (memory_mode == MEMORY_MODE_POOL) {
return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
}
return false;
}

View File

@ -53,6 +53,9 @@ get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
uint32
get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis);
uint32
get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis);
static bool
compare_type_with_signautre(uint8 type, const char signature)
{
@ -359,24 +362,24 @@ wasm_native_init()
#if WASM_ENABLE_LIBC_BUILTIN != 0
n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
if (!wasm_native_register_natives("env", native_symbols, n_native_symbols))
return false;
goto fail;
#endif /* WASM_ENABLE_LIBC_BUILTIN */
#if WASM_ENABLE_SPEC_TEST
n_native_symbols = get_spectest_export_apis(&native_symbols);
if (!wasm_native_register_natives("spectest", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif /* WASM_ENABLE_SPEC_TEST */
#if WASM_ENABLE_LIBC_WASI != 0
n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
if (!wasm_native_register_natives("wasi_unstable", native_symbols,
n_native_symbols))
return false;
goto fail;
if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif
#if WASM_ENABLE_BASE_LIB != 0
@ -384,7 +387,7 @@ wasm_native_init()
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif
#if WASM_ENABLE_APP_FRAMEWORK != 0
@ -392,18 +395,18 @@ wasm_native_init()
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif
#if WASM_ENABLE_LIB_PTHREAD != 0
if (!lib_pthread_init())
return false;
goto fail;
n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif
#if WASM_ENABLE_LIBC_EMCC != 0
@ -411,10 +414,21 @@ wasm_native_init()
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
return false;
goto fail;
#endif /* WASM_ENABLE_LIBC_EMCC */
#if WASM_ENABLE_LIB_RATS != 0
n_native_symbols = get_lib_rats_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
goto fail;
#endif /* WASM_ENABLE_LIB_RATS */
return true;
fail:
wasm_native_destroy();
return false;
}
void

View File

@ -341,10 +341,6 @@ wasm_runtime_init()
void
wasm_runtime_destroy()
{
#if WASM_ENABLE_FAST_JIT != 0
jit_compiler_destroy();
#endif
#if WASM_ENABLE_REF_TYPES != 0
wasm_externref_map_destroy();
#endif
@ -368,6 +364,14 @@ wasm_runtime_destroy()
os_mutex_destroy(&registered_module_list_lock);
#endif
#if WASM_ENABLE_FAST_JIT != 0
/* Destroy Fast-JIT compiler after destroying the modules
* loaded by multi-module feature, since the Fast JIT's
* code cache allocator may be used by these modules.
*/
jit_compiler_destroy();
#endif
#if WASM_ENABLE_SHARED_MEMORY
wasm_shared_memory_destroy();
#endif
@ -404,7 +408,6 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args)
#if WASM_ENABLE_DEBUG_INTERP != 0
if (strlen(init_args->ip_addr))
if (!wasm_debug_engine_init(init_args->ip_addr,
init_args->platform_port,
init_args->instance_port)) {
wasm_runtime_destroy();
return false;
@ -504,7 +507,7 @@ wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
#if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
uint32
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, int32_t port)
{
WASMModuleInstanceCommon *module_inst =
wasm_runtime_get_module_inst(exec_env);
@ -522,12 +525,18 @@ wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
return cluster->debug_inst->control_thread->port;
}
if (wasm_debug_instance_create(cluster)) {
if (wasm_debug_instance_create(cluster, port)) {
return cluster->debug_inst->control_thread->port;
}
return 0;
}
uint32
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
{
return wasm_runtime_start_debug_instance_with_port(exec_env, -1);
}
#endif
#if WASM_ENABLE_MULTI_MODULE != 0

View File

@ -565,6 +565,11 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
uint32 num_args, ...);
#if WASM_ENABLE_DEBUG_INTERP != 0
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN uint32
wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
int32_t port);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN uint32
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);

View File

@ -135,9 +135,9 @@ simd_integer_narrow_common(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVM_CONST(i32_fourteen), LLVM_CONST(i32_fifteen),
};
if (!(trunc_type[0] == LLVMVectorType(INT8_TYPE, 8))
|| !(trunc_type[1] == LLVMVectorType(INT16_TYPE, 4))
|| !(trunc_type[2] == LLVMVectorType(I32_TYPE, 2))) {
if (!(trunc_type[0] = LLVMVectorType(INT8_TYPE, 8))
|| !(trunc_type[1] = LLVMVectorType(INT16_TYPE, 4))
|| !(trunc_type[2] = LLVMVectorType(I32_TYPE, 2))) {
HANDLE_FAILURE("LLVMVectorType");
return false;
}

View File

@ -807,7 +807,11 @@ jit_compile_op_block(JitCompContext *cc, uint8 **p_frame_ip,
return true;
fail:
jit_block_destroy(block);
/* Only destroy the block if it hasn't been pushed into
the block stack, or if will be destroyed again when
destroying the block stack */
if (jit_block_stack_top(&cc->block_stack) != block)
jit_block_destroy(block);
return false;
}

View File

@ -539,8 +539,13 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op,
insn = GEN_INSN(DIV_U, rax_hreg, rax_hreg, right);
}
jit_lock_reg_in_insn(cc, insn, eax_hreg);
jit_lock_reg_in_insn(cc, insn, edx_hreg);
if (!insn) {
goto fail;
}
if (!jit_lock_reg_in_insn(cc, insn, eax_hreg)
|| !jit_lock_reg_in_insn(cc, insn, edx_hreg)) {
goto fail;
}
if (is_i32) {
res = jit_cc_new_reg_I32(cc);
@ -551,9 +556,12 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op,
insn1 = jit_insn_new_MOV(res, rax_hreg);
}
if (insn && insn1) {
jit_insn_insert_after(insn, insn1);
if (!insn1) {
jit_set_last_error(cc, "generate insn failed");
goto fail;
}
jit_insn_insert_after(insn, insn1);
break;
}
case INT_REM_S:
@ -576,8 +584,13 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op,
insn = GEN_INSN(REM_U, rdx_hreg, rax_hreg, right);
}
jit_lock_reg_in_insn(cc, insn, eax_hreg);
jit_lock_reg_in_insn(cc, insn, edx_hreg);
if (!insn) {
goto fail;
}
if (!jit_lock_reg_in_insn(cc, insn, eax_hreg)
|| !jit_lock_reg_in_insn(cc, insn, edx_hreg)) {
goto fail;
}
if (is_i32) {
res = jit_cc_new_reg_I32(cc);
@ -588,9 +601,12 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op,
insn1 = jit_insn_new_MOV(res, rdx_hreg);
}
if (insn && insn1) {
jit_insn_insert_after(insn, insn1);
if (!insn1) {
jit_set_last_error(cc, "generate insn failed");
goto fail;
}
jit_insn_insert_after(insn, insn1);
break;
}
#else
@ -1133,13 +1149,20 @@ compile_int_shl(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
insn = GEN_INSN(SHL, res, left, is_i32 ? ecx_hreg : rcx_hreg);
jit_lock_reg_in_insn(cc, insn, ecx_hreg);
if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
goto fail;
}
#else
GEN_INSN(SHL, res, left, right);
if (jit_get_last_error(cc)) {
goto fail;
}
#endif
shortcut:
return res;
fail:
return (JitReg)0;
}
static JitReg
@ -1164,13 +1187,20 @@ compile_int_shrs(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
insn = GEN_INSN(SHRS, res, left, is_i32 ? ecx_hreg : rcx_hreg);
jit_lock_reg_in_insn(cc, insn, ecx_hreg);
if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
goto fail;
}
#else
GEN_INSN(SHRS, res, left, right);
if (jit_get_last_error(cc)) {
goto fail;
}
#endif
shortcut:
return res;
fail:
return (JitReg)0;
}
static JitReg
@ -1195,13 +1225,20 @@ compile_int_shru(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
insn = GEN_INSN(SHRU, res, left, is_i32 ? ecx_hreg : rcx_hreg);
jit_lock_reg_in_insn(cc, insn, ecx_hreg);
if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
goto fail;
}
#else
GEN_INSN(SHRU, res, left, right);
if (jit_get_last_error(cc)) {
goto fail;
}
#endif
shortcut:
return res;
fail:
return (JitReg)0;
}
DEF_UNI_INT_CONST_OPS(rotl)
@ -1257,13 +1294,20 @@ compile_int_rotl(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
insn = GEN_INSN(ROTL, res, left, is_i32 ? ecx_hreg : rcx_hreg);
jit_lock_reg_in_insn(cc, insn, ecx_hreg);
if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
goto fail;
}
#else
GEN_INSN(ROTL, res, left, right);
if (jit_get_last_error(cc)) {
goto fail;
}
#endif
shortcut:
return res;
fail:
return (JitReg)0;
}
DEF_UNI_INT_CONST_OPS(rotr)
@ -1319,13 +1363,20 @@ compile_int_rotr(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
insn = GEN_INSN(ROTR, res, left, is_i32 ? ecx_hreg : rcx_hreg);
jit_lock_reg_in_insn(cc, insn, ecx_hreg);
if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
goto fail;
}
#else
GEN_INSN(ROTR, res, left, right);
if (jit_get_last_error(cc)) {
goto fail;
}
#endif
shortcut:
return res;
fail:
return (JitReg)0;
}
static bool

View File

@ -64,7 +64,7 @@ apply_compiler_passes(JitCompContext *cc)
cc->cur_pass_no = p - jit_globals.passes;
bh_assert(*p < COMPILER_PASS_NUM);
if (!compiler_passes[*p].run(cc)) {
if (!compiler_passes[*p].run(cc) || jit_get_last_error(cc)) {
LOG_VERBOSE("JIT: compilation failed at pass[%td] = %s\n",
p - jit_globals.passes, compiler_passes[*p].name);
return false;

View File

@ -70,6 +70,21 @@ get_module_reg(JitFrame *frame)
return frame->module_reg;
}
JitReg
get_import_func_ptrs_reg(JitFrame *frame)
{
JitCompContext *cc = frame->cc;
JitReg module_inst_reg = get_module_inst_reg(frame);
if (!frame->import_func_ptrs_reg) {
frame->import_func_ptrs_reg = cc->import_func_ptrs_reg;
GEN_INSN(
LDPTR, frame->import_func_ptrs_reg, module_inst_reg,
NEW_CONST(I32, offsetof(WASMModuleInstance, import_func_ptrs)));
}
return frame->import_func_ptrs_reg;
}
JitReg
get_fast_jit_func_ptrs_reg(JitFrame *frame)
{
@ -85,6 +100,21 @@ get_fast_jit_func_ptrs_reg(JitFrame *frame)
return frame->fast_jit_func_ptrs_reg;
}
JitReg
get_func_type_indexes_reg(JitFrame *frame)
{
JitCompContext *cc = frame->cc;
JitReg module_inst_reg = get_module_inst_reg(frame);
if (!frame->func_type_indexes_reg) {
frame->func_type_indexes_reg = cc->func_type_indexes_reg;
GEN_INSN(
LDPTR, frame->func_type_indexes_reg, module_inst_reg,
NEW_CONST(I32, offsetof(WASMModuleInstance, func_type_indexes)));
}
return frame->func_type_indexes_reg;
}
JitReg
get_global_data_reg(JitFrame *frame)
{
@ -376,7 +406,9 @@ clear_fixed_virtual_regs(JitFrame *frame)
frame->module_inst_reg = 0;
frame->module_reg = 0;
frame->import_func_ptrs_reg = 0;
frame->fast_jit_func_ptrs_reg = 0;
frame->func_type_indexes_reg = 0;
frame->global_data_reg = 0;
frame->aux_stack_bound_reg = 0;
frame->aux_stack_bottom_reg = 0;
@ -572,7 +604,9 @@ create_fixed_virtual_regs(JitCompContext *cc)
cc->module_inst_reg = jit_cc_new_reg_ptr(cc);
cc->module_reg = jit_cc_new_reg_ptr(cc);
cc->import_func_ptrs_reg = jit_cc_new_reg_ptr(cc);
cc->fast_jit_func_ptrs_reg = jit_cc_new_reg_ptr(cc);
cc->func_type_indexes_reg = jit_cc_new_reg_ptr(cc);
cc->global_data_reg = jit_cc_new_reg_ptr(cc);
cc->aux_stack_bound_reg = jit_cc_new_reg_I32(cc);
cc->aux_stack_bottom_reg = jit_cc_new_reg_I32(cc);
@ -697,6 +731,9 @@ form_and_translate_func(JitCompContext *cc)
*(jit_annl_end_bcip(cc, cc->exit_label)) =
cc->cur_wasm_module->load_addr;
if (jit_get_last_error(cc)) {
return false;
}
return true;
}

View File

@ -187,9 +187,15 @@ get_module_inst_reg(JitFrame *frame);
JitReg
get_module_reg(JitFrame *frame);
JitReg
get_import_func_ptrs_reg(JitFrame *frame);
JitReg
get_fast_jit_func_ptrs_reg(JitFrame *frame);
JitReg
get_func_type_indexes_reg(JitFrame *frame);
JitReg
get_global_data_reg(JitFrame *frame);
@ -507,6 +513,8 @@ set_local_f64(JitFrame *frame, int n, JitReg val)
#define PUSH(jit_value, value_type) \
do { \
if (!jit_value) \
goto fail; \
if (!jit_cc_push_value(cc, value_type, jit_value)) \
goto fail; \
} while (0)

View File

@ -474,8 +474,10 @@ jit_cc_destroy(JitCompContext *cc)
}
/* Release entry and exit blocks. */
jit_basic_block_delete(jit_cc_entry_basic_block(cc));
jit_basic_block_delete(jit_cc_exit_basic_block(cc));
if (0 != cc->entry_label)
jit_basic_block_delete(jit_cc_entry_basic_block(cc));
if (0 != cc->exit_label)
jit_basic_block_delete(jit_cc_exit_basic_block(cc));
/* clang-format off */
/* Release blocks and instructions. */

View File

@ -909,8 +909,12 @@ typedef struct JitFrame {
JitReg module_inst_reg;
/* WASM module */
JitReg module_reg;
/* module_inst->import_func_ptrs */
JitReg import_func_ptrs_reg;
/* module_inst->fast_jit_func_ptrs */
JitReg fast_jit_func_ptrs_reg;
/* module_inst->func_type_indexes */
JitReg func_type_indexes_reg;
/* Base address of global data */
JitReg global_data_reg;
/* Boundary of auxiliary stack */
@ -1027,8 +1031,12 @@ typedef struct JitCompContext {
JitReg module_inst_reg;
/* WASM module */
JitReg module_reg;
/* module_inst->import_func_ptrs */
JitReg import_func_ptrs_reg;
/* module_inst->fast_jit_func_ptrs */
JitReg fast_jit_func_ptrs_reg;
/* module_inst->func_type_indexes */
JitReg func_type_indexes_reg;
/* Base address of global data */
JitReg global_data_reg;
/* Boundary of auxiliary stack */

View File

@ -121,6 +121,13 @@ typedef union MemAllocOption {
} MemAllocOption;
#endif
/* Memory pool info */
typedef struct mem_alloc_info_t {
uint32_t total_size;
uint32_t total_free_size;
uint32_t highmark_size;
} mem_alloc_info_t;
/* WASM runtime initialize arguments */
typedef struct RuntimeInitArgs {
mem_alloc_type_t mem_alloc_type;
@ -137,7 +144,7 @@ typedef struct RuntimeInitArgs {
/* Debug settings, only used when
WASM_ENABLE_DEBUG_INTERP != 0 */
char ip_addr[128];
int platform_port;
int unused; /* was platform_port */
int instance_port;
/* Fast JIT code cache size */
@ -229,6 +236,12 @@ wasm_runtime_realloc(void *ptr, unsigned int size);
WASM_RUNTIME_API_EXTERN void
wasm_runtime_free(void *ptr);
/*
* Get memory info, only pool mode is supported now.
*/
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info);
/**
* Get the package type of a buffer.
*
@ -252,20 +265,18 @@ WASM_RUNTIME_API_EXTERN bool
wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size);
/**
* It is a callback for WAMR providing by embedding to load a module file
* into a buffer
* Callback to load a module file into a buffer in multi-module feature
*/
typedef bool (*module_reader)(const char *module_name,
uint8_t **p_buffer, uint32_t *p_size);
/**
* It is a callback for WAMR providing by embedding to release the buffer which
* is used by loading a module file
* Callback to release the buffer loaded by module_reader callback
*/
typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
/**
* To setup callbacks for reading and releasing a buffer about a module file
* Setup callbacks for reading and releasing a buffer about a module file
*
* @param reader a callback to read a module file into a buffer
* @param destroyer a callback to release above buffer
@ -275,7 +286,7 @@ wasm_runtime_set_module_reader(const module_reader reader,
const module_destroyer destroyer);
/**
* Give the "module" a name "module_name".
* can not assign a new name to a module if it already has a name
* Can not assign a new name to a module if it already has a name
*
* @param module_name indicate a name
* @param module the target module
@ -289,8 +300,8 @@ wasm_runtime_register_module(const char *module_name, wasm_module_t module,
char *error_buf, uint32_t error_buf_size);
/**
* To check if there is already a loaded module named module_name in the
* runtime. you will not want to load repeately
* Check if there is already a loaded module named module_name in the
* runtime. Repeately loading a module with the same name is not allowed.
*
* @param module_name indicate a name
*
@ -520,10 +531,19 @@ wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
* they are sharing the same cluster with the main exec_env.
*
* @param exec_env the execution environment to start debug instance
* @param port the port for the debug server to listen on.
* 0 means automatic assignment.
* -1 means to use the global setting in RuntimeInitArgs.
*
* @return debug port if success, 0 otherwise.
*/
WASM_RUNTIME_API_EXTERN uint32_t
wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env, int32_t port);
/**
* Same as wasm_runtime_start_debug_instance_with_port(env, -1).
*/
WASM_RUNTIME_API_EXTERN uint32_t
wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
/**
@ -1042,7 +1062,7 @@ wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
wasm_thread_callback_t callback, void *arg);
/**
* Waits a spawned thread to terminate
* Wait a spawned thread to terminate
*
* @param tid thread id
* @param retval if not NULL, output the return value of the thread

View File

@ -29,6 +29,7 @@ extern "C" {
#define VALUE_TYPE_ANY 0x42
#define DEFAULT_NUM_BYTES_PER_PAGE 65536
#define DEFAULT_MAX_PAGES 65536
#define NULL_REF (0xFFFFFFFF)

View File

@ -1266,7 +1266,7 @@ fail:
static bool
check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
{
if (init_size > 65536) {
if (init_size > DEFAULT_MAX_PAGES) {
set_error_buf(error_buf, error_buf_size,
"memory size must be at most 65536 pages (4GiB)");
return false;
@ -1284,7 +1284,7 @@ check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
return false;
}
if (max_size > 65536) {
if (max_size > DEFAULT_MAX_PAGES) {
set_error_buf(error_buf, error_buf_size,
"memory size must be at most 65536 pages (4GiB)");
return false;
@ -1299,12 +1299,12 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = *p_buf, *p_end = buf_end;
uint32 pool_size = wasm_runtime_memory_pool_size();
#if WASM_ENABLE_APP_FRAMEWORK != 0
uint32 pool_size = wasm_runtime_memory_pool_size();
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ DEFAULT_NUM_BYTES_PER_PAGE;
#else
uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
uint32 max_page_count = DEFAULT_MAX_PAGES;
#endif /* WASM_ENABLE_APP_FRAMEWORK */
uint32 declare_max_page_count_flag = 0;
uint32 declare_init_page_count = 0;
@ -1529,12 +1529,12 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
uint32 pool_size = wasm_runtime_memory_pool_size();
#if WASM_ENABLE_APP_FRAMEWORK != 0
uint32 pool_size = wasm_runtime_memory_pool_size();
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ DEFAULT_NUM_BYTES_PER_PAGE;
#else
uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
uint32 max_page_count = DEFAULT_MAX_PAGES;
#endif
p_org = p;
@ -3317,17 +3317,30 @@ load_from_sections(WASMModule *module, WASMSection *sections,
#if WASM_ENABLE_MULTI_MODULE == 0
if (module->import_memory_count) {
memory_import = &module->import_memories[0].u.memory;
/* Memory init page count cannot be larger than 65536, we don't
check integer overflow again. */
memory_import->num_bytes_per_page *= memory_import->init_page_count;
memory_import->init_page_count = memory_import->max_page_count = 1;
if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
memory_import->num_bytes_per_page *=
memory_import->init_page_count;
else
memory_import->num_bytes_per_page = UINT32_MAX;
if (memory_import->init_page_count > 0)
memory_import->init_page_count = memory_import->max_page_count =
1;
else
memory_import->init_page_count = memory_import->max_page_count =
0;
}
if (module->memory_count) {
/* Memory init page count cannot be larger than 65536, we don't
check integer overflow again. */
memory = &module->memories[0];
memory->num_bytes_per_page *= memory->init_page_count;
memory->init_page_count = memory->max_page_count = 1;
if (memory->init_page_count < DEFAULT_MAX_PAGES)
memory->num_bytes_per_page *= memory->init_page_count;
else
memory->num_bytes_per_page = UINT32_MAX;
if (memory->init_page_count > 0)
memory->init_page_count = memory->max_page_count = 1;
else
memory->init_page_count = memory->max_page_count = 0;
}
#endif
}
@ -7300,7 +7313,6 @@ re_scan:
}
case WASM_OP_DROP:
case WASM_OP_DROP_64:
{
BranchBlock *cur_block = loader_ctx->frame_csp - 1;
int32 available_stack_cell =
@ -7368,7 +7380,6 @@ re_scan:
}
case WASM_OP_SELECT:
case WASM_OP_SELECT_64:
{
uint8 ref_type;
BranchBlock *cur_block = loader_ctx->frame_csp - 1;

View File

@ -534,12 +534,12 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = *p_buf, *p_end = buf_end;
uint32 pool_size = wasm_runtime_memory_pool_size();
#if WASM_ENABLE_APP_FRAMEWORK != 0
uint32 pool_size = wasm_runtime_memory_pool_size();
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ DEFAULT_NUM_BYTES_PER_PAGE;
#else
uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
uint32 max_page_count = DEFAULT_MAX_PAGES;
#endif /* WASM_ENABLE_APP_FRAMEWORK */
uint32 declare_max_page_count_flag = 0;
uint32 declare_init_page_count = 0;
@ -650,12 +650,12 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
uint32 pool_size = wasm_runtime_memory_pool_size();
#if WASM_ENABLE_APP_FRAMEWORK != 0
uint32 pool_size = wasm_runtime_memory_pool_size();
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ DEFAULT_NUM_BYTES_PER_PAGE;
#else
uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
uint32 max_page_count = DEFAULT_MAX_PAGES;
#endif
p_org = p;
@ -2153,18 +2153,31 @@ load_from_sections(WASMModule *module, WASMSection *sections,
if (module->import_memory_count) {
memory_import = &module->import_memories[0].u.memory;
/* Memory init page count cannot be larger than 65536, we don't
check integer overflow again. */
memory_import->num_bytes_per_page *= memory_import->init_page_count;
memory_import->init_page_count = memory_import->max_page_count = 1;
if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
memory_import->num_bytes_per_page *=
memory_import->init_page_count;
else
memory_import->num_bytes_per_page = UINT32_MAX;
if (memory_import->init_page_count > 0)
memory_import->init_page_count = memory_import->max_page_count =
1;
else
memory_import->init_page_count = memory_import->max_page_count =
0;
}
if (module->memory_count) {
/* Memory init page count cannot be larger than 65536, we don't
check integer overflow again. */
memory = &module->memories[0];
memory->num_bytes_per_page *= memory->init_page_count;
memory->init_page_count = memory->max_page_count = 1;
if (memory->init_page_count < DEFAULT_MAX_PAGES)
memory->num_bytes_per_page *= memory->init_page_count;
else
memory->num_bytes_per_page = UINT32_MAX;
if (memory->init_page_count > 0)
memory->init_page_count = memory->max_page_count = 1;
else
memory->init_page_count = memory->max_page_count = 0;
}
}
@ -5506,7 +5519,6 @@ re_scan:
}
case WASM_OP_DROP:
case WASM_OP_DROP_64:
{
BranchBlock *cur_block = loader_ctx->frame_csp - 1;
int32 available_stack_cell =
@ -5559,7 +5571,6 @@ re_scan:
}
case WASM_OP_SELECT:
case WASM_OP_SELECT_64:
{
uint8 ref_type;
BranchBlock *cur_block = loader_ctx->frame_csp - 1;

View File

@ -109,8 +109,11 @@ memories_deinstantiate(WASMModuleInstance *module_inst,
for (i = 0; i < count; i++) {
if (memories[i]) {
#if WASM_ENABLE_MULTI_MODULE != 0
if (i < module_inst->module->import_memory_count)
WASMModule *module = module_inst->module;
if (i < module->import_memory_count
&& module->import_memories[i].u.memory.import_module) {
continue;
}
#endif
#if WASM_ENABLE_SHARED_MEMORY != 0
if (memories[i]->is_shared) {
@ -203,18 +206,27 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
if (init_page_count == max_page_count && init_page_count == 1) {
/* If only one page and at most one page, we just append
the app heap to the end of linear memory, enlarge the
num_bytes_per_page, and don't change the page count*/
num_bytes_per_page, and don't change the page count */
heap_offset = num_bytes_per_page;
num_bytes_per_page += heap_size;
if (num_bytes_per_page < heap_size) {
set_error_buf(error_buf, error_buf_size,
"memory size must be at most 65536 pages (4GiB)");
"failed to insert app heap into linear memory, "
"try using `--heap_size=0` option");
return NULL;
}
}
else if (heap_size > 0) {
if (module->aux_heap_base_global_index != (uint32)-1
&& module->aux_heap_base < num_bytes_per_page * init_page_count) {
if (init_page_count == max_page_count && init_page_count == 0) {
/* If the memory data size is always 0, we resize it to
one page for app heap */
num_bytes_per_page = heap_size;
heap_offset = 0;
inc_page_count = 1;
}
else if (module->aux_heap_base_global_index != (uint32)-1
&& module->aux_heap_base
< num_bytes_per_page * init_page_count) {
/* Insert app heap before __heap_base */
aux_heap_base = module->aux_heap_base;
bytes_of_last_page = aux_heap_base % num_bytes_per_page;
@ -256,13 +268,18 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
}
init_page_count += inc_page_count;
max_page_count += inc_page_count;
if (init_page_count > 65536) {
if (init_page_count > DEFAULT_MAX_PAGES) {
set_error_buf(error_buf, error_buf_size,
"memory size must be at most 65536 pages (4GiB)");
"failed to insert app heap into linear memory, "
"try using `--heap_size=0` option");
return NULL;
}
if (max_page_count > 65536)
max_page_count = 65536;
else if (init_page_count == DEFAULT_MAX_PAGES) {
num_bytes_per_page = UINT32_MAX;
init_page_count = max_page_count = 1;
}
if (max_page_count > DEFAULT_MAX_PAGES)
max_page_count = DEFAULT_MAX_PAGES;
}
LOG_VERBOSE("Memory instantiate:");
@ -277,6 +294,7 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
memory_data_size = (uint64)num_bytes_per_page * max_page_count;
}
#endif
bh_assert(memory_data_size <= 4 * (uint64)BH_GB);
/* Allocate memory space, addr data and global data */
if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance), error_buf,
@ -298,9 +316,8 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
* both i and memarg.offset are u32 in range 0 to 4G
* so the range of ea is 0 to 8G
*/
if (memory_data_size >= UINT32_MAX
|| !(memory->memory_data = mapped_mem =
os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
if (!(memory->memory_data = mapped_mem =
os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
goto fail1;
}
@ -324,10 +341,14 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
* again here */
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
if (memory_data_size > UINT32_MAX)
memory_data_size = (uint32)memory_data_size;
memory->module_type = Wasm_Module_Bytecode;
memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = init_page_count;
memory->max_page_count = max_page_count;
memory->memory_data_size = (uint32)memory_data_size;
memory->heap_data = memory->memory_data + heap_offset;
memory->heap_data_end = memory->heap_data + heap_size;
@ -1241,6 +1262,47 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
return true;
}
#if WASM_ENABLE_FAST_JIT != 0
static uint32
get_smallest_type_idx(WASMModule *module, WASMType *func_type)
{
uint32 i;
for (i = 0; i < module->type_count; i++) {
if (func_type == module->types[i])
return i;
}
bh_assert(0);
return -1;
}
static bool
init_func_type_indexes(WASMModuleInstance *module_inst, char *error_buf,
uint32 error_buf_size)
{
uint32 i;
uint64 total_size = (uint64)sizeof(uint32) * module_inst->function_count;
/* Allocate memory */
if (!(module_inst->func_type_indexes =
runtime_malloc(total_size, error_buf, error_buf_size))) {
return false;
}
for (i = 0; i < module_inst->function_count; i++) {
WASMFunctionInstance *func_inst = module_inst->functions + i;
WASMType *func_type = func_inst->is_import_func
? func_inst->u.func_import->func_type
: func_inst->u.func->func_type;
module_inst->func_type_indexes[i] =
get_smallest_type_idx(module_inst->module, func_type);
}
return true;
}
#endif
/**
* Instantiate module
*/
@ -1363,6 +1425,10 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
&& !(module_inst->export_globals = export_globals_instantiate(
module, module_inst, module_inst->export_glob_count,
error_buf, error_buf_size)))
#endif
#if WASM_ENABLE_FAST_JIT != 0
|| (module_inst->function_count > 0
&& !init_func_type_indexes(module_inst, error_buf, error_buf_size))
#endif
) {
goto fail;
@ -1693,6 +1759,11 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
if (!module_inst)
return;
#if WASM_ENABLE_FAST_JIT != 0
if (module_inst->func_type_indexes)
wasm_runtime_free(module_inst->func_type_indexes);
#endif
#if WASM_ENABLE_MULTI_MODULE != 0
sub_module_deinstantiate(module_inst);
#endif
@ -2162,27 +2233,11 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
addr = mem_allocator_malloc(memory->heap_handle, size);
}
else if (module_inst->malloc_function && module_inst->free_function) {
#if WASM_ENABLE_DEBUG_INTERP != 0
/* TODO: obviously, we can not create debug instance for
* module malloc here, so, just disable the engine here,
* it is strange, but we now are lack of ways to indicate
* which calls should not be debugged. And we have other
* execute_xxx_function may need to be taken care of
*/
bool active = wasm_debug_get_engine_active();
wasm_debug_set_engine_active(false);
#endif
if (!execute_malloc_function(module_inst, module_inst->malloc_function,
module_inst->retain_function, size,
&offset)) {
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_debug_set_engine_active(active);
#endif
return 0;
}
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_debug_set_engine_active(active);
#endif
/* If we use app's malloc function,
the default memory may be changed while memory growing */
memory = module_inst->default_memory;
@ -2261,17 +2316,7 @@ wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
else if (module_inst->malloc_function && module_inst->free_function
&& memory->memory_data <= addr
&& addr < memory->memory_data_end) {
#if WASM_ENABLE_DEBUG_INTERP != 0
/*TODO: obviously, we can not create debug instance for module
malloc here, so, just disable the engine here, it is strange. the
wasm's call should be marshed to its own thread */
bool active = wasm_debug_get_engine_active();
wasm_debug_set_engine_active(false);
#endif
execute_free_function(module_inst, module_inst->free_function, ptr);
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_debug_set_engine_active(active);
#endif
}
}
}
@ -2418,39 +2463,49 @@ bool
wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
{
WASMMemoryInstance *memory = module->default_memory;
uint8 *new_memory_data, *memory_data, *heap_data_old;
uint32 heap_size, total_size_old, total_page_count;
uint64 total_size;
uint8 *memory_data_old, *memory_data_new, *heap_data_old;
uint32 num_bytes_per_page, heap_size, total_size_old;
uint32 cur_page_count, max_page_count, total_page_count;
uint64 total_size_new;
bool ret = true;
if (!memory)
return false;
memory_data = memory->memory_data;
heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
total_size_old = (uint32)(memory->memory_data_end - memory_data);
total_page_count = inc_page_count + memory->cur_page_count;
total_size = memory->num_bytes_per_page * (uint64)total_page_count;
heap_data_old = memory->heap_data;
heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
memory_data_old = memory->memory_data;
total_size_old = memory->memory_data_size;
num_bytes_per_page = memory->num_bytes_per_page;
cur_page_count = memory->cur_page_count;
max_page_count = memory->max_page_count;
total_page_count = inc_page_count + cur_page_count;
total_size_new = num_bytes_per_page * (uint64)total_page_count;
if (inc_page_count <= 0)
/* No need to enlarge memory */
return true;
if (total_page_count < memory->cur_page_count /* integer overflow */
|| total_page_count > memory->max_page_count) {
if (total_page_count < cur_page_count /* integer overflow */
|| total_page_count > max_page_count) {
return false;
}
if (total_size >= UINT32_MAX) {
return false;
bh_assert(total_size_new <= 4 * (uint64)BH_GB);
if (total_size_new > UINT32_MAX) {
/* Resize to 1 page with size 4G-1 */
num_bytes_per_page = UINT32_MAX;
total_page_count = max_page_count = 1;
total_size_new = UINT32_MAX;
}
#if WASM_ENABLE_SHARED_MEMORY != 0
if (memory->is_shared) {
/* For shared memory, we have reserved the maximum spaces during
instantiate, only change the cur_page_count here */
memory->num_bytes_per_page = UINT32_MAX;
memory->cur_page_count = total_page_count;
memory->max_page_count = max_page_count;
return true;
}
#endif
@ -2462,25 +2517,25 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
}
}
if (!(new_memory_data =
wasm_runtime_realloc(memory_data, (uint32)total_size))) {
if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
if (!(memory_data_new =
wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) {
if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) {
return false;
}
if (memory_data) {
bh_memcpy_s(new_memory_data, (uint32)total_size, memory_data,
total_size_old);
wasm_runtime_free(memory_data);
if (memory_data_old) {
bh_memcpy_s(memory_data_new, (uint32)total_size_new,
memory_data_old, total_size_old);
wasm_runtime_free(memory_data_old);
}
}
memset(new_memory_data + total_size_old, 0,
(uint32)total_size - total_size_old);
memset(memory_data_new + total_size_old, 0,
(uint32)total_size_new - total_size_old);
if (heap_size > 0) {
if (mem_allocator_migrate(memory->heap_handle,
(char *)heap_data_old
+ (new_memory_data - memory_data),
+ (memory_data_new - memory_data_old),
heap_size)
!= 0) {
/* Don't return here as memory->memory_data is obsolete and
@ -2489,26 +2544,30 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
}
}
memory->memory_data = new_memory_data;
memory->cur_page_count = total_page_count;
memory->heap_data = new_memory_data + (heap_data_old - memory_data);
memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
memory->heap_data_end = memory->heap_data + heap_size;
memory->memory_data_end =
memory->memory_data + memory->num_bytes_per_page * total_page_count;
memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = total_page_count;
memory->max_page_count = max_page_count;
memory->memory_data_size = (uint32)total_size_new;
memory->memory_data = memory_data_new;
memory->memory_data_end = memory_data_new + (uint32)total_size_new;
#if WASM_ENABLE_FAST_JIT != 0
#if UINTPTR_MAX == UINT64_MAX
memory->mem_bound_check_1byte = total_size - 1;
memory->mem_bound_check_2bytes = total_size - 2;
memory->mem_bound_check_4bytes = total_size - 4;
memory->mem_bound_check_8bytes = total_size - 8;
memory->mem_bound_check_16bytes = total_size - 16;
memory->mem_bound_check_1byte = total_size_new - 1;
memory->mem_bound_check_2bytes = total_size_new - 2;
memory->mem_bound_check_4bytes = total_size_new - 4;
memory->mem_bound_check_8bytes = total_size_new - 8;
memory->mem_bound_check_16bytes = total_size_new - 16;
#else
memory->mem_bound_check_1byte = (uint32)total_size - 1;
memory->mem_bound_check_2bytes = (uint32)total_size - 2;
memory->mem_bound_check_4bytes = (uint32)total_size - 4;
memory->mem_bound_check_8bytes = (uint32)total_size - 8;
memory->mem_bound_check_16bytes = (uint32)total_size - 16;
memory->mem_bound_check_1byte = (uint32)total_size_new - 1;
memory->mem_bound_check_2bytes = (uint32)total_size_new - 2;
memory->mem_bound_check_4bytes = (uint32)total_size_new - 4;
memory->mem_bound_check_8bytes = (uint32)total_size_new - 8;
memory->mem_bound_check_16bytes = (uint32)total_size_new - 16;
#endif
#endif
@ -2519,39 +2578,52 @@ bool
wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
{
WASMMemoryInstance *memory = module->default_memory;
uint32 num_bytes_per_page, total_page_count;
uint32 num_bytes_per_page, total_size_old;
uint32 cur_page_count, max_page_count, total_page_count;
uint64 total_size_new;
if (!memory)
return false;
total_page_count = inc_page_count + memory->cur_page_count;
num_bytes_per_page = memory->num_bytes_per_page;
cur_page_count = memory->cur_page_count;
max_page_count = memory->max_page_count;
total_size_old = num_bytes_per_page * cur_page_count;
total_page_count = inc_page_count + cur_page_count;
total_size_new = num_bytes_per_page * (uint64)total_page_count;
if (inc_page_count <= 0)
/* No need to enlarge memory */
return true;
if (total_page_count < memory->cur_page_count /* integer overflow */
|| total_page_count > memory->max_page_count) {
if (total_page_count < cur_page_count /* integer overflow */
|| total_page_count > max_page_count) {
return false;
}
num_bytes_per_page = memory->num_bytes_per_page;
bh_assert(total_size_new <= 4 * (uint64)BH_GB);
if (total_size_new > UINT32_MAX) {
/* Resize to 1 page with size 4G-1 */
num_bytes_per_page = UINT32_MAX;
total_page_count = max_page_count = 1;
total_size_new = UINT32_MAX;
}
#ifdef BH_PLATFORM_WINDOWS
if (!os_mem_commit(memory->memory_data_end,
num_bytes_per_page * inc_page_count,
(uint32)total_size_new - total_size_old,
MMAP_PROT_READ | MMAP_PROT_WRITE)) {
return false;
}
#endif
if (os_mprotect(memory->memory_data_end,
num_bytes_per_page * inc_page_count,
(uint32)total_size_new - total_size_old,
MMAP_PROT_READ | MMAP_PROT_WRITE)
!= 0) {
#ifdef BH_PLATFORM_WINDOWS
os_mem_decommit(memory->memory_data_end,
num_bytes_per_page * inc_page_count);
(uint32)total_size_new - total_size_old);
#endif
return false;
}
@ -2559,9 +2631,20 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
/* The increased pages are filled with zero by the OS when os_mmap,
no need to memset it again here */
memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = total_page_count;
memory->memory_data_end =
memory->memory_data + num_bytes_per_page * total_page_count;
memory->max_page_count = max_page_count;
memory->memory_data_size = (uint32)total_size_new;
memory->memory_data_end = memory->memory_data + (uint32)total_size_new;
#if WASM_ENABLE_FAST_JIT != 0
memory->mem_bound_check_1byte = total_size_new - 1;
memory->mem_bound_check_2bytes = total_size_new - 2;
memory->mem_bound_check_4bytes = total_size_new - 4;
memory->mem_bound_check_8bytes = total_size_new - 8;
memory->mem_bound_check_16bytes = total_size_new - 16;
#endif
return true;
}
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */

View File

@ -26,12 +26,25 @@ struct WASMMemoryInstance {
uint32 module_type;
/* Shared memory flag */
bool is_shared;
/* Number bytes per page */
uint32 num_bytes_per_page;
/* Current page count */
uint32 cur_page_count;
/* Maximum page count */
uint32 max_page_count;
/* Memory data size */
uint32 memory_data_size;
/**
* Memory data begin address, Note:
* the app-heap might be inserted in to the linear memory,
* when memory is re-allocated, the heap data and memory data
* must be copied to new memory also
*/
uint8 *memory_data;
/* Memory data end address */
uint8 *memory_data_end;
/* Heap data base address */
uint8 *heap_data;
@ -45,14 +58,6 @@ struct WASMMemoryInstance {
korp_mutex mem_lock;
#endif
/* Memory data end address */
uint8 *memory_data_end;
/* Memory data begin address, the layout is: memory data + heap data
Note: when memory is re-allocated, the heap data and memory data
must be copied to new memory also. */
uint8 *memory_data;
#if WASM_ENABLE_FAST_JIT != 0
#if UINTPTR_MAX == UINT64_MAX
uint64 mem_bound_check_1byte;
@ -186,6 +191,7 @@ struct WASMModuleInstance {
#if WASM_ENABLE_FAST_JIT != 0
/* point to JITed functions */
void **fast_jit_func_ptrs;
uint32 *func_type_indexes;
#endif
WASMMemoryInstance **memories;

View File

@ -11,17 +11,15 @@
#include "wasm_opcode.h"
#include "wasm_runtime.h"
static uint8 break_instr[] = { DEBUG_OP_BREAK };
static const uint8 break_instr[] = { DEBUG_OP_BREAK };
typedef struct WASMDebugEngine {
struct WASMDebugEngine *next;
WASMDebugControlThread *control_thread;
char ip_addr[128];
int32 platform_port;
int32 process_base_port;
bh_list debug_instance_list;
korp_mutex instance_list_lock;
bool active;
} WASMDebugEngine;
void
@ -81,10 +79,12 @@ control_thread_routine(void *arg)
control_thread->debug_instance = debug_inst;
bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->ip_addr),
g_debug_engine->ip_addr);
control_thread->port =
(g_debug_engine->process_base_port == 0)
? 0
: g_debug_engine->process_base_port + debug_inst->id;
if (control_thread->port == -1) {
control_thread->port =
(g_debug_engine->process_base_port == 0)
? 0
: g_debug_engine->process_base_port + debug_inst->id - 1;
}
LOG_WARNING("control thread of debug object %p start\n", debug_inst);
@ -93,6 +93,7 @@ control_thread_routine(void *arg)
if (!control_thread->server) {
LOG_ERROR("Failed to create debug server\n");
control_thread->port = 0;
os_cond_signal(&debug_inst->wait_cond);
os_mutex_unlock(&debug_inst->wait_lock);
return NULL;
@ -178,7 +179,7 @@ control_thread_routine(void *arg)
}
static WASMDebugControlThread *
wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
wasm_debug_control_thread_create(WASMDebugInstance *debug_instance, int32 port)
{
WASMDebugControlThread *control_thread;
@ -188,6 +189,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
return NULL;
}
memset(control_thread, 0, sizeof(WASMDebugControlThread));
control_thread->port = port;
if (os_mutex_init(&control_thread->wait_lock) != 0)
goto fail;
@ -198,7 +200,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
if (0
!= os_thread_create(&control_thread->tid, control_thread_routine,
debug_instance, APP_THREAD_STACK_SIZE_MAX)) {
debug_instance, APP_THREAD_STACK_SIZE_DEFAULT)) {
os_mutex_unlock(&debug_instance->wait_lock);
goto fail1;
}
@ -265,16 +267,6 @@ wasm_debug_engine_create()
/* reset current instance id */
current_instance_id = 1;
/* TODO: support Wasm platform in LLDB */
/*
engine->control_thread =
wasm_debug_control_thread_create((WASMDebugObject *)engine);
engine->control_thread->debug_engine = (WASMDebugObject *)engine;
engine->control_thread->debug_instance = NULL;
sprintf(engine->control_thread->ip_addr, "127.0.0.1");
engine->control_thread->port = 1234;
*/
bh_list_init(&engine->debug_instance_list);
return engine;
}
@ -291,7 +283,7 @@ wasm_debug_engine_destroy()
}
bool
wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
wasm_debug_engine_init(char *ip_addr, int32 process_port)
{
if (wasm_debug_handler_init() != 0) {
return false;
@ -302,9 +294,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
}
if (g_debug_engine) {
process_port -= 1;
g_debug_engine->platform_port =
platform_port > 0 ? platform_port : 1234;
g_debug_engine->process_base_port =
(process_port > 0) ? process_port : 0;
if (ip_addr)
@ -313,7 +302,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
else
snprintf(g_debug_engine->ip_addr, sizeof(g_debug_engine->ip_addr),
"%s", "127.0.0.1");
g_debug_engine->active = true;
}
else {
wasm_debug_handler_deinit();
@ -322,33 +310,16 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
return g_debug_engine != NULL ? true : false;
}
void
wasm_debug_set_engine_active(bool active)
{
if (g_debug_engine) {
g_debug_engine->active = active;
}
}
bool
wasm_debug_get_engine_active(void)
{
if (g_debug_engine) {
return g_debug_engine->active;
}
return false;
}
/* A debug Instance is a debug "process" in gdb remote protocol
and bound to a runtime cluster */
WASMDebugInstance *
wasm_debug_instance_create(WASMCluster *cluster)
wasm_debug_instance_create(WASMCluster *cluster, int32 port)
{
WASMDebugInstance *instance;
WASMExecEnv *exec_env = NULL;
wasm_module_inst_t module_inst = NULL;
if (!g_debug_engine || !g_debug_engine->active) {
if (!g_debug_engine) {
return NULL;
}
@ -392,7 +363,7 @@ wasm_debug_instance_create(WASMCluster *cluster)
}
instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset;
if (!wasm_debug_control_thread_create(instance)) {
if (!wasm_debug_control_thread_create(instance, port)) {
LOG_ERROR("WASM Debug Engine error: failed to create control thread");
goto fail3;
}

View File

@ -108,7 +108,7 @@ void
on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env);
WASMDebugInstance *
wasm_debug_instance_create(WASMCluster *cluster);
wasm_debug_instance_create(WASMCluster *cluster, int32 port);
void
wasm_debug_instance_destroy(WASMCluster *cluster);
@ -117,17 +117,11 @@ WASMDebugInstance *
wasm_exec_env_get_instance(WASMExecEnv *exec_env);
bool
wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port);
wasm_debug_engine_init(char *ip_addr, int32 process_port);
void
wasm_debug_engine_destroy();
void
wasm_debug_set_engine_active(bool active);
bool
wasm_debug_get_engine_active(void);
WASMExecEnv *
wasm_debug_instance_get_current_env(WASMDebugInstance *instance);

View File

@ -18,7 +18,7 @@ struct packet_handler_elem {
#define DEL_HANDLER(r, h) [r] = { .request = r, .handler = h }
static struct packet_handler_elem packet_handler_table[255] = {
static const struct packet_handler_elem packet_handler_table[255] = {
DEL_HANDLER('Q', handle_general_set),
DEL_HANDLER('q', handle_general_query),
DEL_HANDLER('v', handle_v_packet),

View File

@ -10,19 +10,51 @@
#include "utils.h"
#include "wasm_runtime.h"
#define MAX_PACKET_SIZE (0x20000)
static char tmpbuf[MAX_PACKET_SIZE];
/*
* Note: A moderate MAX_PACKET_SIZE is ok because
* LLDB queries our buffer size (via qSupported PacketSize)
* and limits packet sizes accordingly.
*/
#if defined(DEBUG_MAX_PACKET_SIZE)
#define MAX_PACKET_SIZE DEBUG_MAX_PACKET_SIZE
#else
#define MAX_PACKET_SIZE (4096)
#endif
/*
* Note: It's assumed that MAX_PACKET_SIZE is reasonably large.
* See GetWorkingDir, WasmCallStack, etc.
*/
#if MAX_PACKET_SIZE < PATH_MAX || MAX_PACKET_SIZE < (2048 + 1)
#error MAX_PACKET_SIZE is too small
#endif
static char *tmpbuf;
static korp_mutex tmpbuf_lock;
int
wasm_debug_handler_init()
{
return os_mutex_init(&tmpbuf_lock);
int ret;
tmpbuf = wasm_runtime_malloc(MAX_PACKET_SIZE);
if (tmpbuf == NULL) {
LOG_ERROR("debug-engine: Packet buffer allocation failure");
return BHT_ERROR;
}
ret = os_mutex_init(&tmpbuf_lock);
if (ret != BHT_OK) {
wasm_runtime_free(tmpbuf);
tmpbuf = NULL;
}
return ret;
}
void
wasm_debug_handler_deinit()
{
wasm_runtime_free(tmpbuf);
tmpbuf = NULL;
os_mutex_destroy(&tmpbuf_lock);
}
@ -76,14 +108,17 @@ process_xfer(WASMGDBServer *server, const char *name, char *args)
os_mutex_lock(&tmpbuf_lock);
#if WASM_ENABLE_LIBC_WASI != 0
char objname[128];
wasm_debug_instance_get_current_object_name(
(WASMDebugInstance *)server->thread->debug_instance, objname, 128);
snprintf(tmpbuf, sizeof(tmpbuf),
if (!wasm_debug_instance_get_current_object_name(
(WASMDebugInstance *)server->thread->debug_instance, objname,
128)) {
objname[0] = 0; /* use an empty string */
}
snprintf(tmpbuf, MAX_PACKET_SIZE,
"l<library-list><library name=\"%s\"><section "
"address=\"0x%" PRIx64 "\"/></library></library-list>",
objname, addr);
#else
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"l<library-list><library name=\"%s\"><section "
"address=\"0x%" PRIx64 "\"/></library></library-list>",
"nobody.wasm", addr);
@ -103,7 +138,7 @@ process_wasm_local(WASMGDBServer *server, char *args)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "E01");
snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &local_index) == 2) {
ret = wasm_debug_instance_get_local(
(WASMDebugInstance *)server->thread->debug_instance, frame_index,
@ -126,7 +161,7 @@ process_wasm_global(WASMGDBServer *server, char *args)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "E01");
snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &global_index)
== 2) {
ret = wasm_debug_instance_get_global(
@ -161,14 +196,14 @@ handle_general_query(WASMGDBServer *server, char *payload)
(WASMDebugInstance *)server->thread->debug_instance);
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "QCp%" PRIx64 ".%" PRIx64 "", pid,
snprintf(tmpbuf, MAX_PACKET_SIZE, "QCp%" PRIx64 ".%" PRIx64 "", pid,
tid);
write_packet(server, tmpbuf);
os_mutex_unlock(&tmpbuf_lock);
}
if (!strcmp(name, "Supported")) {
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"qXfer:libraries:read+;PacketSize=%" PRIx32 ";",
MAX_PACKET_SIZE);
write_packet(server, tmpbuf);
@ -196,7 +231,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
strlen("wasm32-wamr-wasi-wasm"));
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"vendor:wamr;ostype:wasi;arch:wasm32;"
"triple:%s;endian:little;ptrsize:4;",
triple);
@ -227,7 +262,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
strlen("wasm32-wamr-wasi-wasm"));
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"pid:%" PRIx64 ";parent-pid:%" PRIx64
";vendor:wamr;ostype:wasi;arch:wasm32;"
"triple:%s;endian:little;ptrsize:4;",
@ -238,7 +273,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
if (!strcmp(name, "RegisterInfo0")) {
os_mutex_lock(&tmpbuf_lock);
snprintf(
tmpbuf, sizeof(tmpbuf),
tmpbuf, MAX_PACKET_SIZE,
"name:pc;alt-name:pc;bitsize:64;offset:0;encoding:uint;format:hex;"
"set:General Purpose Registers;gcc:16;dwarf:16;generic:pc;");
write_packet(server, tmpbuf);
@ -260,7 +295,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
mem2hex(mem_info->name, name_buf, strlen(mem_info->name));
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"start:%" PRIx64 ";size:%" PRIx64
";permissions:%s;name:%s;",
(uint64)mem_info->start, mem_info->size,
@ -339,7 +374,7 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
if (status == 0) {
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "W%02x", status);
snprintf(tmpbuf, MAX_PACKET_SIZE, "W%02x", status);
write_packet(server, tmpbuf);
os_mutex_unlock(&tmpbuf_lock);
return;
@ -355,16 +390,16 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
os_mutex_lock(&tmpbuf_lock);
// TODO: how name a wasm thread?
len += snprintf(tmpbuf, sizeof(tmpbuf), "T%02xthread:%" PRIx64 ";name:%s;",
len += snprintf(tmpbuf, MAX_PACKET_SIZE, "T%02xthread:%" PRIx64 ";name:%s;",
gdb_status, (uint64)(uintptr_t)tid, "nobody");
if (tids_count > 0) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "threads:");
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "threads:");
while (i < tids_count) {
if (i == tids_count - 1)
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"%" PRIx64 ";", (uint64)(uintptr_t)tids[i]);
else
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"%" PRIx64 ",", (uint64)(uintptr_t)tids[i]);
i++;
}
@ -383,29 +418,29 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
* correctly processed by LLDB */
uint32 exception_len = strlen(exception);
len +=
snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"thread-pcs:%" PRIx64 ";00:%s;reason:%s;description:", pc,
pc_string, "exception");
/* The description should be encoded as HEX */
for (i = 0; i < exception_len; i++) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "%02x",
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "%02x",
exception[i]);
}
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, ";");
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, ";");
}
else {
if (status == WAMR_SIG_TRAP) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
pc_string, "breakpoint");
}
else if (status == WAMR_SIG_SINGSTEP) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
pc_string, "trace");
}
else if (status > 0) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
pc_string, "signal");
}
@ -551,7 +586,7 @@ handle_get_read_memory(WASMGDBServer *server, char *payload)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
if (sscanf(payload, "%" SCNx64 ",%" SCNx64, &maddr, &mlen) == 2) {
char *buff;
@ -585,7 +620,7 @@ handle_get_write_memory(WASMGDBServer *server, char *payload)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
if (sscanf(payload, "%" SCNx64 ",%" SCNx64 ":%n", &maddr, &mlen, &offset)
== 2) {
payload += offset;
@ -599,7 +634,7 @@ handle_get_write_memory(WASMGDBServer *server, char *payload)
(WASMDebugInstance *)server->thread->debug_instance, maddr,
buff, &mlen);
if (ret) {
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
}
wasm_runtime_free(buff);
}
@ -681,7 +716,7 @@ handle_malloc(WASMGDBServer *server, char *payload)
}
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
size = strtoll(payload, NULL, 16);
if (size > 0) {
@ -701,7 +736,7 @@ handle_malloc(WASMGDBServer *server, char *payload)
(WASMDebugInstance *)server->thread->debug_instance, size,
map_prot);
if (addr) {
snprintf(tmpbuf, sizeof(tmpbuf), "%" PRIx64, addr);
snprintf(tmpbuf, MAX_PACKET_SIZE, "%" PRIx64, addr);
}
}
write_packet(server, tmpbuf);
@ -715,13 +750,13 @@ handle_free(WASMGDBServer *server, char *payload)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
addr = strtoll(payload, NULL, 16);
ret = wasm_debug_instance_ummap(
(WASMDebugInstance *)server->thread->debug_instance, addr);
if (ret) {
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
}
write_packet(server, tmpbuf);

View File

@ -0,0 +1,33 @@
# Copyright (c) 2022 Intel Corporation
# Copyright (c) 2020-2021 Alibaba Cloud
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (LIB_RATS_DIR ${CMAKE_CURRENT_LIST_DIR})
add_definitions (-DWASM_ENABLE_LIB_RATS=1)
include_directories(${LIB_RATS_DIR})
include(FetchContent)
set(RATS_BUILD_MODE "sgx"
CACHE INTERNAL "Select build mode for librats(host|occlum|sgxwasm)")
set(RATS_INSTALL_PATH "${CMAKE_BINARY_DIR}/librats" CACHE INTERNAL "")
FetchContent_Declare(
librats
GIT_REPOSITORY https://github.com/inclavare-containers/librats
GIT_TAG master
)
FetchContent_GetProperties(librats)
if (NOT librats_POPULATED)
message("-- Fetching librats ..")
FetchContent_Populate(librats)
include_directories("${librats_SOURCE_DIR}/include")
add_subdirectory(${librats_SOURCE_DIR} ${librats_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
file (GLOB source_all ${LIB_RATS_DIR}/*.c)
set (LIB_RATS_SOURCE ${source_all})

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2022 Intel Corporation
* Copyright (c) 2020-2021 Alibaba Cloud
*
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdlib.h>
#include <librats/api.h>
#include "wasm_export.h"
#include "bh_common.h"
static uint32
librats_collect_wrapper(wasm_exec_env_t exec_env, const uint8_t *hash)
{
char *json = NULL;
char *str_ret;
uint32 len;
uint32 str_ret_offset = 0;
wasm_module_inst_t module_inst = get_module_inst(exec_env);
int code = librats_collect_evidence_to_json(hash, &json);
if (code != 0) {
return str_ret_offset;
}
if (json) {
len = (uint32)strlen(json) + 1;
str_ret_offset = module_malloc(len, (void **)&str_ret);
if (str_ret_offset) {
bh_memcpy_s(str_ret, len, json, len);
}
}
return str_ret_offset;
}
static int
librats_verify_wrapper(wasm_exec_env_t exec_env, const char *evidence_json,
const uint8_t *hash)
{
return librats_verify_evidence_from_json(evidence_json, hash);
}
/* clang-format off */
#define REG_NATIVE_FUNC(func_name, signature) \
{ #func_name, func_name##_wrapper, signature, NULL }
/* clang-format on */
static NativeSymbol native_symbols_lib_rats[] = {
REG_NATIVE_FUNC(librats_collect, "($)i"),
REG_NATIVE_FUNC(librats_verify, "($$)i")
};
uint32_t
get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis)
{
*p_lib_rats_apis = native_symbols_lib_rats;
return sizeof(native_symbols_lib_rats) / sizeof(NativeSymbol);
}

View File

@ -0,0 +1,18 @@
/*
* Copyright (c) 2022 Intel Corporation
* Copyright (c) 2020-2021 Alibaba Cloud
*
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _RATS_WAMR_API_H
#define _RATS_WAMR_API_H
#include <stdint.h>
char *
librats_collect(const uint8_t *hash);
int
librats_verify(const char *json_string, const uint8_t *hash);
#endif

View File

@ -125,23 +125,24 @@ int
gc_destroy_with_pool(gc_handle_t handle)
{
gc_heap_t *heap = (gc_heap_t *)handle;
int ret = GC_SUCCESS;
#if BH_ENABLE_GC_VERIFY != 0
hmu_t *cur = (hmu_t *)heap->base_addr;
hmu_t *end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
if (!heap->is_heap_corrupted
&& (hmu_t *)((char *)cur + hmu_get_size(cur)) != end) {
os_printf("Memory leak detected:\n");
gci_dump(heap);
#if WASM_ENABLE_SPEC_TEST != 0
while (1) {
}
#endif
ret = GC_ERROR;
}
#endif
os_mutex_destroy(&heap->lock);
memset(heap->base_addr, 0, heap->current_size);
memset(heap, 0, sizeof(gc_heap_t));
return GC_SUCCESS;
return ret;
}
uint32

View File

@ -25,10 +25,10 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf,
pool_buf, pool_buf_size);
}
void
int
mem_allocator_destroy(mem_allocator_t allocator)
{
gc_destroy_with_pool((gc_handle_t)allocator);
return gc_destroy_with_pool((gc_handle_t)allocator);
}
uint32
@ -69,6 +69,13 @@ mem_allocator_is_heap_corrupted(mem_allocator_t allocator)
return gc_is_heap_corrupted((gc_handle_t)allocator);
}
bool
mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info)
{
gc_heap_stats((gc_handle_t)allocator, mem_alloc_info, 3);
return true;
}
#else /* else of DEFAULT_MEM_ALLOCATOR */
#include "tlsf/tlsf.h"

View File

@ -6,6 +6,10 @@ set (MEM_ALLOC_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${MEM_ALLOC_DIR})
if (WAMR_BUILD_GC_VERIFY EQUAL 1)
add_definitions (-DBH_ENABLE_GC_VERIFY=1)
endif ()
file (GLOB_RECURSE source_all
${MEM_ALLOC_DIR}/ems/*.c
${MEM_ALLOC_DIR}/tlsf/*.c

View File

@ -23,7 +23,7 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf,
void *pool_buf,
uint32_t pool_buf_size);
void
int
mem_allocator_destroy(mem_allocator_t allocator);
uint32
@ -45,6 +45,9 @@ mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new,
bool
mem_allocator_is_heap_corrupted(mem_allocator_t allocator);
bool
mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info);
#ifdef __cplusplus
}
#endif

View File

@ -30,6 +30,7 @@ typedef aos_task_t korp_thread;
typedef korp_thread *korp_tid;
typedef aos_task_t *aos_tid_t;
typedef aos_mutex_t korp_mutex;
typedef aos_sem_t korp_sem;
struct os_thread_wait_node;
typedef struct os_thread_wait_node *os_thread_wait_list;

View File

@ -59,7 +59,7 @@ bh_list_init(bh_list *list);
* <code>BH_LIST_ERROR</code> if input is invalid or no memory
* available.
*/
extern bh_list_status
bh_list_status
bh_list_insert(bh_list *list, void *elem);
/**