Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() \
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
49 changes: 37 additions & 12 deletions core/iwasm/migration/wasm_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) { \
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/migration/wasm_migration.h
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
18 changes: 9 additions & 9 deletions core/iwasm/migration/wasm_restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down