Skip to content

Commit 5f13792

Browse files
committed
test: draft the proptest for wasm_smith generated Wasm compilation
1 parent cbb7e91 commit 5f13792

File tree

5 files changed

+140
-1
lines changed

5 files changed

+140
-1
lines changed

Cargo.lock

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

tests/integration/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ filetime = "0.2.23"
3535
glob = "0.3.1"
3636
walkdir = "2.5.0"
3737
proptest.workspace = true
38+
wasm-smith = "0.211"
39+
arbitrary = "1.3"
3840

3941
[dev-dependencies]
4042
miden-core.workspace = true

tests/integration/src/compiler_test.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ pub enum CompilerTestSource {
3333
RustCargoComponent {
3434
artifact_name: String,
3535
},
36+
Wasm {
37+
artifact_name: String,
38+
},
3639
}
3740

3841
impl CompilerTestSource {
@@ -497,6 +500,27 @@ impl CompilerTest {
497500
Self::rust_source_cargo_lib(proj.root(), name, is_build_std, Some("entrypoint".to_string()))
498501
}
499502

503+
/// Set the Wasm core module to compile
504+
pub fn wasm_module(wasm_bytes: Vec<u8>) -> Self {
505+
let artifact_name = "wasm_smith";
506+
let input_file = InputFile::from_bytes(
507+
wasm_bytes,
508+
miden_diagnostics::FileName::Virtual(artifact_name.into()),
509+
)
510+
.unwrap();
511+
Self {
512+
config: WasmTranslationConfig::default(),
513+
session: default_session(input_file),
514+
source: CompilerTestSource::Wasm {
515+
artifact_name: artifact_name.to_string(),
516+
},
517+
entrypoint: None,
518+
hir: None,
519+
masm_program: None,
520+
masm_src: None,
521+
}
522+
}
523+
500524
/// Compare the compiled Wasm against the expected output
501525
pub fn expect_wasm(&self, expected_wat_file: expect_test::ExpectFile) {
502526
let wasm_bytes = self.wasm_bytes();
@@ -770,7 +794,7 @@ pub(crate) fn demangle(name: &str) -> String {
770794
String::from_utf8(demangled).unwrap()
771795
}
772796

773-
fn wasm_to_wat(wasm_bytes: &[u8]) -> String {
797+
pub fn wasm_to_wat(wasm_bytes: &[u8]) -> String {
774798
let mut wasm_printer = wasmprinter::Printer::new();
775799
// disable printing of the "producers" section because it contains a rustc version
776800
// to not brake tests when rustc is updated

tests/integration/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ pub use exec_vm::execute_vm;
1515

1616
#[cfg(test)]
1717
mod rust_masm_tests;
18+
19+
#[cfg(test)]
20+
mod wasm_smith_tests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use arbitrary::Unstructured;
2+
use prop::collection::vec;
3+
use proptest::{prelude::*, test_runner::TestRunner};
4+
use wasm_smith::{Config, InstructionKind, InstructionKinds};
5+
6+
use crate::{compiler_test::wasm_to_wat, CompilerTest};
7+
8+
fn wasm_smith_default_config() -> Config {
9+
Config {
10+
allow_start_export: false,
11+
allowed_instructions: InstructionKinds::new(&[InstructionKind::Control]),
12+
bulk_memory_enabled: false,
13+
exceptions_enabled: false,
14+
gc_enabled: false,
15+
max_aliases: 0,
16+
max_data_segments: 0,
17+
max_element_segments: 0,
18+
max_elements: 0,
19+
max_exports: 0,
20+
max_funcs: 1,
21+
max_imports: 0,
22+
max_table_elements: 0,
23+
max_tables: 0,
24+
max_tags: 0,
25+
memory64_enabled: false,
26+
min_funcs: 1,
27+
multi_value_enabled: false,
28+
reference_types_enabled: false,
29+
relaxed_simd_enabled: false,
30+
saturating_float_to_int_enabled: false,
31+
sign_extension_ops_enabled: false,
32+
simd_enabled: false,
33+
tail_call_enabled: false,
34+
..Config::default()
35+
}
36+
}
37+
38+
#[test]
39+
fn simple_ctrl() {
40+
TestRunner::default()
41+
.run(&vec(0..=255u8, 100), move |bytes| {
42+
let config = wasm_smith_default_config();
43+
let mut wasm_module =
44+
wasm_smith::Module::new(config, &mut Unstructured::new(&bytes)).unwrap();
45+
wasm_module.ensure_termination(100).unwrap();
46+
let wasm_module_bytes = wasm_module.to_bytes();
47+
let wat = wasm_to_wat(&wasm_module_bytes);
48+
eprintln!("wat:\n{}", wat);
49+
let mut test = CompilerTest::wasm_module(wasm_module_bytes);
50+
let vm_program = &test.masm_program();
51+
eprintln!("vm_program: {}", vm_program);
52+
// let rust_out = miden_integration_tests_rust_fib::fib(a);
53+
// let args = [Felt::from(a)];
54+
// let vm_out: u32 = (*execute_vm(vm_program, &args).first().unwrap()).into();
55+
// prop_assert_eq!(rust_out, vm_out);
56+
Ok(())
57+
})
58+
.unwrap();
59+
}

0 commit comments

Comments
 (0)