diff --git a/Cargo.lock b/Cargo.lock index 0071b28c4be9..fdf5a62934d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -650,9 +650,11 @@ dependencies = [ "regex", "serde", "serde_derive", + "serde_json", "similar", "smallvec", "target-lexicon", + "tempfile", "thiserror", "toml", "walkdir", diff --git a/cranelift/filetests/Cargo.toml b/cranelift/filetests/Cargo.toml index 262520bacd1c..82176f4170a7 100644 --- a/cranelift/filetests/Cargo.toml +++ b/cranelift/filetests/Cargo.toml @@ -31,7 +31,7 @@ anyhow = { workspace = true } similar = { workspace = true } wat.workspace = true toml = { workspace = true } -serde = { workspace = true } +serde.workspace = true serde_derive = { workspace = true } cranelift-wasm.workspace = true wasmparser.workspace = true @@ -42,3 +42,5 @@ smallvec = { workspace = true } regex = { workspace = true } wasmtime = { workspace = true, features = ["cranelift"] } walkdir = { workspace = true } +tempfile.workspace = true +serde_json.workspace = true diff --git a/cranelift/filetests/src/lib.rs b/cranelift/filetests/src/lib.rs index 24d3e48050dd..726521a5d518 100644 --- a/cranelift/filetests/src/lib.rs +++ b/cranelift/filetests/src/lib.rs @@ -22,6 +22,7 @@ mod match_directive; mod runner; mod runone; mod subtest; +mod zkasm_runner; mod test_alias_analysis; mod test_cat; diff --git a/cranelift/filetests/src/test_zkasm.rs b/cranelift/filetests/src/test_zkasm.rs index 0029b28a52e6..e2937569631c 100644 --- a/cranelift/filetests/src/test_zkasm.rs +++ b/cranelift/filetests/src/test_zkasm.rs @@ -17,6 +17,8 @@ mod tests { use walkdir::WalkDir; use wasmtime::*; + use crate::zkasm_runner::run_zkasm; + fn setup() { let _ = env_logger::builder().is_test(true).try_init(); } @@ -380,6 +382,7 @@ mod tests { .join(format!("generated/{name}.zkasm"))]; let result = std::panic::catch_unwind(|| { let program = generate_zkasm(&module_binary); + run_zkasm(&program).unwrap(); expected.assert_eq(&program); }); if let Err(err) = result { diff --git a/cranelift/filetests/src/zkasm_runner.rs b/cranelift/filetests/src/zkasm_runner.rs new file mode 100644 index 000000000000..44966e803cfe --- /dev/null +++ b/cranelift/filetests/src/zkasm_runner.rs @@ -0,0 +1,61 @@ +use serde_derive::Deserialize; +use std::io::Read; +use std::path::Path; +use std::task::Wake; +use tempfile::{NamedTempFile, TempDir}; + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Counters { + cnt_arith: String, + cnt_binary: String, + cnt_keccak_f: String, + cnt_mem_align: String, + cnt_steps: u64, +} + +#[derive(Deserialize)] +pub struct ExecutionResult { + path: String, + status: String, + counters: Option, +} + +pub fn run_zkasm(contents: &str) -> anyhow::Result> { + let tmp_dir = TempDir::new()?; + let zkasm_file = tmp_dir.path().join("code.zkasm"); + std::fs::write(&zkasm_file, contents)?; + run_zkasm_path(&zkasm_file) +} + +pub fn run_zkasm_path(input_path: &Path) -> anyhow::Result> { + let dir_path = if input_path.is_dir() { + input_path + } else { + input_path.parent().unwrap() + }; + std::fs::create_dir(dir_path.join("helpers"))?; + let helpers_file = dir_path.join("helpers/2-exp.zkasm"); + std::fs::write( + helpers_file, + include_str!("../../zkasm_data/generated/helpers/2-exp.zkasm"), + )?; + + let mut output_file = NamedTempFile::new()?; + let output = std::process::Command::new("npm") + .args([ + "--prefix", + "../../tests/zkasm", + "test", + input_path.to_str().unwrap(), + output_file.path().to_str().unwrap(), + ]) + .output()?; + let mut buf = String::new(); + output_file.read_to_string(&mut buf)?; + if buf.is_empty() { + return Ok(Vec::new()); + } + let execution_results = serde_json::from_str(&buf)?; + Ok(execution_results) +}