Added emtests

This commit is contained in:
Syrus 2018-12-10 17:06:55 -08:00
parent 44a745e4f5
commit 5796b172d1
15 changed files with 8450 additions and 75 deletions

View File

@ -4,24 +4,79 @@
//! - Compile using emcc and get the .wasm from it (wasm)
//! - Generate the test that will compare the output of running the .wasm file
//! with wasmer with the expected output
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
static BANNER: &str = "// Rust test file autogenerated with cargo build (build/emtests.rs).
// Please do NOT modify it by hand, as it will be reseted on next build.\n";
const TESTS: [&str; 1] = [
"emtests/localtime.c"
"emtests/printf.c"
];
pub fn compile(file: &str) -> String {
let mut output_path = PathBuf::from(file);
output_path.set_extension("out");
let output_str = output_path.to_str().unwrap();
// Compile to .out
Command::new("cc")
.arg(file)
.arg("-o")
.arg(output_str)
.output()
.expect("failed to execute process");
// Get the result of .out
let output = Command::new(output_str)
.arg(output_str)
.output()
.expect("failed to execute process");
// Remove executable
fs::remove_file(output_str).unwrap();
let mut output_path = PathBuf::from(file);
output_path.set_extension("js");
let output_str = output_path.to_str().unwrap();
// Compile to wasm
Command::new("emcc")
.arg(file)
.arg("-s").arg("WASM=1")
.arg("-o")
.arg(output_str)
.output()
.expect("failed to execute process");
// Remove js file
fs::remove_file(output_str).unwrap();
let mut output_path = PathBuf::from(file);
output_path.set_extension("output");
let module_name = output_path.file_stem().unwrap().to_str().unwrap().to_owned();
let output_str = output_path.to_str().unwrap();
// Write the output to file
fs::write(output_str, output.stdout).expect("Unable to write file");
module_name
// panic!("OUTPUT: {:?}", output);
}
pub fn build() {
let rust_test_modpath = concat!(env!("CARGO_MANIFEST_DIR"), "/src/emtests/mod.rs");
let mut modules: Vec<String> = Vec::new();
// modules.reserve_exact(TESTS.len());
for test in TESTS.iter() {
let moudle_name = compile(test);
modules.push(format!("mod {};", moudle_name));
}
modules.insert(0, BANNER.to_string());
// We add an empty line
modules.push("".to_string());

View File

@ -3,6 +3,18 @@ This directory contains tests for unit testing each of the functions/syscalls th
If you want to generate the wasm files, you will just need to:
```
make emtests
```
This process will do something similar to:
```
cc localtime.c -o localtime.out
# Execute the out file and save its output
./localtime.out > ./localtime.output
rm localtime.out
# Generate the .wasm file
emcc localtime.c -o localtime.js
# Delte the js file, as we don't need it
rm localtime.js

View File

@ -1,5 +0,0 @@
Hello wasmer!
Almost!
Current local time and date: #ASCTIME#
Done!

8277
emtests/printf.c Normal file

File diff suppressed because it is too large Load Diff

17
emtests/printf.output Normal file
View File

@ -0,0 +1,17 @@
ab1.23cd
n=7
Characters: a A
Decimals: 1977 650000 12 4
Preceding with blanks: 1977 -1977
Preceding with zeros: 0000001977 -000001977
Force sign: +1977 -1977 +1977 -1977
Force sign or space: 1977 -1977 1977 -1977
Sign overrides space: +1977 -1977 +1977 -1977
Some different radixes: 100 64 144 0x64 0144
floats: 3.14 +3e+00 3.141600E+00 00003.14
negative floats: -3.14 -3e+00 -3.141600E+00 -0003.14
Force sign or space: 3.14 -3.14 3.14 -3.14
Width trick: 10
A string %
Null string: (null)

BIN
emtests/printf.wasm Normal file

Binary file not shown.

View File

@ -483,35 +483,3 @@ pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
import_object
}
#[cfg(test)]
mod tests {
use super::generate_emscripten_env;
use crate::webassembly::{instantiate, Export, Instance};
#[test]
fn test_putchar() {
let wasm_bytes = include_wast2wasm_bytes!("tests/putchar.wast");
let import_object = generate_emscripten_env();
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
let func_index = match result_object.module.info.exports.get("main") {
Some(&Export::Function(index)) => index,
_ => panic!("Function not found"),
};
let main: fn(&Instance) = get_instance_function!(result_object.instance, func_index);
main(&result_object.instance);
}
#[test]
fn test_print() {
let wasm_bytes = include_wast2wasm_bytes!("tests/printf.wast");
let import_object = generate_emscripten_env();
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
let func_index = match result_object.module.info.exports.get("main") {
Some(&Export::Function(index)) => index,
_ => panic!("Function not found"),
};
let main: fn(&Instance) = get_instance_function!(result_object.instance, func_index);
main(&result_object.instance);
}
}

View File

@ -1,19 +0,0 @@
(module
(type $FUNCSIG$ii (func (param i32) (result i32)))
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
(import "env" "printf" (func $printf (param i32 i32) (result i32)))
(table 0 anyfunc)
(memory $0 1)
(data (i32.const 16) "Hello World\00")
(export "memory" (memory $0))
(export "main" (func $main))
(func $main (; 1 ;) (result i32)
(drop
(call $printf
(i32.const 16)
(i32.const 0)
)
)
(i32.const 0)
)
)

View File

@ -1,16 +0,0 @@
(module
(type $FUNCSIG$ii (func (param i32) (result i32)))
(import "env" "putchar" (func $putchar (param i32) (result i32)))
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "main" (func $main))
(func $main (; 1 ;) (result i32)
(drop
(call $putchar
(i32.const 97)
)
)
(i32.const 0)
)
)

View File

@ -1 +1,2 @@
pub mod slice;
pub mod stdio;

66
src/common/stdio.rs Normal file
View File

@ -0,0 +1,66 @@
use libc;
use std::fs::File;
use std::io::Read;
use std::os::unix::io::FromRawFd;
// A struct to hold the references to the base stdout and the captured one
pub struct StdioCapturer {
stdout_backup: libc::c_int,
stderr_backup: libc::c_int,
stdout_reader: libc::c_int,
stderr_reader: libc::c_int,
}
// Implementation inspired in
// https://github.com/rust-lang/rust/blob/7d52cbce6db83e4fc2d8706b4e4b9c7da76cbcf8/src/test/run-pass/issues/issue-30490.rs
// Currently only works in Unix systems (Mac, Linux)
impl StdioCapturer {
fn pipe() -> (libc::c_int, libc::c_int) {
let mut fds = [0; 2];
assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);
(fds[0], fds[1])
}
pub fn new() -> Self {
let stdout_backup = unsafe { libc::dup(libc::STDOUT_FILENO) };
let stderr_backup = unsafe { libc::dup(libc::STDERR_FILENO) };
let (stdout_reader, stdout_writer) = Self::pipe();
let (stderr_reader, stderr_writer) = Self::pipe();
assert!(unsafe { libc::dup2(stdout_writer, libc::STDOUT_FILENO) } > -1);
assert!(unsafe { libc::dup2(stderr_writer, libc::STDERR_FILENO) } > -1);
// Make sure we close any duplicates of the writer end of the pipe,
// otherwise we can get stuck reading from the pipe which has open
// writers but no one supplying any input
assert_eq!(unsafe { libc::close(stdout_writer) }, 0);
assert_eq!(unsafe { libc::close(stderr_writer) }, 0);
StdioCapturer {
stdout_backup,
stderr_backup,
stdout_reader,
stderr_reader
}
}
pub fn end(self) -> (String, String) {
// The Stdio passed into the Command took over (and closed) std{out, err}
// so we should restore them as they were.
assert!(unsafe { libc::dup2(self.stdout_backup, libc::STDOUT_FILENO) } > -1);
assert!(unsafe { libc::dup2(self.stderr_backup, libc::STDERR_FILENO) } > -1);
assert_eq!(unsafe { libc::close(self.stdout_backup) }, 0);
assert_eq!(unsafe { libc::close(self.stderr_backup) }, 0);
let mut stdout_read = String::new();
let mut stdout_file: File = unsafe { FromRawFd::from_raw_fd(self.stdout_reader) };
let x = stdout_file.read_to_string(&mut stdout_read).expect("failed to read from stdout file");
let mut stderr_read = String::new();
let mut stderr_file: File = unsafe { FromRawFd::from_raw_fd(self.stderr_reader) };
let y = stderr_file.read_to_string(&mut stderr_read).expect("failed to read from stdout file");
(stdout_read, stderr_read)
}
}

0
src/emtests/_common.rs Normal file
View File

View File

@ -1,3 +1,4 @@
// Rust test file autogenerated with cargo build (build/emtests.rs).
// Please do NOT modify it by hand, as it will be reseted on next build.
mod printf;

16
src/emtests/printf.rs Normal file
View File

@ -0,0 +1,16 @@
use crate::apis::generate_emscripten_env;
use crate::webassembly::{instantiate, Export, Instance, start_instance};
use crate::common::stdio::StdioCapturer;
#[test]
fn test_printf() {
let wasm_bytes = include_bytes!("../../emtests/printf.wasm");
let import_object = generate_emscripten_env();
let mut result_object = instantiate(wasm_bytes.to_vec(), import_object).expect("Not compiled properly");
let mut capturer = StdioCapturer::new();
start_instance(&result_object.module, &mut result_object.instance, "printf", vec![]);
let output = capturer.end().0;
println!("Captured {}", output);
panic!();
}

View File

@ -27,5 +27,7 @@ pub mod common;
pub mod sighandler;
#[cfg(test)]
mod spectests;
#[cfg(test)]
mod emtests;
pub mod update;
pub mod webassembly;