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

Mount current directory to /home and Set CWD to /home when running WASI #4730

Merged
merged 25 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f25b549
mount current dir to /home and set CWD to /home for WASI process
maminrayej May 22, 2024
f6e6de0
conditionally set cwd to /home
maminrayej May 22, 2024
41121ce
fix wasi-fyi expected output
maminrayej May 22, 2024
f9d6ce1
factor out MAPPED_CURRENT_DIR_DEFAULT_PATH
maminrayej May 22, 2024
5c24168
fix lint
maminrayej May 22, 2024
265191a
only mount home when --dir is present
maminrayej May 22, 2024
6aee08d
adjust wasi-fyi expected output
maminrayej May 22, 2024
5b5b9bb
fix issue with local wasm files
maminrayej May 30, 2024
7895fab
add wasix tests
maminrayej May 30, 2024
51c97a4
temp: disable wasmer-config jobs
maminrayej May 30, 2024
887ca94
switch to root before some steps
maminrayej May 30, 2024
f2c93c9
switch to home before some steps
maminrayej May 30, 2024
5db91c7
expose WASMER to tests
maminrayej May 30, 2024
af2a682
add return status
maminrayej May 30, 2024
af85574
fail on purpose
maminrayej May 30, 2024
29b942d
fail on purpose
maminrayej May 30, 2024
985fddd
fix the wasix test
maminrayej May 30, 2024
debe493
bring back jobs and make them required
maminrayej May 30, 2024
01b3ddd
fail on purpose
maminrayej May 30, 2024
8d37bdd
fix test and bring back jobs
maminrayej May 30, 2024
df8032e
test more code paths
maminrayej May 30, 2024
d71a26c
undo unnecessary comment
maminrayej May 30, 2024
eaab7d6
remove all system go job
maminrayej May 30, 2024
d47a69f
include wasmer.toml files in tests folder
maminrayej May 30, 2024
d202535
make the build step common among tests
maminrayej May 31, 2024
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
36 changes: 35 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ env:
MSRV: "1.74"
NEXTEST_PROFILE: "ci"
RUSTUP_WINDOWS_PATH_ADD_BIN: 1
WASI_SDK_VERSION: "22"

jobs:

setup:
name: Set up
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -144,6 +144,40 @@ jobs:
# run: |
# make test-js-core

test_wasix:
name: Test WASIX
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ env.MSRV }}
- name: Install Tools
run: |
sudo apt-get update
sudo apt-get install -y git llvm clang make lld curl
- name: Build wasix sysroot
run: |
cd ~
git clone --recurse-submodules https://github.com/wasix-org/wasix-libc
cd wasix-libc
./build32.sh
rm -rf /opt/wasix-sysroot
cp -r sysroot32 ~/wasix-sysroot
- name: Install wasi-sdk Tools
run: |
cd ~
curl -L "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${{ env.WASI_SDK_VERSION }}/wasi-sdk-${{ env.WASI_SDK_VERSION }}.0-linux.tar.gz" -o wasi-sdk.tar.gz
tar -xzf wasi-sdk.tar.gz
cp -r wasi-sdk-${{ env.WASI_SDK_VERSION }}.0 ~/wasi-sdk
- name: Install wasm-opt
run: |
sudo apt-get install -y binaryen
- name: make test-wasix
run: |
WASI_SDK=~/wasi-sdk WASIX_SYSROOT=~/wasix-sysroot make test-wasix

test_wasm_build:
name: Test wasm build
runs-on: ubuntu-22.04
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ test-wasi-fyi: build-wasmer
cd tests/wasi-fyi; \
./test.sh

test-wasix: build-wasmer
cd tests/wasix; \
./test.sh

test-integration-cli: build-wasmer build-capi package-capi-headless package distribution
cp ./dist/wasmer.tar.gz ./link.tar.gz
rustup target add wasm32-wasi
Expand Down
5 changes: 4 additions & 1 deletion lib/cli/src/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,15 @@ impl Run {

let mut runner = WasiRunner::new();

let (is_home_mapped, mapped_diretories) = self.wasi.build_mapped_directories()?;

runner
.with_args(&self.args)
.with_injected_packages(packages)
.with_envs(self.wasi.env_vars.clone())
.with_mapped_host_commands(self.wasi.build_mapped_commands()?)
.with_mapped_directories(self.wasi.build_mapped_directories()?)
.with_mapped_directories(mapped_diretories)
.with_home_mapped(is_home_mapped)
.with_forward_host_env(self.wasi.forward_host_env)
.with_capabilities(self.wasi.capabilities());

Expand Down
17 changes: 8 additions & 9 deletions lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use wasmer_wasix::{
journal::{CompactingLogFileJournal, DynJournal},
os::{tty_sys::SysTty, TtyBridge},
rewind_ext,
runners::MAPPED_CURRENT_DIR_DEFAULT_PATH,
runners::{MappedCommand, MappedDirectory},
runtime::{
module_cache::{FileSystemCache, ModuleCache},
Expand Down Expand Up @@ -180,8 +181,6 @@ pub struct RunProperties {

#[allow(dead_code)]
impl Wasi {
const MAPPED_CURRENT_DIR_DEFAULT_PATH: &'static str = "/mnt/host";

pub fn map_dir(&mut self, alias: &str, target_on_disk: PathBuf) {
self.mapped_dirs.push(MappedDirectory {
guest: alias.to_string(),
Expand Down Expand Up @@ -269,7 +268,7 @@ impl Wasi {

MappedDirectory {
host: current_dir,
guest: Self::MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(),
guest: MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(),
}
} else {
let resolved = dir.canonicalize().with_context(|| {
Expand Down Expand Up @@ -322,7 +321,7 @@ impl Wasi {

MappedDirectory {
host: resolved_host,
guest: Self::MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(),
guest: MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(),
}
} else {
MappedDirectory {
Expand Down Expand Up @@ -353,7 +352,7 @@ impl Wasi {
.unwrap();

if have_current_dir {
b.map_dir(".", Self::MAPPED_CURRENT_DIR_DEFAULT_PATH)?
b.map_dir(".", MAPPED_CURRENT_DIR_DEFAULT_PATH)?
} else {
b.map_dir(".", "/")?
}
Expand Down Expand Up @@ -402,7 +401,7 @@ impl Wasi {
Ok(Vec::new())
}

pub fn build_mapped_directories(&self) -> Result<Vec<MappedDirectory>, anyhow::Error> {
pub fn build_mapped_directories(&self) -> Result<(bool, Vec<MappedDirectory>), anyhow::Error> {
let mut mapped_dirs = Vec::new();

// Process the --dirs flag and merge it with --mapdir.
Expand All @@ -419,7 +418,7 @@ impl Wasi {

MappedDirectory {
host: current_dir,
guest: Self::MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(),
guest: MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(),
}
} else {
let resolved = dir.canonicalize().with_context(|| {
Expand Down Expand Up @@ -472,7 +471,7 @@ impl Wasi {

MappedDirectory {
host: resolved_host,
guest: Self::MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(),
guest: MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(),
}
} else {
MappedDirectory {
Expand All @@ -483,7 +482,7 @@ impl Wasi {
mapped_dirs.push(mapping);
}

Ok(mapped_dirs)
Ok((have_current_dir, mapped_dirs))
}

pub fn build_mapped_commands(&self) -> Result<Vec<MappedCommand>, anyhow::Error> {
Expand Down
4 changes: 3 additions & 1 deletion lib/wasix/src/runners/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ pub mod wcgi;

pub use self::{
runner::Runner,
wasi_common::{MappedCommand, MappedDirectory, MountedDirectory},
wasi_common::{
MappedCommand, MappedDirectory, MountedDirectory, MAPPED_CURRENT_DIR_DEFAULT_PATH,
},
};
11 changes: 10 additions & 1 deletion lib/wasix/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
};
use wasmer_types::ModuleHash;

use super::wasi_common::MappedCommand;
use super::wasi_common::{MappedCommand, MAPPED_CURRENT_DIR_DEFAULT_PATH};

#[derive(Debug, Default, Clone)]
pub struct WasiRunner {
Expand Down Expand Up @@ -80,6 +80,11 @@ impl WasiRunner {
self.with_mounted_directories(dirs.into_iter().map(Into::into).map(MountedDirectory::from))
}

pub fn with_home_mapped(&mut self, is_home_mapped: bool) -> &mut Self {
self.wasi.is_home_mapped = is_home_mapped;
self
}

pub fn with_mounted_directories<I, D>(&mut self, dirs: I) -> &mut Self
where
I: IntoIterator<Item = D>,
Expand Down Expand Up @@ -250,6 +255,10 @@ impl WasiRunner {
builder.set_stderr(Box::new(stderr.clone()));
}

if self.wasi.is_home_mapped {
builder.set_current_dir(MAPPED_CURRENT_DIR_DEFAULT_PATH);
}

Ok(builder)
}

Expand Down
3 changes: 3 additions & 0 deletions lib/wasix/src/runners/wasi_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::{
WasiEnvBuilder,
};

pub const MAPPED_CURRENT_DIR_DEFAULT_PATH: &str = "/home";

#[derive(Debug, Clone)]
pub struct MappedCommand {
/// The new alias.
Expand All @@ -34,6 +36,7 @@ pub(crate) struct CommonWasiOptions {
pub(crate) forward_host_env: bool,
pub(crate) mapped_host_commands: Vec<MappedCommand>,
pub(crate) mounts: Vec<MountedDirectory>,
pub(crate) is_home_mapped: bool,
pub(crate) injected_packages: Vec<BinaryPackage>,
pub(crate) capabilities: Capabilities,
#[derivative(Debug = "ignore")]
Expand Down
3 changes: 3 additions & 0 deletions tests/wasix/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output*
*.webc
*.wasm
1 change: 1 addition & 0 deletions tests/wasix/cwd-to-home/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Empty file.
10 changes: 10 additions & 0 deletions tests/wasix/cwd-to-home/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <fcntl.h>

int main()
{
int fd = open("hello.txt", O_RDWR);
printf("%d", (fd == -1));

return 0;
}
5 changes: 5 additions & 0 deletions tests/wasix/cwd-to-home/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

$WASMER -q run main.wasm --dir=. > output

diff -u output expected 1>/dev/null
90 changes: 90 additions & 0 deletions tests/wasix/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash

if [[ -z "${WASI_SDK}" ]]; then
echo "WASI_SDK is not found"
exit 1
fi

if [[ -z "${WASIX_SYSROOT}" ]]; then
echo "WASIX_SYSROOT is not found"
exit 1
fi

export RANLIB="$WASI_SDK/bin/ranlib"
export AR="$WASI_SDK/bin/ar"
export NM="$WASI_SDK/bin/nm"
export CC="$WASI_SDK/bin/clang"
export CXX="$WASI_SDK/bin/clang"
export CFLAGS="\
--sysroot=$WASIX_SYSROOT \
-matomics \
-mbulk-memory \
-mmutable-globals \
-pthread \
-mthread-model posix \
-ftls-model=local-exec \
-fno-trapping-math \
-D_WASI_EMULATED_MMAN \
-D_WASI_EMULATED_SIGNAL \
-D_WASI_EMULATED_PROCESS_CLOCKS \
-O3 \
-g \
-flto"
export LD="$WASI_SDK/bin/wasm-ld"
export LDFLAGS="\
-Wl,--shared-memory \
-Wl,--max-memory=4294967296 \
-Wl,--import-memory \
-Wl,--export-dynamic \
-Wl,--export=__heap_base \
-Wl,--export=__stack_pointer \
-Wl,--export=__data_end \
-Wl,--export=__wasm_init_tls \
-Wl,--export=__wasm_signal \
-Wl,--export=__tls_size \
-Wl,--export=__tls_align \
-Wl,--export=__tls_base \
-lwasi-emulated-mman \
-O3 \
-g \
-flto"
export LIBS="\
-Wl,--shared-memory \
-Wl,--max-memory=4294967296 \
-Wl,--import-memory \
-Wl,--export-dynamic \
-Wl,--export=__heap_base \
-Wl,--export=__stack_pointer \
-Wl,--export=__data_end \
-Wl,--export=__wasm_init_tls \
-Wl,--export=__wasm_signal \
-Wl,--export=__tls_size \
-Wl,--export=__tls_align \
-Wl,--export=__tls_base \
-lwasi-emulated-mman \
-O3 \
-g \
-flto"

export WASMER=$(realpath "../../target/release/wasmer")

printf "\n\nStarting WASIX Test Suite:\n"

status=0
while read dir; do
dir=$(basename "$dir")
printf "Testing $dir..."

cmd="cd $dir; \
$CC $CFLAGS $LDFLAGS -o main.wasm main.c; \
./run.sh"

if bash -c "$cmd"; then
printf "\rTesting $dir ✅\n"
else
printf "\rTesting $dir ❌\n"
status=1
fi
done < <(find . -mindepth 1 -maxdepth 1 -type d)

exit $status
Loading