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

Commit 6d6c487

Browse files
committed
Add wasmldr integration test(s)
We can run wasm inside keeps now - let's prove it! Here's what this patch does: * Move wasm sources (*.wat) from internal/wasmldr/fixtures to tests/wasm * Copy .wat contents into wasmldr unittests and build inside the test * Remove wasmldr build.rs and update Cargo.toml * Build tests/wasm/*.wat to OUT_DIR/bin/*.wasm in build.rs * Add tests/wasmldr_tests.rs, which runs the .wasm test binaries The intent is that we should be adding more wasm tests over time; this gives us a place to put them and a harness to run them. The tricky bit was getting Rust to pass the module to the child process, since right now the only way we have to do that is to pass it on FD3 - but Rust doesn't have `dup2()` and it *really* wants to set FD_CLOEXEC on everything it opens, so we have to do some unsafe things to actually pass FDs to a child process. This isn't pretty, but it's a start. Signed-off-by: Will Woods <[email protected]>
1 parent bcfcfa7 commit 6d6c487

13 files changed

+257
-52
lines changed

Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ default = ["backend-kvm", "backend-sgx", "wasmldr"]
2727

2828
backend-kvm = ["x86_64", "kvm-bindings", "kvm-ioctls"]
2929
backend-sgx = ["x86_64", "sgx"]
30-
wasmldr = []
30+
wasmldr = ["wat"]
3131

3232
[dependencies]
3333
sgx = { git = "https://github.com/enarx/sgx", rev = "57df3753a0ea1777963dbf3023452993df2edb8c", features = ["openssl"], optional = true }
@@ -55,6 +55,7 @@ vdso = "0.1"
5555

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

build.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ fn build_cc_tests(in_path: &Path, out_path: &Path) {
107107
}
108108
}
109109

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

236248
build_cc_tests(&Path::new(CRATE).join(TEST_BINS_IN), &out_dir_bin);
237249
build_rs_tests(&Path::new(CRATE).join(TEST_BINS_IN), &out_dir_bin);
250+
#[cfg(feature = "wasmldr")]
251+
build_wasm_tests(&Path::new(CRATE).join("tests/wasm"), &out_dir_bin);
238252

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

internal/wasmldr/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ anyhow = "1.0"
2323
env_logger = { version = "0.9", default-features = false }
2424
log = "0.4"
2525

26-
[build-dependencies]
26+
[dev-dependencies]
2727
wat = "1.0"
2828

2929
[profile.release]

internal/wasmldr/build.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

internal/wasmldr/fixtures/bundle/config.yaml

Lines changed: 0 additions & 8 deletions
This file was deleted.

internal/wasmldr/fixtures/bundle/stdin.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/wasmldr/src/workload.rs

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,57 @@ pub(crate) mod test {
114114
use crate::workload;
115115
use std::iter::empty;
116116

117+
const NO_EXPORT_WAT: &'static str = r#"(module
118+
(memory (export "") 1)
119+
)"#;
120+
121+
const RETURN_1_WAT: &'static str = r#"(module
122+
(func (export "") (result i32) i32.const 1)
123+
)"#;
124+
125+
const WASI_COUNT_ARGS_WAT: &'static str = r#"(module
126+
(import "wasi_snapshot_preview1" "args_sizes_get"
127+
(func $__wasi_args_sizes_get (param i32 i32) (result i32)))
128+
(func (export "_start") (result i32)
129+
(i32.store (i32.const 0) (i32.const 0))
130+
(i32.store (i32.const 4) (i32.const 0))
131+
(call $__wasi_args_sizes_get (i32.const 0) (i32.const 4))
132+
drop
133+
(i32.load (i32.const 0))
134+
)
135+
(memory 1)
136+
(export "memory" (memory 0))
137+
)"#;
138+
139+
const HELLO_WASI_WAT: &'static str = r#"(module
140+
(import "wasi_snapshot_preview1" "proc_exit"
141+
(func $__wasi_proc_exit (param i32)))
142+
(import "wasi_snapshot_preview1" "fd_write"
143+
(func $__wasi_fd_write (param i32 i32 i32 i32) (result i32)))
144+
(func $_start
145+
(i32.store (i32.const 24) (i32.const 14))
146+
(i32.store (i32.const 20) (i32.const 0))
147+
(block
148+
(br_if 0
149+
(call $__wasi_fd_write
150+
(i32.const 1)
151+
(i32.const 20)
152+
(i32.const 1)
153+
(i32.const 16)))
154+
(br_if 0 (i32.ne (i32.load (i32.const 16)) (i32.const 14)))
155+
(br 1)
156+
)
157+
(call $__wasi_proc_exit (i32.const 1))
158+
)
159+
(memory 1)
160+
(export "memory" (memory 0))
161+
(export "_start" (func $_start))
162+
(data (i32.const 0) "Hello, world!\0a")
163+
)"#;
164+
117165
#[test]
118166
fn workload_run_return_1() {
119-
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/fixtures/return_1.wasm")).to_vec();
167+
let bytes = wat::parse_str(RETURN_1_WAT).expect("error parsing wat");
120168

121169
let results: Vec<i32> =
122170
workload::run(&bytes, empty::<String>(), empty::<(String, String)>())
@@ -130,7 +178,7 @@ pub(crate) mod test {
130178

131179
#[test]
132180
fn workload_run_no_export() {
133-
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/fixtures/no_export.wasm")).to_vec();
181+
let bytes = wat::parse_str(NO_EXPORT_WAT).expect("error parsing wat");
134182

135183
match workload::run(&bytes, empty::<String>(), empty::<(String, String)>()) {
136184
Err(workload::Error::ExportNotFound) => {}
@@ -139,9 +187,8 @@ pub(crate) mod test {
139187
}
140188

141189
#[test]
142-
fn workload_run_wasi_snapshot1() {
143-
let bytes =
144-
include_bytes!(concat!(env!("OUT_DIR"), "/fixtures/wasi_snapshot1.wasm")).to_vec();
190+
fn workload_run_wasi_count_args() {
191+
let bytes = wat::parse_str(WASI_COUNT_ARGS_WAT).expect("error parsing wat");
145192

146193
let results: Vec<i32> = workload::run(
147194
&bytes,
@@ -156,18 +203,17 @@ pub(crate) mod test {
156203
assert_eq!(results, vec![3]);
157204
}
158205

159-
#[cfg(bundle_tests)]
160206
#[test]
161-
fn workload_run_bundled() {
162-
let bytes = include_bytes!(concat!(
163-
env!("OUT_DIR"),
164-
"/fixtures/hello_wasi_snapshot1.bundled.wasm"
165-
))
166-
.to_vec();
207+
fn workload_run_hello_wasi() {
208+
let bytes = wat::parse_str(HELLO_WASI_WAT).expect("error parsing wat");
209+
let args: Vec<String> = vec![];
210+
let envs: Vec<(String, String)> = vec![];
211+
212+
let results = workload::run(&bytes, args, envs).unwrap();
167213

168-
workload::run(&bytes, empty::<&str>(), empty::<(&str, &str)>()).unwrap();
214+
assert_eq!(results.len(), 0);
169215

170-
let output = std::fs::read("stdout.txt").unwrap();
171-
assert_eq!(output, "Hello, world!\n".to_string().into_bytes());
216+
// TODO/FIXME: we need a way to configure WASI stdout so we can capture
217+
// and check it here...
172218
}
173219
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)