Skip to content

Commit

Permalink
Merge pull request #7 from Multi-V-VM/main
Browse files Browse the repository at this point in the history
Merge main to bench
  • Loading branch information
victoryang00 authored Oct 25, 2023
2 parents 963e9f5 + 6e776a2 commit 105a518
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 133 deletions.
9 changes: 6 additions & 3 deletions include/wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tuple>

class WAMRInstance {
WASMExecEnv *exec_env{};
Expand All @@ -29,6 +30,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'
std::map<int, SocketMetaData> socket_fd_map_;
bool is_jit;
char *buffer{};
char error_buf[128]{};
Expand Down Expand Up @@ -56,7 +58,8 @@ class WAMRInstance {
const std::vector<std::string> &addr_list, const std::vector<std::string> &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();
};
Expand Down
8 changes: 6 additions & 2 deletions include/wamr_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion include/wamr_wasi_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <atomic>
Expand All @@ -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<uint32, std::pair<std::string,int>> 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
57 changes: 54 additions & 3 deletions src/checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "thread_manager.h"
#include "wamr.h"
#include "wamr_wasi_context.h"
#include "wasm_runtime.h"
#include <cstdio>
#include <cxxopts.hpp>
#include <fstream>
Expand Down Expand Up @@ -44,18 +46,67 @@ 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())
wamr->fd_map_.erase(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
Expand Down
4 changes: 3 additions & 1 deletion src/restore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "wamr.h"
#include "wamr_exec_env.h"
#include "wamr_read_write.h"
#include "wasm_runtime.h"
#include <cxxopts.hpp>
#include <iostream>
#include <memory>
Expand All @@ -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){};
Expand Down
17 changes: 16 additions & 1 deletion src/wamr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<void **>(&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))) {
Expand Down Expand Up @@ -224,6 +238,7 @@ void WAMRInstance::set_wasi_args(const std::vector<std::string> &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<std::string>(wasiContext.addr_pool.size());
Expand Down
124 changes: 3 additions & 121 deletions src/wamr_wasi_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)<<fmt::format("fd:{} path:{} option",fd,res.first);
}

#if 0
// Need to open the file and reinitialize the file descripter by map.
this->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<std::string>(env->argv_environ->argv_list, env->argv_environ->argv_list + env->argv_environ->argc);
this->argv_environ.env_list = std::vector<std::string>(
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;
Expand Down Expand Up @@ -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));
}
};
2 changes: 2 additions & 0 deletions test/read-file.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

// #include <cstdio>
#include <stdbool.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include <unistd.h>
Expand Down
3 changes: 3 additions & 0 deletions test/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 105a518

Please sign in to comment.