Skip to content

Commit

Permalink
test: draft the proptest for wasm_smith generated Wasm compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jun 24, 2024
1 parent cbb7e91 commit 5f13792
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 1 deletion.
51 changes: 51 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions tests/integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ filetime = "0.2.23"
glob = "0.3.1"
walkdir = "2.5.0"
proptest.workspace = true
wasm-smith = "0.211"
arbitrary = "1.3"

[dev-dependencies]
miden-core.workspace = true
Expand Down
26 changes: 25 additions & 1 deletion tests/integration/src/compiler_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub enum CompilerTestSource {
RustCargoComponent {
artifact_name: String,
},
Wasm {
artifact_name: String,
},
}

impl CompilerTestSource {
Expand Down Expand Up @@ -497,6 +500,27 @@ impl CompilerTest {
Self::rust_source_cargo_lib(proj.root(), name, is_build_std, Some("entrypoint".to_string()))
}

/// Set the Wasm core module to compile
pub fn wasm_module(wasm_bytes: Vec<u8>) -> Self {
let artifact_name = "wasm_smith";
let input_file = InputFile::from_bytes(
wasm_bytes,
miden_diagnostics::FileName::Virtual(artifact_name.into()),
)
.unwrap();
Self {
config: WasmTranslationConfig::default(),
session: default_session(input_file),
source: CompilerTestSource::Wasm {
artifact_name: artifact_name.to_string(),
},
entrypoint: None,
hir: None,
masm_program: None,
masm_src: None,
}
}

/// Compare the compiled Wasm against the expected output
pub fn expect_wasm(&self, expected_wat_file: expect_test::ExpectFile) {
let wasm_bytes = self.wasm_bytes();
Expand Down Expand Up @@ -770,7 +794,7 @@ pub(crate) fn demangle(name: &str) -> String {
String::from_utf8(demangled).unwrap()
}

fn wasm_to_wat(wasm_bytes: &[u8]) -> String {
pub fn wasm_to_wat(wasm_bytes: &[u8]) -> String {
let mut wasm_printer = wasmprinter::Printer::new();
// disable printing of the "producers" section because it contains a rustc version
// to not brake tests when rustc is updated
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ pub use exec_vm::execute_vm;

#[cfg(test)]
mod rust_masm_tests;

#[cfg(test)]
mod wasm_smith_tests;
59 changes: 59 additions & 0 deletions tests/integration/src/wasm_smith_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use arbitrary::Unstructured;
use prop::collection::vec;
use proptest::{prelude::*, test_runner::TestRunner};
use wasm_smith::{Config, InstructionKind, InstructionKinds};

use crate::{compiler_test::wasm_to_wat, CompilerTest};

fn wasm_smith_default_config() -> Config {
Config {
allow_start_export: false,
allowed_instructions: InstructionKinds::new(&[InstructionKind::Control]),
bulk_memory_enabled: false,
exceptions_enabled: false,
gc_enabled: false,
max_aliases: 0,
max_data_segments: 0,
max_element_segments: 0,
max_elements: 0,
max_exports: 0,
max_funcs: 1,
max_imports: 0,
max_table_elements: 0,
max_tables: 0,
max_tags: 0,
memory64_enabled: false,
min_funcs: 1,
multi_value_enabled: false,
reference_types_enabled: false,
relaxed_simd_enabled: false,
saturating_float_to_int_enabled: false,
sign_extension_ops_enabled: false,
simd_enabled: false,
tail_call_enabled: false,
..Config::default()
}
}

#[test]
fn simple_ctrl() {
TestRunner::default()
.run(&vec(0..=255u8, 100), move |bytes| {
let config = wasm_smith_default_config();
let mut wasm_module =
wasm_smith::Module::new(config, &mut Unstructured::new(&bytes)).unwrap();
wasm_module.ensure_termination(100).unwrap();
let wasm_module_bytes = wasm_module.to_bytes();
let wat = wasm_to_wat(&wasm_module_bytes);
eprintln!("wat:\n{}", wat);
let mut test = CompilerTest::wasm_module(wasm_module_bytes);
let vm_program = &test.masm_program();
eprintln!("vm_program: {}", vm_program);
// let rust_out = miden_integration_tests_rust_fib::fib(a);
// let args = [Felt::from(a)];
// let vm_out: u32 = (*execute_vm(vm_program, &args).first().unwrap()).into();
// prop_assert_eq!(rust_out, vm_out);
Ok(())
})
.unwrap();
}

0 comments on commit 5f13792

Please sign in to comment.