Skip to content

Commit

Permalink
update new
Browse files Browse the repository at this point in the history
  • Loading branch information
KikiSpace committed Oct 24, 2023
1 parent 9df086a commit 951dada
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 98 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
39 changes: 6 additions & 33 deletions include/wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand All @@ -54,10 +28,7 @@ class WAMRInstance {
std::vector<const char *> ns_pool_;
std::map<int, std::tuple<std::string, int, int>> fd_map_;
// add offset to pair->tuple, 3rd param 'int'

WasiSockOpenData sock_open_data;
WasiSockSendToData sock_sendto_data;
WasiSockRecvFromData sock_recvfrom_data;
std::map<uint32, SocketMetaData> socket_fd_map_;

bool is_jit;
char *buffer{};
Expand Down Expand Up @@ -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
29 changes: 20 additions & 9 deletions include/wamr_export.h
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
41 changes: 41 additions & 0 deletions include/wamr_wasi_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <atomic>
#include <filesystem>
#include <map>
Expand All @@ -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<int, std::tuple<std::string, int, int>> fd_map;
std::map<int, SocketMetaData> socket_fd_map;
std::vector<std::string> dir;
std::vector<std::string> map_dir;
WAMRArgvEnvironValues argv_environ;
Expand Down
2 changes: 1 addition & 1 deletion lib/wasm-micro-runtime
27 changes: 15 additions & 12 deletions src/wamr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down Expand Up @@ -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<void **>(&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<uint32>(af), static_cast<uint32>(socktype),buffer_for_wasm};
memcpy(buffer_, ((void *)sockfd), sizeof(uint32)); // use native address for accessing in runtime
uint32 argv[4] = {poolfd, static_cast<uint32>(af), static_cast<uint32>(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.";
Expand Down Expand Up @@ -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<void **>(&si_data_));
wasm_runtime_module_malloc(module_inst, sizeof(iovec_app_t), reinterpret_cast<void **>(&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<void **>(&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<void **>(&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);
Expand All @@ -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.";
Expand Down Expand Up @@ -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<void **>(&ri_data_));
wasm_runtime_module_malloc(module_inst, sizeof(iovec_app_t), reinterpret_cast<void **>(&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<void **>(&src_addr_));
Expand Down
Loading

0 comments on commit 951dada

Please sign in to comment.