Skip to content
This repository has been archived by the owner on Oct 26, 2021. It is now read-only.

Commit

Permalink
WIP: add "hello world" wasm test
Browse files Browse the repository at this point in the history
This commit needs to get broken up but I want it to be public so.. here
we go!

Signed-off-by: Will Woods <[email protected]>
  • Loading branch information
wgwoods committed Oct 4, 2021
1 parent 175c6dd commit 37105d3
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 43 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ vdso = "0.1"

[build-dependencies]
cc = "1.0"
wat = "1.0"
walkdir = "2"
protobuf-codegen-pure = "2.25"
sallyport = { git = "https://github.com/enarx/sallyport", rev = "a567a22665c7e5ba88a8c4acd64ab43ee32b4681", features = [ "asm" ] }
Expand Down
18 changes: 16 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ fn build_cc_tests(in_path: &Path, out_path: &Path) {
}
}

use wat;

fn build_wasm_tests(in_path: &Path, out_path: &Path) {
for wat in find_files_with_extensions(&["wat"], &in_path) {
let wasm = out_path.join(wat.file_stem().unwrap()).with_extension("wasm");
let bin = wat::parse_file(&wat)
.unwrap_or_else(|_| panic!("failed to compile {:?}", &wat));
std::fs::write(&wasm, &bin)
.unwrap_or_else(|_| panic!("failed to write {:?}", &wasm));
println!("cargo:rerun-if-changed={}", &wat.display());
}
}

// Build a binary named `bin_name` from the crate located at `in_dir`,
// targeting `target_name`, then strip the resulting binary and place it
// at `out_dir`/bin/`bin_name`.
Expand Down Expand Up @@ -235,6 +248,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

build_cc_tests(&Path::new(CRATE).join(TEST_BINS_IN), &out_dir_bin);
build_rs_tests(&Path::new(CRATE).join(TEST_BINS_IN), &out_dir_bin);
build_wasm_tests(&Path::new(CRATE).join("tests/wasm"), &out_dir_bin);

let target = "x86_64-unknown-linux-musl";

Expand All @@ -257,8 +271,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "wasmldr")]
"wasmldr" => cargo_build_bin(&path, &out_dir, target, "wasmldr")?,

#[cfg(feature = "backend-kvm")]
"shim-sev" => cargo_build_bin(&path, &out_dir, target, "shim-sev")?,
//#[cfg(feature = "backend-kvm")]
//"shim-sev" => cargo_build_bin(&path, &out_dir, target, "shim-sev")?,

#[cfg(feature = "backend-sgx")]
"shim-sgx" => cargo_build_bin(&path, &out_dir, target, "shim-sgx")?,
Expand Down
26 changes: 0 additions & 26 deletions internal/wasmldr/build.rs

This file was deleted.

8 changes: 0 additions & 8 deletions internal/wasmldr/fixtures/bundle/config.yaml

This file was deleted.

1 change: 0 additions & 1 deletion internal/wasmldr/fixtures/bundle/stdin.txt

This file was deleted.

1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2021-09-30
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ struct Info {}
struct Exec {
/// The payload to run inside the keep
code: Option<PathBuf>,

/*
/// Provide a "shim" binary to set up the keep
#[structopt(long)]
shim: Option<PathBuf>,
/// Provide a "workldr" binary to load & execute the workload
#[structopt(long)]
workldr: Option<PathBuf>,
/// Path to the workload to execute inside the keep
workload: Option<PathBuf>,
*/
}

#[derive(StructOpt)]
Expand Down
6 changes: 6 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub const CRATE: &str = env!("CARGO_MANIFEST_DIR");
pub const KEEP_BIN: &str = env!("CARGO_BIN_EXE_enarx-keepldr");
pub const OUT_DIR: &str = env!("OUT_DIR");
pub const TEST_BINS_OUT: &str = "bin";
pub const TIMEOUT_SECS: u64 = 10;
pub const MAX_ASSERT_ELEMENTS: usize = 100;
8 changes: 2 additions & 6 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ use serial_test::serial;
use std::sync::Arc;
use tempdir::TempDir;

const CRATE: &str = env!("CARGO_MANIFEST_DIR");
const KEEP_BIN: &str = env!("CARGO_BIN_EXE_enarx-keepldr");
const OUT_DIR: &str = env!("OUT_DIR");
const TEST_BINS_OUT: &str = "bin";
const TIMEOUT_SECS: u64 = 10;
const MAX_ASSERT_ELEMENTS: usize = 100;
mod common;
use common::*;

fn assert_eq_slices(expected_output: &[u8], output: &[u8], what: &str) {
let max_len = usize::min(output.len(), expected_output.len());
Expand Down
File renamed without changes.
File renamed without changes.
68 changes: 68 additions & 0 deletions tests/wasmldr_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: Apache-2.0

use std::fs::File;
use std::path::Path;
use std::process::{Command, Stdio};
//use std::os::unix::process::CommandExt;
use std::os::unix::io::{RawFd, FromRawFd, IntoRawFd};
use std::io;
use std::time::Duration;
use process_control::{ChildExt, Output, Timeout};

extern crate libc;
use libc::c_int;

mod common;
use common::{CRATE, OUT_DIR, TEST_BINS_OUT, KEEP_BIN, TIMEOUT_SECS};

use serial_test::serial;
//use anyhow::Result;

const MODULE_FD: RawFd = 3;

// wrap a libc call to return io::Result
fn cvt(rv: c_int) -> io::Result<c_int> {
if rv == -1 { Err(io::Error::last_os_error()) } else { Ok(rv) }
}

fn open_module(path: &Path) -> io::Result<File> {
let mut file = File::open(path)?;
let fd = file.into_raw_fd();
unsafe {
file = File::from_raw_fd(cvt(libc::dup2(fd, MODULE_FD))?)
}
Ok(file)
}

fn run_test(wasm: &str) -> Output {
let path = Path::new(CRATE).join(OUT_DIR).join(TEST_BINS_OUT).join(wasm);
let _fd3 = open_module(&path).unwrap();
let child = Command::new(KEEP_BIN)
.arg("exec")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap_or_else(|e| panic!("failed to run `{:?}`: {:#?}", wasm, e));
let output = child
.with_output_timeout(Duration::from_secs(TIMEOUT_SECS))
.terminating()
.wait()
.unwrap_or_else(|e| panic!("failed to run `{}`: {:#?}", wasm, e))
.unwrap_or_else(|| panic!("process `{}` timed out", wasm));

let _exit_status = output.status.code().unwrap_or_else(|| {
panic!("process `{}` terminated by signal {:?}", wasm, output.status.signal())
});

// TODO: compare output.stdout, output.stderr, etc.

output
}

#[test]
#[serial]
fn hello_wasi_snapshot1() {
let out = run_test("hello_wasi_snapshot1.wasm");
assert_eq!(out.stdout, &b"Hello, world!\n"[..]);
}

0 comments on commit 37105d3

Please sign in to comment.