diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index be66fcd9..b6ca433d 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -11,6 +11,7 @@ #include "wasm_opcode.h" #include "wasm_loader.h" #include "wasm_memory.h" +#include "esp_log.h" #include "../common/wasm_exec_env.h" #include "../migration/wasm_migration.h" #include "../migration/wasm_dump.h" @@ -1427,6 +1428,8 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst, } // #define FETCH_OPCODE_AND_DISPATCH() goto *handle_table[*frame_ip++] +static int dispatch_count = 0; +static const char *TAG = "wasm-interp"; #define FETCH_OPCODE_AND_DISPATCH() \ do { \ CHECK_DUMP() \ diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 19455983..6992ef33 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -330,6 +330,9 @@ memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent, memory->heap_data_end = memory->heap_data + heap_size; memory->memory_data_end = memory->memory_data + memory_data_size; + // espのリニアメモリを0初期化する + memset(memory->memory_data, 0, memory_data_size); + /* Initialize heap */ if (heap_size > 0) { uint32 heap_struct_size = mem_allocator_get_heap_struct_size(); diff --git a/core/iwasm/migration/wasm_dump.c b/core/iwasm/migration/wasm_dump.c index 8a45cba7..3a9855e1 100644 --- a/core/iwasm/migration/wasm_dump.c +++ b/core/iwasm/migration/wasm_dump.c @@ -6,9 +6,12 @@ #include "wasm_migration.h" #include "wasm_dump.h" #include "wasm_dispatch.h" +#include "esp_log.h" #define BH_PLATFORM_LINUX 0 +static const char *TAG = "wasm-dump"; + // #define skip_leb(p) while (*p++ & 0x80) #define skip_leb(p) \ while (1) { \ @@ -50,7 +53,7 @@ int debug_memories(WASMModuleInstance *module) { // 積まれてるframe stackを出力する void debug_frame_info(WASMExecEnv* exec_env, WASMInterpFrame *frame) { - WASMModuleInstance *module = exec_env->module_inst; + WASMModuleInstance *module = (WASMModuleInstance *)exec_env->module_inst; int cnt = 0; printf("=== DEBUG Frame Stack ===\n"); @@ -108,11 +111,11 @@ int get_opcode_offset(uint8 *ip, uint8 *ip_lim) { // TODO: コードごちゃごちゃで読めないので、整理する uint8* get_type_stack(uint32 fidx, uint32 offset, uint32* type_stack_size, bool is_return_address) { - FILE *tablemap_func = fopen("tablemap_func", "rb"); + FILE *tablemap_func = fopen("FUNC", "rb"); if (!tablemap_func) printf("not found tablemap_func\n"); - FILE *tablemap_offset = fopen("tablemap_offset", "rb"); + FILE *tablemap_offset = fopen("OFFSET", "rb"); if (!tablemap_func) printf("not found tablemap_offset\n"); - FILE *type_table = fopen("type_table", "rb"); + FILE *type_table = fopen("TYPE", "rb"); if (!tablemap_func) printf("not found type_table\n"); /// tablemap_func @@ -178,7 +181,7 @@ static void _dump_stack(WASMExecEnv *exec_env, struct WASMInterpFrame *frame, FILE *fp, bool is_top) { int i; - WASMModuleInstance *module = exec_env->module_inst; + WASMModuleInstance *module = (WASMModuleInstance *)exec_env->module_inst; // Entry function // wasm_dump_stackの方でdump @@ -371,15 +374,37 @@ int dump_dirty_memory(WASMMemoryInstance *memory) { } int wasm_dump_memory(WASMMemoryInstance *memory) { - FILE *mem_size_fp = open_image("mem_page_count.img", "wb"); - - dump_dirty_memory(memory); + FILE *mem_size_fp = open_image("memcount.img", "wb"); + if (!mem_size_fp) { + ESP_LOGE(TAG, "Failed to open memcount.img"); + return -1; + } + // cur_pageをdump + if (fwrite(&(memory->cur_page_count), sizeof(uint32), 1, mem_size_fp) != 1) { + ESP_LOGE(TAG, "Failed to write memory page count"); + fclose(mem_size_fp); + return -1; + } + fclose(mem_size_fp); - printf("page_count: %d\n", memory->cur_page_count); - fwrite(&(memory->cur_page_count), sizeof(uint32), 1, mem_size_fp); + // dump_dirty_memory(memory); - fclose(mem_size_fp); + // memoryを全部吐く(ただし、cur_page分) + FILE *mem_fp = fopen("/memory.img", "wb"); + if (!mem_fp) { + ESP_LOGE(TAG, "Failed to open memory.img"); + return -1; + } + + int write_memory_size = WASM_PAGE_SIZE * memory->cur_page_count; + int ret = fwrite(memory->memory_data, sizeof(uint8), write_memory_size, mem_fp); + if (ret != write_memory_size) { + ESP_LOGE(TAG, "Failed to write memory"); + fclose(mem_fp); + return -1; + } + fclose(mem_fp); // デバッグのために、すべてのメモリも保存 // FILE *all_memory_fp = open_image("all_memory.img", "wb"); @@ -429,7 +454,7 @@ int wasm_dump_program_counter( ) { FILE *fp; - const char *file = "program_counter.img"; + const char *file = "program.img"; fp = fopen(file, "wb"); if (fp == NULL) { fprintf(stderr, "failed to open %s\n", file); diff --git a/core/iwasm/migration/wasm_migration.h b/core/iwasm/migration/wasm_migration.h index b3c50eec..669990af 100644 --- a/core/iwasm/migration/wasm_migration.h +++ b/core/iwasm/migration/wasm_migration.h @@ -1,6 +1,8 @@ #ifndef _WASM_MIGRATION_H #define _WASM_MIGRATION_H +#define WASM_PAGE_SIZE 65536 + // #include "../common/wasm_exec_env.h" #include "../interpreter/wasm_interp.h" diff --git a/core/iwasm/migration/wasm_restore.c b/core/iwasm/migration/wasm_restore.c index e31873a4..8c82391f 100644 --- a/core/iwasm/migration/wasm_restore.c +++ b/core/iwasm/migration/wasm_restore.c @@ -41,15 +41,15 @@ wasm_alloc_frame(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame static void _restore_stack(WASMExecEnv *exec_env, WASMInterpFrame *frame, FILE *fp) { - WASMModuleInstance *module_inst = exec_env->module_inst; + WASMModuleInstance *module_inst = (WASMModuleInstance *)exec_env->module_inst; WASMFunctionInstance *func = frame->function; int read_size = 0; // 初期化 frame->sp_bottom = frame->lp + func->param_cell_num + func->local_cell_num; frame->sp_boundary = frame->sp_bottom + func->u.func->max_stack_cell_num; - frame->csp_bottom = frame->sp_boundary; - frame->csp_boundary = frame->csp_bottom + func->u.func->max_block_num; + frame->csp_bottom = (WASMBranchBlock *)frame->sp_boundary; + frame->csp_boundary = (WASMBranchBlock *)(frame->csp_bottom + func->u.func->max_block_num); // frame->tsp_bottom = frame->csp_boundary; // frame->tsp_boundary = frame->tsp_bottom + func->u.func->max_stack_cell_num; @@ -198,18 +198,18 @@ void restore_dirty_memory(WASMMemoryInstance **memory, FILE* memory_fp) { int wasm_restore_memory(WASMModuleInstance *module, WASMMemoryInstance **memory, uint8** maddr) { FILE* memory_fp = open_image("memory.img", "rb"); - FILE* mem_size_fp = open_image("mem_page_count.img", "rb"); + FILE* mem_size_fp = open_image("memcount.img", "rb"); // restore page_count uint32 page_count; fread(&page_count, sizeof(uint32), 1, mem_size_fp); wasm_enlarge_memory(module, page_count- (*memory)->cur_page_count); - *maddr = page_count * (*memory)->num_bytes_per_page; + *maddr = (uint8 *)(page_count * WASM_PAGE_SIZE); - restore_dirty_memory(memory, memory_fp); + // restore_dirty_memory(memory, memory_fp); // restore memory_data - // fread((*memory)->memory_data, sizeof(uint8), - // (*memory)->num_bytes_per_page * (*memory)->cur_page_count, memory_fp); + int read_size = WASM_PAGE_SIZE * (*memory)->cur_page_count; + fread((*memory)->memory_data, sizeof(uint8), read_size, memory_fp); fclose(memory_fp); fclose(mem_size_fp); @@ -246,7 +246,7 @@ int wasm_restore_program_counter( WASMModuleInstance *module, uint8 **frame_ip) { - FILE* fp = open_image("program_counter.img", "rb"); + FILE* fp = open_image("program.img", "rb"); uint32 fidx, offset; fread(&fidx, sizeof(uint32), 1, fp);