-
Notifications
You must be signed in to change notification settings - Fork 387
chore(fuzz): Fuzz the SSA parser #8647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b268e74
Fuzz the SSA parser
aakoshh a12bb67
Pipeline can be empty
aakoshh a2678fc
Merge branch 'master' into af/ast-fuzz-ssa-parser
aakoshh b26a57a
Fix clippy
aakoshh b1a5e4e
Merge branch 'master' into af/ast-fuzz-ssa-parser
aakoshh ee42494
Merge branch 'master' into af/ast-fuzz-ssa-parser
aakoshh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| //! Test that the SSA of an arbitrary program can be printed and parsed back. | ||
| //! | ||
| //! ```shell | ||
| //! cargo test -p noir_ast_fuzzer --test parser | ||
| //! ``` | ||
| use std::time::Duration; | ||
|
|
||
| use acir::circuit::ExpressionWidth; | ||
| use arbtest::arbtest; | ||
| use noir_ast_fuzzer::{Config, DisplayAstAsNoir, arb_program}; | ||
| use noirc_evaluator::{ | ||
| brillig::BrilligOptions, | ||
| ssa::{ | ||
| self, | ||
| ir::function::function_values_iter, | ||
| primary_passes, | ||
| ssa_gen::{self, Ssa}, | ||
| }, | ||
| }; | ||
|
|
||
| fn seed_from_env() -> Option<u64> { | ||
| let Ok(seed) = std::env::var("NOIR_ARBTEST_SEED") else { return None }; | ||
| let seed = u64::from_str_radix(seed.trim_start_matches("0x"), 16) | ||
| .unwrap_or_else(|e| panic!("failed to parse seed '{seed}': {e}")); | ||
| Some(seed) | ||
| } | ||
|
|
||
| #[test] | ||
| fn arb_ssa_roundtrip() { | ||
| let maybe_seed = seed_from_env(); | ||
|
|
||
| let mut prop = arbtest(|u| { | ||
| let config = Config::default(); | ||
| let program = arb_program(u, config)?; | ||
|
|
||
| let options = ssa::SsaEvaluatorOptions { | ||
| ssa_logging: ssa::SsaLogging::None, | ||
| brillig_options: BrilligOptions::default(), | ||
| print_codegen_timings: false, | ||
| expression_width: ExpressionWidth::default(), | ||
| emit_ssa: None, | ||
| skip_underconstrained_check: true, | ||
| skip_brillig_constraints_check: true, | ||
| enable_brillig_constraints_check_lookback: false, | ||
| inliner_aggressiveness: 0, | ||
| max_bytecode_increase_percent: None, | ||
| skip_passes: Default::default(), | ||
| }; | ||
| let pipeline = primary_passes(&options); | ||
| let last_pass = u.choose_index(pipeline.len())?; | ||
| let passes = &pipeline[0..last_pass]; | ||
|
|
||
| // Print the AST if something goes wrong, then panic. | ||
| let print_ast_and_panic = |msg: &str| -> ! { | ||
| eprintln!("{}", DisplayAstAsNoir(&program)); | ||
| panic!("{msg}") | ||
| }; | ||
|
|
||
| // If we have a seed to debug and we know it's going to crash, print the AST. | ||
| if maybe_seed.is_some() { | ||
| eprintln!("{}", DisplayAstAsNoir(&program)); | ||
| } | ||
|
|
||
| // Generate the initial SSA; | ||
| let ssa = ssa_gen::generate_ssa(program.clone()).unwrap_or_else(|e| { | ||
| print_ast_and_panic(&format!("Failed to generate initial SSA: {e}")) | ||
| }); | ||
|
|
||
| let mut ssa1 = passes.iter().fold(ssa, |ssa, pass| { | ||
| pass.run(ssa).unwrap_or_else(|e| { | ||
| print_ast_and_panic(&format!("Failed to run pass {}: {e}", pass.msg())) | ||
| }) | ||
| }); | ||
|
|
||
| // Normalize before printing so IDs don't change. | ||
| ssa1.normalize_ids(); | ||
|
|
||
| // Print to str and parse back. | ||
| let ssa2 = Ssa::from_str(&ssa1.to_string()).unwrap_or_else(|e| { | ||
| let msg = passes.last().map(|p| p.msg()).unwrap_or("Initial SSA"); | ||
| print_ast_and_panic(&format!( | ||
| "Could not parse SSA after step {last_pass} ({msg}): \n{e:?}" | ||
| )) | ||
| }); | ||
|
|
||
| // Not everything is populated by the parser, and unfortunately serializing to JSON doesn't work either. | ||
| for (func_id, func1) in ssa1.functions { | ||
| let func2 = &ssa2.functions[&func_id]; | ||
| let values1 = function_values_iter(&func1).collect::<Vec<_>>(); | ||
| let values2 = function_values_iter(func2).collect::<Vec<_>>(); | ||
| similar_asserts::assert_eq!(values1, values2); | ||
| } | ||
|
|
||
| Ok(()) | ||
| }) | ||
| .budget(Duration::from_secs(10)) | ||
| .size_min(1 << 12) | ||
| .size_max(1 << 20); | ||
|
|
||
| if let Some(seed) = maybe_seed { | ||
| prop = prop.seed(seed); | ||
| } | ||
|
|
||
| prop.run(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.