Skip to content

Commit

Permalink
1. add read-file to output.txt
Browse files Browse the repository at this point in the history
2. move the record and replay to wamr_export
3. reader writer seperately
  • Loading branch information
KikiSpace committed Oct 19, 2023
1 parent 74725ec commit 9df086a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 101 deletions.
2 changes: 1 addition & 1 deletion lib/wasm-micro-runtime
98 changes: 16 additions & 82 deletions src/checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,91 +15,14 @@

WAMRInstance *wamr = nullptr;
std::ostringstream re{};
auto writer = FwriteStream("test.bin");
FwriteStream *writer;
std::vector<std::unique_ptr<WAMRExecEnv>> 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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -205,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 @@ -219,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
32 changes: 15 additions & 17 deletions src/restore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,38 @@
#include <memory>
#include <string>

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",
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
85 changes: 85 additions & 0 deletions src/wamr_export.cpp
Original file line number Diff line number Diff line change
@@ -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) {}
7 changes: 6 additions & 1 deletion test/read-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
#include <string.h>
#include <errno.h>

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)) {
Expand All @@ -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
Expand Down

0 comments on commit 9df086a

Please sign in to comment.