From 951dada1cc6a45d10f7acbff0c57b05b56028141 Mon Sep 17 00:00:00 2001 From: kikispace Date: Mon, 23 Oct 2023 17:13:49 -0700 Subject: [PATCH] update new --- CMakeLists.txt | 1 - include/wamr.h | 39 ++-------- include/wamr_export.h | 29 +++++--- include/wamr_wasi_context.h | 41 +++++++++++ lib/wasm-micro-runtime | 2 +- src/wamr.cpp | 27 +++---- src/wamr_export.cpp | 140 +++++++++++++++++++++++++++--------- src/wamr_wasi_context.cpp | 32 ++++++--- 8 files changed, 213 insertions(+), 98 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b6b094..c782513 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,5 @@ target_link_libraries(MVVM_restore fmt::fmt cxxopts::cxxopts vmlib -lm -ldl -lpt target_link_libraries(MVVM_checkpoint fmt::fmt cxxopts::cxxopts vmlib -lm -ldl -lpthread ${LLVM_LLDB_LIB} ${LLVM_AVAILABLE_LIBS}) add_definitions(-DMVVM_INTERP=1) add_definitions(-DMVVM_DEBUG=1) -add_definitions(-DMVVM_WASI=1) add_definitions(-DCXXOPTS_NO_RTTI=1) add_subdirectory(test) \ No newline at end of file diff --git a/include/wamr.h b/include/wamr.h index a03345f..2bc5a56 100644 --- a/include/wamr.h +++ b/include/wamr.h @@ -12,32 +12,6 @@ #include "wamr_read_write.h" #include "wasm_runtime.h" - -struct WasiSockOpenData { - uint32_t poolfd; - int af; - int socktype; - uint32_t* sockfd; -}; - -struct WasiSockSendToData { - 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; -}; - -struct WasiSockRecvFromData { - 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; -}; - class WAMRInstance { WASMExecEnv *exec_env{}; WASMExecEnv *cur_env{}; @@ -54,10 +28,7 @@ class WAMRInstance { std::vector ns_pool_; std::map> fd_map_; // add offset to pair->tuple, 3rd param 'int' - - WasiSockOpenData sock_open_data; - WasiSockSendToData sock_sendto_data; - WasiSockRecvFromData sock_recvfrom_data; + std::map socket_fd_map_; bool is_jit; char *buffer{}; @@ -94,9 +65,11 @@ class WAMRInstance { int invoke_preopen(uint32 fd, const std::string &path); int invoke_socket(uint32 domain, const std::string &path); int invoke_(uint32 domain, const std::string &path); - int invoke_sock_sendto(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); - int invoke_sock_recvfrom(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); - int invoke_sock_open(uint32_t poolfd, int af, int socktype, uint32_t* sockfd); + int invoke_sock_sendto(uint32_t sock, const iovec_app_t *si_data, uint32 si_data_len, uint16_t si_flags, + const __wasi_addr_t *dest_addr, uint32 *so_data_len); + int invoke_sock_recvfrom(uint32_t sock, iovec_app_t *ri_data, uint32 ri_data_len, uint16_t ri_flags, + __wasi_addr_t *src_addr, uint32 *ro_data_len); + int invoke_sock_open(uint32_t poolfd, int af, int socktype, uint32_t *sockfd); ~WAMRInstance(); }; #endif // MVVM_WAMR_H diff --git a/include/wamr_export.h b/include/wamr_export.h index 15078bc..f22cb04 100644 --- a/include/wamr_export.h +++ b/include/wamr_export.h @@ -1,25 +1,36 @@ // // Created by victoryang00 on 6/17/23. // +#include "wasm_runtime.h" #ifdef __cplusplus extern "C" { #endif -#ifdef MVVM_WASI -#include "wasm_runtime.h" -typedef struct iovec_app1 { + +#if !defined(MVVM_WASI) +#define MVVM_WASI +struct SocketAddrPool { + uint16 ip4[4]; + uint16 ip6[8]; + bool is_4; + uint16 port; +}; +typedef struct iovec_app { uint32 buf_offset; uint32 buf_len; -} iovec_app1_t; - +} iovec_app_t; void serialize_to_file(struct WASMExecEnv *); + #endif -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_open_data(uint32_t, int, int, uint32_t *); -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_sock_send_to_data(uint32_t sock, const iovec_app_t *si_data, uint32 si_data_len, uint16_t si_flags, + const __wasi_addr_t *dest_addr, uint32 *so_data_len); +void insert_sock_open_data(uint32_t, int, int, uint32_t); +void insert_sock_recv_from_data(uint32_t sock, iovec_app_t *ri_data, uint32 ri_data_len, uint16_t ri_flags, + __wasi_addr_t *src_addr, uint32 *ro_data_len); 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 be9bd44..e827076 100644 --- a/include/wamr_wasi_context.h +++ b/include/wamr_wasi_context.h @@ -6,8 +6,10 @@ #define MVVM_WAMR_WASI_CONTEXT_H #include "logging.h" +#include "wamr_export.h" #include "wamr_serializer.h" #include "wasm_runtime.h" +#include "wasmtime_ssp.h" #include #include #include @@ -28,8 +30,47 @@ struct WAMRAddrPool { bool is_4; uint8 mask; }; +struct WAMRWasiAddr { + WAMRAddrPool ip; + uint16 port; +}; +struct WasiSockOpenData { + uint32 poolfd; + int af; + int socktype; + uint32 sockfd; +}; + +struct WasiSockSendToData { + uint32 sock; + iovec_app_t si_data; + uint32 si_data_len; + uint16_t si_flags; + WAMRWasiAddr dest_addr; + uint32 so_data_len; +}; + +struct WasiSockRecvFromData { + uint32_t sock; + iovec_app_t ri_data; + uint32 ri_data_len; + uint16_t ri_flags; + WAMRWasiAddr src_addr; + uint32 ro_data_len; +}; + +struct SocketMetaData { + int domain{}; + int type{}; + int protocol{}; + SocketAddrPool socketAddress{}; + WasiSockOpenData socketOpenData{}; + WasiSockSendToData socketSentToData{}; + WasiSockRecvFromData socketRecvFromData{}; +}; struct WAMRWASIContext { std::map> fd_map; + std::map socket_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 972bf93..4f1c282 160000 --- a/lib/wasm-micro-runtime +++ b/lib/wasm-micro-runtime @@ -1 +1 @@ -Subproject commit 972bf938f6bbd23350c6f301257b4733c05dd1ac +Subproject commit 4f1c282b18244ff7679cc0ac4f1fb67909c56c08 diff --git a/src/wamr.cpp b/src/wamr.cpp index 436e696..86fb0d1 100644 --- a/src/wamr.cpp +++ b/src/wamr.cpp @@ -144,7 +144,8 @@ int WAMRInstance::invoke_frenumber(uint32 fd, uint32 to) { uint32 argv[2] = {fd, to}; return wasm_runtime_call_wasm(exec_env, func, 2, argv); }; -int WAMRInstance::invoke_sock_open(uint32_t poolfd, int af, int socktype, uint32_t* sockfd) { + +int WAMRInstance::invoke_sock_open(uint32_t poolfd, int af, int socktype, uint32_t *sockfd) { auto name = "__wasi_sock_open"; if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { LOGV(ERROR) << "The wasi fopen function is not found."; @@ -174,17 +175,17 @@ int WAMRInstance::invoke_sock_open(uint32_t poolfd, int af, int socktype, uint32 buffer_for_wasm = wasm_runtime_module_malloc(module_inst, sizeof(uint32), reinterpret_cast(&buffer_)); if (buffer_for_wasm != 0) { - memcpy(buffer_, ((void*)sockfd), sizeof(uint32)); // use native address for accessing in runtime - uint32 argv[4] = {poolfd, static_cast(af), static_cast(socktype),buffer_for_wasm}; + memcpy(buffer_, ((void *)sockfd), sizeof(uint32)); // use native address for accessing in runtime + uint32 argv[4] = {poolfd, static_cast(af), static_cast(socktype), buffer_for_wasm}; auto res = wasm_runtime_call_wasm(exec_env, func, 4, argv); wasm_runtime_module_free(module_inst, buffer_for_wasm); return res; } return -1; - } -int WAMRInstance::invoke_sock_sendto(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) { +int WAMRInstance::invoke_sock_sendto(uint32_t sock, const iovec_app_t *si_data, uint32 si_data_len, uint16_t si_flags, + const __wasi_addr_t *dest_addr, uint32 *so_data_len) { auto name = "__wasi_sock_send_to"; if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { LOGV(ERROR) << "The wasi fopen function is not found."; @@ -219,21 +220,22 @@ int WAMRInstance::invoke_sock_sendto(uint32_t sock, const iovec_app1_t* si_data, int res = -1; si_data_for_wasm = - wasm_runtime_module_malloc(module_inst, sizeof(iovec_app1_t), reinterpret_cast(&si_data_)); + wasm_runtime_module_malloc(module_inst, sizeof(iovec_app_t), reinterpret_cast(&si_data_)); if (si_data_for_wasm != 0) { - memcpy(si_data_, ((void *)si_data), sizeof(iovec_app1_t)); // use native address for accessing in runtime + memcpy(si_data_, ((void *)si_data), sizeof(iovec_app_t)); // use native address for accessing in runtime dest_addr_for_wasm = wasm_runtime_module_malloc(module_inst, sizeof(__wasi_addr_t), reinterpret_cast(&dest_addr_)); if (dest_addr_for_wasm != 0) { - memcpy(dest_addr_, ((void *)dest_addr), sizeof(__wasi_addr_t)); // use native address for accessing in runtime + memcpy(dest_addr_, ((void *)dest_addr), + sizeof(__wasi_addr_t)); // use native address for accessing in runtime so_data_len_for_wasm = wasm_runtime_module_malloc(module_inst, sizeof(uint32), reinterpret_cast(&so_data_len_)); if (so_data_len_for_wasm != 0) { memcpy(so_data_len_, ((void *)so_data_len), sizeof(uint32)); // use native address for accessing in runtime - uint32 argv[6] = {sock, si_data_for_wasm, si_data_len, + uint32 argv[6] = {sock, si_data_for_wasm, si_data_len, si_flags, dest_addr_for_wasm, so_data_len_for_wasm}; wasm_runtime_call_wasm(exec_env, func, 6, argv); @@ -248,7 +250,8 @@ int WAMRInstance::invoke_sock_sendto(uint32_t sock, const iovec_app1_t* si_data, return -1; } -int WAMRInstance::invoke_sock_recvfrom(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) { +int WAMRInstance::invoke_sock_recvfrom(uint32_t sock, iovec_app_t *ri_data, uint32 ri_data_len, uint16_t ri_flags, + __wasi_addr_t *src_addr, uint32 *ro_data_len) { auto name = "__wasi_sock_recv_from"; if (!(func = wasm_runtime_lookup_function(module_inst, name, nullptr))) { LOGV(ERROR) << "The wasi fopen function is not found."; @@ -283,9 +286,9 @@ int WAMRInstance::invoke_sock_recvfrom(uint32_t sock, iovec_app1_t* ri_data, uin int res = -1; ri_data_for_wasm = - wasm_runtime_module_malloc(module_inst, sizeof(iovec_app1_t), reinterpret_cast(&ri_data_)); + wasm_runtime_module_malloc(module_inst, sizeof(iovec_app_t), reinterpret_cast(&ri_data_)); if (ri_data_for_wasm != 0) { - memcpy(ri_data_, ((void *)ri_data), sizeof(iovec_app1_t)); // use native address for accessing in runtime + memcpy(ri_data_, ((void *)ri_data), sizeof(iovec_app_t)); // use native address for accessing in runtime src_addr_for_wasm = wasm_runtime_module_malloc(module_inst, sizeof(__wasi_addr_t), reinterpret_cast(&src_addr_)); diff --git a/src/wamr_export.cpp b/src/wamr_export.cpp index 54d02d2..1093313 100644 --- a/src/wamr_export.cpp +++ b/src/wamr_export.cpp @@ -2,48 +2,69 @@ // Created by victoryang00 on 10/19/23. // -#include "wamr_block_addr.h" #include "wamr.h" +#include "wamr_block_addr.h" +#include "wamr_wasi_context.h" extern WAMRInstance *wamr; +void insert_sock_open_data(uint32_t poolfd, int af, int socktype, uint32_t sockfd) { + SocketMetaData newSocketData{}; + newSocketData = wamr->socket_fd_map_[sockfd]; -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"; - } -} + WasiSockOpenData openData; + openData.poolfd = poolfd; + openData.af = af; + openData.socktype = socktype; + openData.sockfd = 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) { - 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"; - } + newSocketData.socketOpenData = openData; + wamr->socket_fd_map_.insert(std::make_pair(sockfd, newSocketData)); } -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"; - } +void insert_sock_send_to_data(uint32_t sock, const iovec_app_t *si_data, uint32 si_data_len, uint16_t si_flags, + const __wasi_addr_t *dest_addr, uint32 *so_data_len) { + SocketMetaData newSocketData{}; + newSocketData = wamr->socket_fd_map_[sock]; + + // WasiSockSendToData sentToData{}; + // sentToData.sock = sock; + // std::memcpy(&sentToData.si_data, si_data, sizeof(iovec_app_t)); + // sentToData.si_data_len = si_data_len; + // sentToData.si_flags = si_flags; + // // sentToData.dest_addr = dest_addr; //翻译 + // if (dest_addr) { + // if (dest_addr->kind == 0) { // ip4 + // sentToData.dest_addr.ip = WAMRAddrPool { + // .ip4 = {dest_addr->addr[0], dest_addr->addr[1], dest_addr->addr[2], + // dest_addr->addr[3]}, + // .is_4 = true, + // .mask = 0, + // }; + // } else { // ip6 + // } + // } + // sentToData.so_data_len = *so_data_len; + + // newSocketData.socketSentToData = sentToData; + // wamr->socket_fd_map_.insert(std::make_pair(sock, newSocketData)); } +void insert_sock_recv_from_data(uint32_t sock, iovec_app_t *ri_data, uint32 ri_data_len, uint16_t ri_flags, + __wasi_addr_t *src_addr, uint32 *ro_data_len) { + // SocketMetaData newSocketData{}; + // newSocketData = wamr->socket_fd_map_[sock]; + + // WasiSockRecvFromData recvFromData{}; + // recvFromData.sock = sock; + // recvFromData.ri_data = ri_data; + // recvFromData.ri_data_len = ri_data_len; + // recvFromData.ri_flags = ri_flags; + // recvFromData.src_addr = src_addr; + // recvFromData.ro_data_len = ro_data_len; + + // newSocketData.socketSentToData = recvFromData; + // wamr->socket_fd_map_.insert(std::make_pair(sock, newSocketData)); +} /**fopen, fseek*/ void insert_fd(int fd, const char *path, int flags, int offset) { @@ -82,4 +103,55 @@ 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; +} diff --git a/src/wamr_wasi_context.cpp b/src/wamr_wasi_context.cpp index b5cffda..1470da6 100644 --- a/src/wamr_wasi_context.cpp +++ b/src/wamr_wasi_context.cpp @@ -18,6 +18,11 @@ void WAMRWASIContext::dump_impl(WASIContext *env) { auto dumped_res = std::make_tuple(path, flags, offset); this->fd_map[fd] = dumped_res; } + + for (auto [fd, socketMetaData] : wamr->socket_fd_map_) { + SocketMetaData socketMetaDataCopy = socketMetaData; + this->socket_fd_map[fd] = socketMetaDataCopy; + } } void WAMRWASIContext::restore_impl(WASIContext *env) { int r; @@ -35,13 +40,24 @@ void WAMRWASIContext::restore_impl(WASIContext *env) { if (r != fd) wamr->invoke_frenumber(r, fd); wamr->invoke_fseek(fd, std::get<2>(res)); - wamr->invoke_sock_open(wamr->sock_open_data.poolfd, wamr->sock_open_data.af, wamr->sock_open_data.socktype, - wamr->sock_open_data.sockfd); - wamr->invoke_sock_sendto(wamr->sock_sendto_data.sock, wamr->sock_sendto_data.si_data, - wamr->sock_sendto_data.si_data_len, wamr->sock_sendto_data.si_flags, - wamr->sock_sendto_data.dest_addr, wamr->sock_sendto_data.so_data_len); - wamr->invoke_sock_recvfrom(wamr->sock_recvfrom_data.sock, wamr->sock_recvfrom_data.ri_data, - wamr->sock_recvfrom_data.ri_data_len, wamr->sock_recvfrom_data.ri_flags, - wamr->sock_recvfrom_data.src_addr, wamr->sock_recvfrom_data.ro_data_len); + } + + for (auto [fd, socketMetaData] : this->socket_fd_map) { + LOGV(INFO) << "fd: " << fd << " SocketMetaData[domain]: " << socketMetaData.domain + // << " SocketMetaData[socketAddress]: " << socketMetaData.socketAddress + << " SocketMetaData[protocol]: " << socketMetaData.protocol + << " SocketMetaData[type]: " << socketMetaData.type; + uint32 tmp_sock_fd; + wamr->invoke_sock_open(socketMetaData.socketOpenData.poolfd, socketMetaData.socketOpenData.af, + socketMetaData.socketOpenData.socktype, &tmp_sock_fd); + // if socket不一样 renumber + // wamr->invoke_sock_sendto(socketMetaData.socketSentToData.sock, &socketMetaData.socketSentToData.si_data, + // socketMetaData.socketSentToData.si_data_len, socketMetaData.socketSentToData.si_flags, + // socketMetaData.socketSentToData.dest_addr,//翻译 + // socketMetaData.socketSentToData.so_data_len); + // wamr->invoke_sock_recvfrom( + // socketMetaData.socketRecvFromData.sock, socketMetaData.socketRecvFromData.ri_data, + // socketMetaData.socketRecvFromData.ri_data_len, socketMetaData.socketRecvFromData.ri_flags, + // &socketMetaData.socketRecvFromData.src_addr, socketMetaData.socketRecvFromData.ro_data_len); } }; \ No newline at end of file