diff --git a/include/wamr.h b/include/wamr.h index 03a534b..0e91331 100644 --- a/include/wamr.h +++ b/include/wamr.h @@ -8,10 +8,11 @@ #include "bh_read_file.h" #include "logging.h" #include "wamr_exec_env.h" -#include "wamr_export.h" #include "wamr_read_write.h" -#include "wasm_export.h" +#include "wamr_wasi_context.h" #include "wasm_runtime.h" +#include "wamr_export.h" +#include class WAMRInstance { WASMExecEnv *exec_env{}; @@ -29,6 +30,7 @@ class WAMRInstance { std::vector ns_pool_; std::map> fd_map_; // add offset to pair->tuple, 3rd param 'int' + std::map socket_fd_map_; bool is_jit; char *buffer{}; char error_buf[128]{}; @@ -56,7 +58,8 @@ class WAMRInstance { const std::vector &addr_list, const std::vector &ns_lookup_pool); int invoke_main(); - int invoke_open(uint32 fd,const std::string& path, uint32 option); + int invoke_fopen(uint32 fd,const std::string& path, uint32 option); + int invoke_fseek(uint32 fd, uint32 offset); int invoke_preopen(uint32 fd,const std::string& path); ~WAMRInstance(); }; diff --git a/include/wamr_export.h b/include/wamr_export.h index c54c1e3..2a87e54 100644 --- a/include/wamr_export.h +++ b/include/wamr_export.h @@ -2,16 +2,20 @@ // Created by victoryang00 on 6/17/23. // + #ifdef __cplusplus extern "C" { #endif #ifdef MVVM_WASI #include "wasm_runtime.h" -void serialize_to_file(struct WASMExecEnv*); +void serialize_to_file(struct WASMExecEnv *); #endif + + void insert_fd(int, char const *, int, int); void remove_fd(int); -void insert_socket(char const *, int); +void insert_socket(int, int, int, int); +void update_socket_fd_address(int, struct SocketAddrPool *); void remove_socket(char const *); // see whether there's socket maintainance impl in wasi? void insert_lock(char const *, int); void insert_sem(char const *, int); diff --git a/include/wamr_wasi_context.h b/include/wamr_wasi_context.h index ac618be..51bca1c 100644 --- a/include/wamr_wasi_context.h +++ b/include/wamr_wasi_context.h @@ -6,6 +6,7 @@ #define MVVM_WAMR_WASI_CONTEXT_H #include "logging.h" +#include "platform_common.h" #include "wamr_serializer.h" #include "wasm_runtime.h" #include @@ -28,8 +29,15 @@ struct WAMRAddrPool { bool is_4; uint8 mask; }; + +struct SocketMetaData { + int domain{}; + int type{}; + int protocol{}; + SocketAddrPool socketAddress; +}; + struct WAMRWASIContext { - std::map> fd_map; std::vector dir; std::vector map_dir; WAMRArgvEnvironValues argv_environ; diff --git a/lib/wasm-micro-runtime b/lib/wasm-micro-runtime index a46c417..15ee584 160000 --- a/lib/wasm-micro-runtime +++ b/lib/wasm-micro-runtime @@ -1 +1 @@ -Subproject commit a46c41708fd634c89e13ad138978e915d35d1686 +Subproject commit 15ee5849d63cf2856a0e0b8faa1ff55bee576978 diff --git a/src/checkpoint.cpp b/src/checkpoint.cpp index 99cf6f2..8a4cce7 100644 --- a/src/checkpoint.cpp +++ b/src/checkpoint.cpp @@ -4,6 +4,8 @@ #include "thread_manager.h" #include "wamr.h" +#include "wamr_wasi_context.h" +#include "wasm_runtime.h" #include #include #include @@ -44,8 +46,6 @@ void insert_fd(int fd, const char *path, int flags, int offset) { 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()) @@ -53,9 +53,60 @@ void remove_fd(int fd) { else LOGV(ERROR)<< "fd not found" << fd; } -void insert_socket(int fd){ +/* + create fd-socketmetadata map and store the "domain", "type", "protocol" value +*/ +void insert_socket(int fd, int domain, int type, int protocol){ + printf("\n #insert_socket(fd, domain, type, protocol) %d %d %d %d \n\n",fd, domain, type, protocol); + + if (wamr->socket_fd_map_.find(fd) != wamr->socket_fd_map_.end()) { + LOGV(ERROR) << "socket_fd already exist" << fd; + } else{ + SocketMetaData metaData{}; + metaData.domain = domain; + metaData.type = type; + metaData.protocol = protocol; + wamr->socket_fd_map_.insert(std::make_pair(fd, metaData)); + } +} + +void update_socket_fd_address(int fd, SocketAddrPool *address) { + printf("\n #update_socket_fd_address(fd, address) %d \n\n",fd); + + if (wamr->socket_fd_map_.find(fd) == wamr->socket_fd_map_.end()) { + // note: ? fd here is not same as insert_socket? + // set default value + insert_socket(fd, 0, 0, 0); + } + + SocketMetaData metaData{}; + metaData.domain = wamr->socket_fd_map_[fd].domain; + metaData.type = wamr->socket_fd_map_[fd].type; + metaData.protocol = wamr->socket_fd_map_[fd].protocol; + + metaData.socketAddress.port = address->port; + if(address->is_4) { + metaData.socketAddress.is_4 = true; + metaData.socketAddress.ip4[0] = address->ip4[0]; + metaData.socketAddress.ip4[1] = address->ip4[1]; + metaData.socketAddress.ip4[2] = address->ip4[2]; + metaData.socketAddress.ip4[3] = address->ip4[3]; + + } else { + metaData.socketAddress.ip6[0] = address->ip6[0]; + metaData.socketAddress.ip6[1] = address->ip6[1]; + metaData.socketAddress.ip6[2] = address->ip6[2]; + metaData.socketAddress.ip6[3] = address->ip6[3]; + metaData.socketAddress.ip6[4] = address->ip6[4]; + metaData.socketAddress.ip6[5] = address->ip6[5]; + metaData.socketAddress.ip6[6] = address->ip6[6]; + metaData.socketAddress.ip6[7] = address->ip6[7]; + } + wamr->socket_fd_map_[fd] = metaData; + } + void serialize_to_file(WASMExecEnv *instance) { /** Sounds like AoT/JIT is in this?*/ // Note: insert fd diff --git a/src/restore.cpp b/src/restore.cpp index c117807..23635f5 100644 --- a/src/restore.cpp +++ b/src/restore.cpp @@ -6,6 +6,7 @@ #include "wamr.h" #include "wamr_exec_env.h" #include "wamr_read_write.h" +#include "wasm_runtime.h" #include #include #include @@ -16,7 +17,8 @@ WAMRInstance *wamr = nullptr; 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 insert_socket(int fd, int domain, int type, int protocol){}; +void update_socket_fd_address(int fd, SocketAddrPool *address){}; void remove_socket(char const *){}; void insert_lock(char const *, int){}; void insert_sem(char const *, int){}; diff --git a/src/wamr.cpp b/src/wamr.cpp index 8f362c8..52945fe 100644 --- a/src/wamr.cpp +++ b/src/wamr.cpp @@ -77,7 +77,7 @@ int WAMRInstance::invoke_main() { return wasm_runtime_call_wasm(exec_env, func, 0, nullptr); } -int WAMRInstance::invoke_open(uint32 fd, const std::string &path, uint32 option) { +int WAMRInstance::invoke_fopen(uint32 fd, const std::string &path, uint32 option) { if (!(func = wasm_runtime_lookup_function(module_inst, "open", "($i)i"))) { LOGV(ERROR) << "The wasi open function is not found."; return -1; @@ -97,6 +97,20 @@ int WAMRInstance::invoke_open(uint32 fd, const std::string &path, uint32 option) } return -1; }; +int WAMRInstance::invoke_fseek(uint32 fd, uint32 offset) { + if (!(func = wasm_runtime_lookup_function(module_inst, "fseek", "($i)i"))) { + LOGV(ERROR) << "The wasi open function is not found."; + return -1; + } + char *buffer_ = nullptr; + uint32_t buffer_for_wasm; + + buffer_for_wasm = wasm_runtime_module_malloc(module_inst, 100, reinterpret_cast(&buffer_)); + if (buffer_for_wasm != 0) { + uint32 argv[2]; + } + return -1; +}; int WAMRInstance::invoke_preopen(uint32 fd, const std::string &path) { auto name = "__wasilibc_register_preopened_fd"; if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { @@ -224,6 +238,7 @@ void WAMRInstance::set_wasi_args(const std::vector &dir_list, const wasm_runtime_set_wasi_addr_pool(module, addr_.data(), addr_.size()); wasm_runtime_set_wasi_ns_lookup_pool(module, ns_pool_.data(), ns_pool_.size()); } +void restart_execution(uint32 id) {} void WAMRInstance::set_wasi_args(WAMRWASIContext &context) { auto get_addr_from_context = [](const WAMRWASIContext &wasiContext) { auto addr_pool = std::vector(wasiContext.addr_pool.size()); diff --git a/src/wamr_wasi_context.cpp b/src/wamr_wasi_context.cpp index bb7b284..ade43c5 100644 --- a/src/wamr_wasi_context.cpp +++ b/src/wamr_wasi_context.cpp @@ -11,128 +11,9 @@ void WAMRWASIContext::dump_impl(WASIContext *env) { for (int i = 0; i < wamr->map_dir_.size(); i++) { map_dir.emplace_back(wamr->map_dir_[i]); } - for (auto [fd, res] : this->fd_map) { - // differ from path from file - LOGV(DEBUG)<curfds.size = env->curfds->size; - this->curfds.used = env->curfds->used; - wasi_fd_entry *entry = ((wasi_fd_entry *)env->curfds->entries); - for (int i = 0; i < env->curfds->size; i++) { - auto dumped_fo = WAMRFDObjectEntry(); - if (entry[i].object != nullptr) { - dumped_fo.type = entry[i].object->type; - dumped_fo.number = entry[i].object->number; - if (dumped_fo.number > 2 && wamr->fd_map_.contains(dumped_fo.number)) { - fd_map[dumped_fo.number] = wamr->fd_map_[dumped_fo.number]; - LOGV(DEBUG) << fmt::format("fd:{} path:{}", dumped_fo.number, fd_map[dumped_fo.number].first); - } - // open type? read write? or just rw - // if (((uint64)entry[i].object->directory.handle) > 10000) { - // auto d = readdir(entry[i].object->directory.handle); - // // got from dir path, is that one on one? - // // dumped_fo.dir=fmt::sprintf("%s/%s",dir_path, d->d_name); - // dumped_fo.dir = d->d_name; - // dumped_fo.offset = entry[i].object->directory.offset; - // } - LOGV(DEBUG) << "type:" << dumped_fo.type; - LOGV(DEBUG) << "number:" << dumped_fo.number; - } - dumped_fo.rights_base = entry[i].rights_base; - dumped_fo.rights_inheriting = entry[i].rights_inheriting; - this->curfds.entries.emplace_back(dumped_fo); - } -#endif -#if 0 - char path[256] = "/proc/self/fd"; - char fname[256]; - DIR *d; - int len, path_len, fd, flags; - - path_len = strlen(path); - d = opendir(path); - - if (!d) { - throw(std::runtime_error("open self fd error")); - } - - struct dirent *e; - while ((e = readdir(d)) != nullptr) { - if (e->d_name[0] == '.') - continue; // skip "." and ".." - - if (strlen(path) + 1 + strlen(e->d_name) >= 256) // overflow - { - closedir(d); - throw(std::runtime_error("open self fd overflow")); - } - path[path_len] = '/'; - path[path_len + 1] = 0; - strcat(path, e->d_name); - - len = readlink(path, fname, sizeof(fname) - 1); - if (len > 0) { - fname[len] = 0; - } - fd = atoi(e->d_name); - flags = fcntl(fd, F_GETFL); - fd_map[fd] = std::make_pair(fname, flags); - } - closedir(d); -#endif -#if 0 - this->prestats.size = env->prestats->size; - this->prestats.used = env->prestats->used; - auto removeTrailingSlashes = [](const std::string &input) { - size_t lastNotSlash = input.find_last_not_of('/'); - if (lastNotSlash != std::string::npos) { - // lastNotSlash + 1 is the new length of the string - return input.substr(0, lastNotSlash + 1); - } else { - // The string is all slashes - return std::string("/"); - } - }; - for (int i = 0; i < env->prestats->size; i++) { - auto dumped_pres = WAMRFDPrestat(); - auto dir_ = (const char *)((wasi_fd_prestats *)env->prestats)->prestats[i].dir; - if (dir_) { - dumped_pres.dir = removeTrailingSlashes(dir_); - LOGV(DEBUG) << dumped_pres.dir; - } - this->prestats.prestats.emplace_back(dumped_pres); - } - auto cur_pool = env->addr_pool; - while (cur_pool) { - if (cur_pool->addr.ip4 != 0) { - auto dumped_addr = WAMRAddrPool(); - if (cur_pool->type == 0) { - dumped_addr.is_4 = true; - memcpy(dumped_addr.ip4, &cur_pool->addr.ip4, sizeof(uint32)); - LOGV(DEBUG) << fmt::format("ip4:{}", cur_pool->addr.ip4); - } else { - dumped_addr.is_4 = false; - memcpy(dumped_addr.ip6, &cur_pool->addr.ip6, sizeof(uint16) * 8); - } - if (cur_pool->mask == 0) { - dumped_addr.mask = UINT8_MAX; - } - this->addr_pool.emplace_back(dumped_addr); - } - cur_pool = cur_pool->next; - } - // TODO: need to set tcp alive - this->argv_environ.argv_list = - std::vector(env->argv_environ->argv_list, env->argv_environ->argv_list + env->argv_environ->argc); - this->argv_environ.env_list = std::vector( - env->argv_environ->environ_list, env->argv_environ->environ_list + env->argv_environ->environ_count); -#endif this->exit_code = env->exit_code; } -void WAMRWASIContext::restore_impl(WASIContext *env) { +void WAMRWASIContext::git checkout main(WASIContext *env) { #if 0 // Need to open the file and reinitialize the file descripter by map. env->curfds->size = this->curfds.size; @@ -170,6 +51,7 @@ void WAMRWASIContext::restore_impl(WASIContext *env) { #endif for (auto [fd, res] : this->fd_map) { // differ from path from file - wamr->invoke_open(fd, res.first, res.second); + wamr->invoke_fopen(fd, std::get<0>(res),std::get<1>(res)); + wamr->invoke_fseek(fd, std::get<2>(res)); } }; \ No newline at end of file diff --git a/test/read-file.c b/test/read-file.c index 8c7c8bf..fd966a2 100644 --- a/test/read-file.c +++ b/test/read-file.c @@ -1,4 +1,6 @@ + // #include +#include #include #include #include diff --git a/test/server.c b/test/server.c index cbd70de..6e1f581 100644 --- a/test/server.c +++ b/test/server.c @@ -93,6 +93,9 @@ main(int argc, char *argv[]) printf("[Server] Create socket\n"); socket_fd = socket(af, SOCK_DGRAM, 0); + // socket(domain, type, protocol) + // snapshot af, sock_dgram, 0 + if (socket_fd < 0) { perror("Create socket failed"); goto fail;