From 9df086ac723613e776d7f1b00f1c66e88470ccc4 Mon Sep 17 00:00:00 2001 From: kikispace Date: Thu, 19 Oct 2023 14:01:07 -0700 Subject: [PATCH] 1. add read-file to output.txt 2. move the record and replay to wamr_export 3. reader writer seperately --- lib/wasm-micro-runtime | 2 +- src/checkpoint.cpp | 98 +++++++----------------------------------- src/restore.cpp | 32 +++++++------- src/wamr_export.cpp | 85 ++++++++++++++++++++++++++++++++++++ test/read-file.c | 7 ++- 5 files changed, 123 insertions(+), 101 deletions(-) create mode 100644 src/wamr_export.cpp diff --git a/lib/wasm-micro-runtime b/lib/wasm-micro-runtime index 6128c89..972bf93 160000 --- a/lib/wasm-micro-runtime +++ b/lib/wasm-micro-runtime @@ -1 +1 @@ -Subproject commit 6128c894fb5d324b1fd2d67eb6d2c9afb37c18a2 +Subproject commit 972bf938f6bbd23350c6f301257b4733c05dd1ac diff --git a/src/checkpoint.cpp b/src/checkpoint.cpp index 9adc186..ecda655 100644 --- a/src/checkpoint.cpp +++ b/src/checkpoint.cpp @@ -15,91 +15,14 @@ WAMRInstance *wamr = nullptr; std::ostringstream re{}; -auto writer = FwriteStream("test.bin"); +FwriteStream *writer; std::vector> as; std::mutex as_mtx; -void insert_sock_open_data(uint32_t poolfd, int af, int socktype, uint32_t* sockfd) { - if (wamr->sock_open_data.poolfd == 0) { - wamr->sock_open_data.poolfd = poolfd; - wamr->sock_open_data.af = af; - wamr->sock_open_data.socktype = socktype; - wamr->sock_open_data.sockfd = sockfd; - } else { - LOGV(ERROR) << "sock_open_data already exist"; - } -} - -void insert_sock_send_to_data(uint32_t sock, const iovec_app1_t* si_data, uint32 si_data_len, uint16_t si_flags, const __wasi_addr_t* dest_addr, uint32* so_data_len) { - if(wamr->sock_sendto_data.sock == 0) { - wamr->sock_sendto_data.sock = sock; - wamr->sock_sendto_data.si_data = si_data; - wamr->sock_sendto_data.si_data_len = si_data_len; - wamr->sock_sendto_data.si_flags = si_flags; - wamr->sock_sendto_data.dest_addr = dest_addr; - wamr->sock_sendto_data.so_data_len = so_data_len; - } else { - LOGV(ERROR) << "sock_sendto_data already exist"; - } -} - -void insert_sock_recv_from_data(uint32_t sock, iovec_app1_t* ri_data, uint32 ri_data_len, uint16_t ri_flags, __wasi_addr_t* src_addr, uint32* ro_data_len) { - if(wamr->sock_recvfrom_data.sock == 0) { - wamr->sock_recvfrom_data.sock = sock; - wamr->sock_recvfrom_data.ri_data = ri_data; - wamr->sock_recvfrom_data.ri_data_len = ri_data_len; - wamr->sock_recvfrom_data.ri_flags = ri_flags; - wamr->sock_recvfrom_data.src_addr = src_addr; - wamr->sock_recvfrom_data.ro_data_len = ro_data_len; - } else { - LOGV(ERROR) << "sock_recvfrom_data already exist"; - } -} - - -/**fopen, fseek*/ -void insert_fd(int fd, const char *path, int flags, int offset) { - if (fd > 2) { - printf("\n #insert_fd(fd,filename,flags, offset) %d %s %d %d \n\n", fd, path, flags, offset); - - if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) { - LOGV(ERROR) << "fd already exist" << fd; - if (offset == 0) { - // fOpen call - std::string curPath; - int curFlags; - int curOffset; - std::tie(curPath, curFlags, curOffset) = wamr->fd_map_[fd]; - wamr->fd_map_[fd] = std::make_tuple(std::string(path), flags, curOffset); - } else { - // fSeek call - std::string curPath; - int curFlags; - int curOffset; - std::tie(curPath, curFlags, curOffset) = wamr->fd_map_[fd]; - wamr->fd_map_[fd] = std::make_tuple(curPath, curFlags, offset); - } - - } else - wamr->fd_map_.insert(std::make_pair(fd, std::make_tuple(std::string(path), flags, offset))); - } -} - -/* update fd->offset**/ -void insert_fd_fseek(); -/**fclose */ -void remove_fd(int fd) { - if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) - wamr->fd_map_.erase(fd); - else - LOGV(ERROR) << "fd not found" << fd; -} -void insert_socket(int fd) {} void serialize_to_file(WASMExecEnv *instance) { /** Sounds like AoT/JIT is in this?*/ // Note: insert fd std::ifstream stdoutput; - stdoutput.open("output.txt"); std: string current_str; std::string fd_output; @@ -139,7 +62,7 @@ void serialize_to_file(WASMExecEnv *instance) { as.emplace_back(a); as.back().get()->cur_count = cur_count; if (as.size() == all_count) { - struct_pack::serialize_to(writer, as); + struct_pack::serialize_to(*writer, as); LOGV(INFO) << "serialize to file" << cur_count << " " << all_count << "\n"; } as_mtx.unlock(); @@ -150,7 +73,7 @@ void serialize_to_file(WASMExecEnv *instance) { dump(a, instance); as.emplace_back(a); as.back().get()->cur_count = 0; - struct_pack::serialize_to(writer, as); + struct_pack::serialize_to(*writer, as); } exit(0); @@ -205,7 +128,18 @@ int main(int argc, char *argv[]) { "n,ns_pool", "The ns lookup pool exposed to WAMR", cxxopts::value>()->default_value(""))("h,help", "The value for epoch value", cxxopts::value()->default_value("false")); - + auto removeExtension = [](std::string &filename) { + size_t dotPos = filename.find_last_of('.'); + std::string res; + if (dotPos != std::string::npos) { + // Extract the substring before the period + res = filename.substr(0, dotPos); + } else { + // If there's no period in the string, it means there's no extension. + LOGV(ERROR) << "No extension found."; + } + return res; + }; auto result = options.parse(argc, argv); if (result["help"].as()) { std::cout << options.help() << std::endl; @@ -219,10 +153,10 @@ int main(int argc, char *argv[]) { auto arg = result["arg"].as>(); auto addr = result["addr"].as>(); auto ns_pool = result["ns_pool"].as>(); + writer = new FwriteStream((removeExtension(target) + ".bin").c_str()); wamr = new WAMRInstance(target.c_str(), is_jit); wamr->set_wasi_args(dir, map_dir, env, arg, addr, ns_pool); wamr->instantiate(); - freopen("output.txt", "w", stdout); #ifndef MVVM_DEBUG // Define the sigaction structure diff --git a/src/restore.cpp b/src/restore.cpp index f7523d4..8bcab63 100644 --- a/src/restore.cpp +++ b/src/restore.cpp @@ -11,24 +11,9 @@ #include #include -auto reader = FreadStream("test.bin"); +FreadStream *reader; WAMRInstance *wamr = nullptr; -void insert_sock_open_data(uint32_t poolfd, int af, int socktype, uint32_t *sockfd) {} - -void insert_sock_send_to_data(uint32_t sock, const iovec_app1_t *si_data, uint32 si_data_len, uint16_t si_flags, - const __wasi_addr_t *dest_addr, uint32 *so_data_len) {} - -void insert_sock_recv_from_data(uint32_t sock, iovec_app1_t *ri_data, uint32 ri_data_len, uint16_t ri_flags, - __wasi_addr_t *src_addr, uint32 *ro_data_len) {} -void insert_fd(int fd, const char *path, int flags, int offset){}; -void remove_fd(int fd) {} void serialize_to_file(WASMExecEnv *instance) {} -void insert_socket(char const *, int){}; -void remove_socket(char const *){}; -void insert_lock(char const *, int){}; -void insert_sem(char const *, int){}; -void remove_lock(char const *){}; -void remove_sem(char const *){}; int main(int argc, char **argv) { cxxopts::Options options("MVVM", "Migratable Velocity Virtual Machine, to ship the VM state to another machine"); options.add_options()("t,target", "The webassembly file to execute", @@ -36,6 +21,18 @@ int main(int argc, char **argv) { "j,jit", "Whether the jit mode or interp mode", cxxopts::value()->default_value("false"))( "h,help", "The value for epoch value", cxxopts::value()->default_value("false")); // Can first discover from the wasi context. + auto removeExtension = [](std::string &filename) { + size_t dotPos = filename.find_last_of('.'); + std::string res; + if (dotPos != std::string::npos) { + // Extract the substring before the period + res = filename.substr(0, dotPos); + } else { + // If there's no period in the string, it means there's no extension. + LOGV(ERROR) << "No extension found."; + } + return res; + }; auto result = options.parse(argc, argv); if (result["help"].as()) { @@ -43,8 +40,9 @@ int main(int argc, char **argv) { exit(0); } auto target = result["target"].as(); + reader = new FreadStream((removeExtension(target) + ".bin").c_str()); wamr = new WAMRInstance(target.c_str(), false); - auto a = struct_pack::deserialize>>(reader).value(); + auto a = struct_pack::deserialize>>(*reader).value(); wamr->instantiate(); wamr->recover(&a); return 0; diff --git a/src/wamr_export.cpp b/src/wamr_export.cpp new file mode 100644 index 0000000..54d02d2 --- /dev/null +++ b/src/wamr_export.cpp @@ -0,0 +1,85 @@ +// +// Created by victoryang00 on 10/19/23. +// + +#include "wamr_block_addr.h" +#include "wamr.h" +extern WAMRInstance *wamr; + + +void insert_sock_open_data(uint32_t poolfd, int af, int socktype, uint32_t* sockfd) { + if (wamr->sock_open_data.poolfd == 0) { + wamr->sock_open_data.poolfd = poolfd; + wamr->sock_open_data.af = af; + wamr->sock_open_data.socktype = socktype; + wamr->sock_open_data.sockfd = sockfd; + } else { + LOGV(ERROR) << "sock_open_data already exist"; + } +} + +void insert_sock_send_to_data(uint32_t sock, const iovec_app1_t* si_data, uint32 si_data_len, uint16_t si_flags, const __wasi_addr_t* dest_addr, uint32* so_data_len) { + if(wamr->sock_sendto_data.sock == 0) { + wamr->sock_sendto_data.sock = sock; + wamr->sock_sendto_data.si_data = si_data; + wamr->sock_sendto_data.si_data_len = si_data_len; + wamr->sock_sendto_data.si_flags = si_flags; + wamr->sock_sendto_data.dest_addr = dest_addr; + wamr->sock_sendto_data.so_data_len = so_data_len; + } else { + LOGV(ERROR) << "sock_sendto_data already exist"; + } +} + +void insert_sock_recv_from_data(uint32_t sock, iovec_app1_t* ri_data, uint32 ri_data_len, uint16_t ri_flags, __wasi_addr_t* src_addr, uint32* ro_data_len) { + if(wamr->sock_recvfrom_data.sock == 0) { + wamr->sock_recvfrom_data.sock = sock; + wamr->sock_recvfrom_data.ri_data = ri_data; + wamr->sock_recvfrom_data.ri_data_len = ri_data_len; + wamr->sock_recvfrom_data.ri_flags = ri_flags; + wamr->sock_recvfrom_data.src_addr = src_addr; + wamr->sock_recvfrom_data.ro_data_len = ro_data_len; + } else { + LOGV(ERROR) << "sock_recvfrom_data already exist"; + } +} + + +/**fopen, fseek*/ +void insert_fd(int fd, const char *path, int flags, int offset) { + if (fd > 2) { + printf("\n #insert_fd(fd,filename,flags, offset) %d %s %d %d \n\n", fd, path, flags, offset); + + if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) { + LOGV(ERROR) << "fd already exist" << fd; + if (offset == 0) { + // fOpen call + std::string curPath; + int curFlags; + int curOffset; + std::tie(curPath, curFlags, curOffset) = wamr->fd_map_[fd]; + wamr->fd_map_[fd] = std::make_tuple(std::string(path), flags, curOffset); + } else { + // fSeek call + std::string curPath; + int curFlags; + int curOffset; + std::tie(curPath, curFlags, curOffset) = wamr->fd_map_[fd]; + wamr->fd_map_[fd] = std::make_tuple(curPath, curFlags, offset); + } + + } else + wamr->fd_map_.insert(std::make_pair(fd, std::make_tuple(std::string(path), flags, offset))); + } +} + +/* update fd->offset**/ +void insert_fd_fseek(); +/**fclose */ +void remove_fd(int fd) { + if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) + wamr->fd_map_.erase(fd); + else + LOGV(ERROR) << "fd not found" << fd; +} +void insert_socket(int fd) {} diff --git a/test/read-file.c b/test/read-file.c index 7cd2396..d5a6ec9 100644 --- a/test/read-file.c +++ b/test/read-file.c @@ -8,11 +8,16 @@ #include #include +FILE *a_file = NULL; + FILE *fopen_test(const char *restrict filename, const char *restrict mode) { FILE *f; int fd; int flags; + if(!a_file){ + a_file = fopen("./test.txt","a"); + } /* Check for valid initial mode character */ if (!strchr("rwa", *mode)) { @@ -27,7 +32,7 @@ FILE *fopen_test(const char *restrict filename, const char *restrict mode) #else // WASI libc ignores the mode parameter anyway, so skip the varargs. fd = __wasilibc_open_nomode(filename, flags); - printf("\n fopen_test(fd,filename,flags) %d %s %d \n\n",fd,filename,flags); + fprintf(a_file,"\n fopen_test(fd,filename,flags) %d %s %d \n\n",fd,filename,flags); #endif if (fd < 0) return 0; #ifdef __wasilibc_unmodified_upstream // WASI has no syscall