Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fd restore #8

Merged
merged 11 commits into from
Nov 4, 2023
Merged
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
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)
18 changes: 14 additions & 4 deletions include/wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include "bh_read_file.h"
#include "logging.h"
#include "wamr_exec_env.h"
#include "wamr_export.h"
#include "wamr_read_write.h"
#include "wasm_runtime.h"
#include "wamr_export.h"

class WAMRInstance {
WASMExecEnv *exec_env{};
Expand All @@ -26,8 +26,10 @@ class WAMRInstance {
std::vector<const char *> arg_;
std::vector<const char *> addr_;
std::vector<const char *> ns_pool_;
std::map<int, std::tuple<std::string,int, int>> fd_map_;
std::map<int, std::tuple<std::string, int, int>> fd_map_;
// add offset to pair->tuple, 3rd param 'int'
std::map<uint32, SocketMetaData> socket_fd_map_;

bool is_jit;
char *buffer{};
char error_buf[128]{};
Expand Down Expand Up @@ -57,9 +59,17 @@ class WAMRInstance {
const std::vector<std::string> &addr_list, const std::vector<std::string> &ns_lookup_pool);

int invoke_main();
int invoke_fopen(uint32 fd,const std::string& path, uint32 option);
int invoke_fopen(std::string &path, uint32 option);
int invoke_frenumber(uint32 fd, uint32 to);
int invoke_fseek(uint32 fd, uint32 offset);
int invoke_preopen(uint32 fd,const std::string& path);
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_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
25 changes: 22 additions & 3 deletions include/wamr_export.h
Original file line number Diff line number Diff line change
@@ -1,17 +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"

#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_app_t;
void serialize_to_file(struct WASMExecEnv *);

#endif
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
42 changes: 42 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,7 +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
78 changes: 26 additions & 52 deletions src/checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,25 @@

WAMRInstance *wamr = nullptr;
std::ostringstream re{};
auto writer = FwriteStream("test.bin");
FwriteStream *writer;
std::vector<std::unique_ptr<WAMRExecEnv>> as;
std::mutex as_mtx;
/**fopen, fseek*/
void insert_fd(int fd, const char *path, int flags, int offset) {
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::ifstream stdoutput;
std:
string current_str;
std::string fd_output;
std::string filename_output;
std::string flags_output;
if(stdoutput.is_open()) {
while(stdoutput.good()) {

if (stdoutput.is_open()) {
while (stdoutput.good()) {
stdoutput >> current_str;
if(current_str == "fopen_test(fd,filename,flags)") {
stdoutput >> fd_output;
if (current_str == "fopen_test(fd,filename,flags)") {
stdoutput >> fd_output;
stdoutput >> filename_output;
stdoutput >> flags_output;
insert_fd(std::stoi(fd_output), filename_output.c_str(), std::stoi(flags_output), 0);
Expand All @@ -79,7 +43,7 @@ void serialize_to_file(WASMExecEnv *instance) {
stdoutput.close();

//
std::cout<<"dasfasdfasf"<< re.str()<<"dasfasdfasf\n";
std::cout << "dasfasdfasf" << re.str() << "dasfasdfasf\n";
auto cluster = wasm_exec_env_get_cluster(instance);
if (bh_list_length(&cluster->exec_env_list) > 1) {
auto elem = (WASMExecEnv *)bh_list_first_elem(&cluster->exec_env_list);
Expand All @@ -98,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();
Expand All @@ -109,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);
Expand Down Expand Up @@ -154,8 +118,7 @@ int main(int argc, char *argv[]) {
options.add_options()("t,target", "The webassembly file to execute",
cxxopts::value<std::string>()->default_value("./test/counter.wasm"))(
"j,jit", "Whether the jit mode or interp mode", cxxopts::value<bool>()->default_value("false"))(
"d,dir", "The directory list exposed to WAMR",
cxxopts::value<std::vector<std::string>>()->default_value("./"))(
"d,dir", "The directory list exposed to WAMR", cxxopts::value<std::vector<std::string>>()->default_value("./"))(
"m,map_dir", "The mapped directory list exposed to WAMRe",
cxxopts::value<std::vector<std::string>>()->default_value(""))(
"e,env", "The environment list exposed to WAMR",
Expand All @@ -165,7 +128,18 @@ int main(int argc, char *argv[]) {
"n,ns_pool", "The ns lookup pool exposed to WAMR",
cxxopts::value<std::vector<std::string>>()->default_value(""))("h,help", "The value for epoch value",
cxxopts::value<bool>()->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<bool>()) {
std::cout << options.help() << std::endl;
Expand All @@ -179,10 +153,10 @@ int main(int argc, char *argv[]) {
auto arg = result["arg"].as<std::vector<std::string>>();
auto addr = result["addr"].as<std::vector<std::string>>();
auto ns_pool = result["ns_pool"].as<std::vector<std::string>>();
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
Expand Down
25 changes: 15 additions & 10 deletions src/restore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,38 @@
#include <memory>
#include <string>

auto reader = FreadStream("test.bin");
FreadStream *reader;
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 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",
cxxopts::value<std::string>()->default_value("./test/counter.wasm"))(
"j,jit", "Whether the jit mode or interp mode", cxxopts::value<bool>()->default_value("false"))(
"h,help", "The value for epoch value", cxxopts::value<bool>()->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<bool>()) {
std::cout << options.help() << std::endl;
exit(0);
}
auto target = result["target"].as<std::string>();
reader = new FreadStream((removeExtension(target) + ".bin").c_str());
wamr = new WAMRInstance(target.c_str(), false);
auto a = struct_pack::deserialize<std::vector<std::unique_ptr<WAMRExecEnv>>>(reader).value();
auto a = struct_pack::deserialize<std::vector<std::unique_ptr<WAMRExecEnv>>>(*reader).value();
wamr->instantiate();
wamr->recover(&a);
return 0;
Expand Down
Loading
Loading