From 4c7142d0c9be90c8947deb788993d903b2e0a5d1 Mon Sep 17 00:00:00 2001 From: pcpthm Date: Fri, 22 Mar 2019 02:06:48 +0900 Subject: [PATCH] Add fuzz test for reparsing --- crates/ra_syntax/fuzz/Cargo.toml | 5 +++ crates/ra_syntax/fuzz/fuzz_targets/reparse.rs | 9 ++++ crates/ra_syntax/src/fuzz.rs | 42 ++++++++++++++++++- crates/ra_syntax/tests/test.rs | 9 ++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 crates/ra_syntax/fuzz/fuzz_targets/reparse.rs diff --git a/crates/ra_syntax/fuzz/Cargo.toml b/crates/ra_syntax/fuzz/Cargo.toml index c54d12813e9..613ad2857d0 100644 --- a/crates/ra_syntax/fuzz/Cargo.toml +++ b/crates/ra_syntax/fuzz/Cargo.toml @@ -11,6 +11,7 @@ cargo-fuzz = true [dependencies] ra_syntax = { path = ".." } +ra_text_edit = { path = "../../ra_text_edit" } libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" } # Prevent this from interfering with workspaces @@ -20,3 +21,7 @@ members = ["."] [[bin]] name = "parser" path = "fuzz_targets/parser.rs" + +[[bin]] +name = "reparse" +path = "fuzz_targets/reparse.rs" diff --git a/crates/ra_syntax/fuzz/fuzz_targets/reparse.rs b/crates/ra_syntax/fuzz/fuzz_targets/reparse.rs new file mode 100644 index 00000000000..45524d4c123 --- /dev/null +++ b/crates/ra_syntax/fuzz/fuzz_targets/reparse.rs @@ -0,0 +1,9 @@ +#![no_main] +use libfuzzer_sys::fuzz_target; +use ra_syntax::fuzz::CheckReparse; + +fuzz_target!(|data: &[u8]| { + if let Some(check) = CheckReparse::from_data(data) { + check.run(); + } +}); diff --git a/crates/ra_syntax/src/fuzz.rs b/crates/ra_syntax/src/fuzz.rs index 03f453a6ef2..efb080ac20f 100644 --- a/crates/ra_syntax/src/fuzz.rs +++ b/crates/ra_syntax/src/fuzz.rs @@ -1,4 +1,6 @@ -use crate::{SourceFile, validation, AstNode}; +use crate::{SourceFile, validation, TextUnit, TextRange, AstNode}; +use ra_text_edit::AtomTextEdit; +use std::str::{self, FromStr}; fn check_file_invariants(file: &SourceFile) { let root = file.syntax(); @@ -10,3 +12,41 @@ pub fn check_parser(text: &str) { let file = SourceFile::parse(text); check_file_invariants(&file); } + +#[derive(Debug, Clone)] +pub struct CheckReparse { + text: String, + edit: AtomTextEdit, + edited_text: String, +} + +impl CheckReparse { + pub fn from_data(data: &[u8]) -> Option { + let data = str::from_utf8(data).ok()?; + let mut lines = data.lines(); + let delete_start = usize::from_str(lines.next()?).ok()?; + let delete_len = usize::from_str(lines.next()?).ok()?; + let insert = lines.next()?.to_string(); + let text = lines.collect::>().join("\n"); + text.get(delete_start..delete_start.checked_add(delete_len)?)?; // make sure delete is a valid range + let delete = TextRange::offset_len( + TextUnit::from_usize(delete_start), + TextUnit::from_usize(delete_len), + ); + let edited_text = + format!("{}{}{}", &text[..delete_start], &insert, &text[delete_start + delete_len..]); + let edit = AtomTextEdit { delete, insert }; + Some(CheckReparse { text, edit, edited_text }) + } + + pub fn run(&self) { + let file = SourceFile::parse(&self.text); + let new_file = file.reparse(&self.edit); + check_file_invariants(&new_file); + assert_eq!(&new_file.syntax().text().to_string(), &self.edited_text); + let full_reparse = SourceFile::parse(&self.edited_text); + for (a, b) in new_file.syntax().descendants().zip(full_reparse.syntax().descendants()) { + assert_eq!(a.kind(), b.kind(), "different syntax tree produced by a full reparse"); + } + } +} diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 3de4a65af60..537b01368b0 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -51,6 +51,15 @@ fn parser_fuzz_tests() { } } +#[test] +fn reparse_fuzz_tests() { + for (_, text) in collect_tests(&test_data_dir(), &["reparse/fuzz-failures"]) { + let check = fuzz::CheckReparse::from_data(text.as_bytes()).unwrap(); + println!("{:?}", check); + check.run(); + } +} + /// Test that Rust-analyzer can parse and validate the rust-analyser /// TODO: Use this as a benchmark #[test]