Skip to content

Commit

Permalink
fix path 5
Browse files Browse the repository at this point in the history
  • Loading branch information
victoryang00 committed Jun 28, 2023
1 parent 457433b commit 0982f0c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ jobs:
args: install make wget msys2

- name: Install Dependency
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
run: |
cd D:
git clone https://github.com/microsoft/vcpkg
cd vcpkg
./bootstrap-vcpkg.bat
./vcpkg install fmt:x64-windows cxxopts:x64-windows
echo "::add-path::C:/tools/msys64/usr/bin/"
pacman --noconfirm -S mingw-w64-x86_64-gcc
echo "C:/tools/msys64/usr/bin/" >> "$GITHUB_PATH"
C:\tools\msys64\usr\bin\pacman.exe --noconfirm -S mingw-w64-x86_64-gcc
- name: Install LLVM SDK
run: |
Expand All @@ -65,10 +63,8 @@ jobs:
echo D:/wasi-sdk/bin/wasm-ld.bak --no-check-features %1 | tee D:/wasi-sdk/bin/wasm-ld.bat
- name: configure cmake
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
run: |
echo "::add-path::C:/tools/msys64/usr/bin/"
echo "C:/tools/msys64/usr/bin/" >> "$GITHUB_PATH"
cmake -S $GITHUB_WORKSPACE -B ${{runner.workspace}}/build -DCMAKE_BUILD_TYPE=${{matrix.build}} -DCMAKE_TOOLCHAIN_FILE="D:/vcpkg/scripts/buildsystems/vcpkg.cmake" -DLLVM_DIR="D:/LLVM/share/llvm"
- name: build
Expand Down
3 changes: 2 additions & 1 deletion include/wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ 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,std::string path, uint32 option);
int invoke_open(uint32 fd,const std::string& path, uint32 option);
int invoke_preopen(uint32 fd,const std::string& path);
~WAMRInstance();
};
#endif // MVVM_WAMR_H
46 changes: 41 additions & 5 deletions src/wamr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,45 @@ int WAMRInstance::invoke_main() {

return wasm_runtime_call_wasm(exec_env, func, 0, nullptr);
}
int WAMRInstance::invoke_fopen(uint32 fd, std::string path, uint32 option) {
if (!(func = wasm_runtime_lookup_function(module_inst, "fopen", "(ii*~iIIi*)i"))) {
LOGV(ERROR) << "The wasi-open function is not found.";
int WAMRInstance::invoke_open(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;
}
return 0;
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];
strncpy(buffer_, path.c_str(), path.size()); // use native address for accessing in runtime
argv[0] = buffer_for_wasm; // pass the buffer_ address for WASM space
argv[1] = option; // the size of buffer_
auto res = wasm_runtime_call_wasm(exec_env, func, 2, argv);
wasm_runtime_module_free(module_inst, buffer_for_wasm);
return res;
}
return -1;
};
int WAMRInstance::invoke_preopen(uint32 fd, const std::string& path) {
if (!(func = wasm_runtime_lookup_function(module_inst, "__wasilibc_register_preopened_fd", "(i$)i"))) {
LOGV(ERROR) << "The __wasilibc_register_preopened_fd 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];
strncpy(buffer_, path.c_str(), path.size()); // use native address for accessing in runtime
argv[0] = fd; // pass the buffer_ address for WASM space
argv[1] = buffer_for_wasm; // the size of buffer_
auto res = wasm_runtime_call_wasm(exec_env, func, 2, argv);
wasm_runtime_module_free(module_inst, buffer_for_wasm);
return res;
}
return -1;
};
WASMExecEnv *WAMRInstance::get_exec_env() {
return cur_env; // should return the current thread's
Expand All @@ -97,7 +130,10 @@ void WAMRInstance::recover(
[](const std::unique_ptr<WAMRExecEnv> &a, const std::unique_ptr<WAMRExecEnv> &b) {
return a->cur_count > b->cur_count;
});

// need to preopen the stdio here.
invoke_preopen(0, "/dev/stdin");
invoke_preopen(1, "/dev/stdout");
invoke_preopen(2, "/dev/stderr");
for (auto &&exec_ : *execEnv) {
if (exec_->cur_count != 0) {
cur_env = wasm_cluster_spawn_exec_env(exec_env); // look into the pthread create wrapper how it worked.
Expand Down
2 changes: 1 addition & 1 deletion src/wamr_wasi_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@ void WAMRWASIContext::restore_impl(WASIContext *env) {
#endif
for (auto [fd, res] : this->fd_map) {
// differ from path from file
wamr->invoke_fopen(fd, res.first, res.second);
wamr->invoke_open(fd, res.first, res.second);
}
};

0 comments on commit 0982f0c

Please sign in to comment.