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

Commit 37105d3

Browse files
committed
WIP: add "hello world" wasm test
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]>
1 parent 175c6dd commit 37105d3

15 files changed

+132
-43
lines changed

Cargo.lock

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ vdso = "0.1"
5555

5656
[build-dependencies]
5757
cc = "1.0"
58+
wat = "1.0"
5859
walkdir = "2"
5960
protobuf-codegen-pure = "2.25"
6061
sallyport = { git = "https://github.com/enarx/sallyport", rev = "a567a22665c7e5ba88a8c4acd64ab43ee32b4681", features = [ "asm" ] }

build.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ fn build_cc_tests(in_path: &Path, out_path: &Path) {
107107
}
108108
}
109109

110+
use wat;
111+
112+
fn build_wasm_tests(in_path: &Path, out_path: &Path) {
113+
for wat in find_files_with_extensions(&["wat"], &in_path) {
114+
let wasm = out_path.join(wat.file_stem().unwrap()).with_extension("wasm");
115+
let bin = wat::parse_file(&wat)
116+
.unwrap_or_else(|_| panic!("failed to compile {:?}", &wat));
117+
std::fs::write(&wasm, &bin)
118+
.unwrap_or_else(|_| panic!("failed to write {:?}", &wasm));
119+
println!("cargo:rerun-if-changed={}", &wat.display());
120+
}
121+
}
122+
110123
// Build a binary named `bin_name` from the crate located at `in_dir`,
111124
// targeting `target_name`, then strip the resulting binary and place it
112125
// at `out_dir`/bin/`bin_name`.
@@ -235,6 +248,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
235248

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

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

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

260-
#[cfg(feature = "backend-kvm")]
261-
"shim-sev" => cargo_build_bin(&path, &out_dir, target, "shim-sev")?,
274+
//#[cfg(feature = "backend-kvm")]
275+
//"shim-sev" => cargo_build_bin(&path, &out_dir, target, "shim-sev")?,
262276

263277
#[cfg(feature = "backend-sgx")]
264278
"shim-sgx" => cargo_build_bin(&path, &out_dir, target, "shim-sgx")?,

internal/wasmldr/build.rs

-26
This file was deleted.

internal/wasmldr/fixtures/bundle/config.yaml

-8
This file was deleted.

internal/wasmldr/fixtures/bundle/stdin.txt

-1
This file was deleted.

rust-toolchain

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nightly-2021-09-30

src/main.rs

+13
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ struct Info {}
8181
struct Exec {
8282
/// The payload to run inside the keep
8383
code: Option<PathBuf>,
84+
85+
/*
86+
/// Provide a "shim" binary to set up the keep
87+
#[structopt(long)]
88+
shim: Option<PathBuf>,
89+
90+
/// Provide a "workldr" binary to load & execute the workload
91+
#[structopt(long)]
92+
workldr: Option<PathBuf>,
93+
94+
/// Path to the workload to execute inside the keep
95+
workload: Option<PathBuf>,
96+
*/
8497
}
8598

8699
#[derive(StructOpt)]

tests/common.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub const CRATE: &str = env!("CARGO_MANIFEST_DIR");
2+
pub const KEEP_BIN: &str = env!("CARGO_BIN_EXE_enarx-keepldr");
3+
pub const OUT_DIR: &str = env!("OUT_DIR");
4+
pub const TEST_BINS_OUT: &str = "bin";
5+
pub const TIMEOUT_SECS: u64 = 10;
6+
pub const MAX_ASSERT_ELEMENTS: usize = 100;

tests/integration_tests.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ use serial_test::serial;
1717
use std::sync::Arc;
1818
use tempdir::TempDir;
1919

20-
const CRATE: &str = env!("CARGO_MANIFEST_DIR");
21-
const KEEP_BIN: &str = env!("CARGO_BIN_EXE_enarx-keepldr");
22-
const OUT_DIR: &str = env!("OUT_DIR");
23-
const TEST_BINS_OUT: &str = "bin";
24-
const TIMEOUT_SECS: u64 = 10;
25-
const MAX_ASSERT_ELEMENTS: usize = 100;
20+
mod common;
21+
use common::*;
2622

2723
fn assert_eq_slices(expected_output: &[u8], output: &[u8], what: &str) {
2824
let max_len = usize::min(output.len(), expected_output.len());
File renamed without changes.
File renamed without changes.

tests/wasmldr_tests.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
use std::fs::File;
4+
use std::path::Path;
5+
use std::process::{Command, Stdio};
6+
//use std::os::unix::process::CommandExt;
7+
use std::os::unix::io::{RawFd, FromRawFd, IntoRawFd};
8+
use std::io;
9+
use std::time::Duration;
10+
use process_control::{ChildExt, Output, Timeout};
11+
12+
extern crate libc;
13+
use libc::c_int;
14+
15+
mod common;
16+
use common::{CRATE, OUT_DIR, TEST_BINS_OUT, KEEP_BIN, TIMEOUT_SECS};
17+
18+
use serial_test::serial;
19+
//use anyhow::Result;
20+
21+
const MODULE_FD: RawFd = 3;
22+
23+
// wrap a libc call to return io::Result
24+
fn cvt(rv: c_int) -> io::Result<c_int> {
25+
if rv == -1 { Err(io::Error::last_os_error()) } else { Ok(rv) }
26+
}
27+
28+
fn open_module(path: &Path) -> io::Result<File> {
29+
let mut file = File::open(path)?;
30+
let fd = file.into_raw_fd();
31+
unsafe {
32+
file = File::from_raw_fd(cvt(libc::dup2(fd, MODULE_FD))?)
33+
}
34+
Ok(file)
35+
}
36+
37+
fn run_test(wasm: &str) -> Output {
38+
let path = Path::new(CRATE).join(OUT_DIR).join(TEST_BINS_OUT).join(wasm);
39+
let _fd3 = open_module(&path).unwrap();
40+
let child = Command::new(KEEP_BIN)
41+
.arg("exec")
42+
.stdin(Stdio::piped())
43+
.stdout(Stdio::piped())
44+
.stderr(Stdio::piped())
45+
.spawn()
46+
.unwrap_or_else(|e| panic!("failed to run `{:?}`: {:#?}", wasm, e));
47+
let output = child
48+
.with_output_timeout(Duration::from_secs(TIMEOUT_SECS))
49+
.terminating()
50+
.wait()
51+
.unwrap_or_else(|e| panic!("failed to run `{}`: {:#?}", wasm, e))
52+
.unwrap_or_else(|| panic!("process `{}` timed out", wasm));
53+
54+
let _exit_status = output.status.code().unwrap_or_else(|| {
55+
panic!("process `{}` terminated by signal {:?}", wasm, output.status.signal())
56+
});
57+
58+
// TODO: compare output.stdout, output.stderr, etc.
59+
60+
output
61+
}
62+
63+
#[test]
64+
#[serial]
65+
fn hello_wasi_snapshot1() {
66+
let out = run_test("hello_wasi_snapshot1.wasm");
67+
assert_eq!(out.stdout, &b"Hello, world!\n"[..]);
68+
}

0 commit comments

Comments
 (0)