Add fuzz test for reparsing

This commit is contained in:
pcpthm 2019-03-22 02:06:48 +09:00
parent e734190c24
commit 4c7142d0c9
4 changed files with 64 additions and 1 deletions

View file

@ -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"

View file

@ -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();
}
});

View file

@ -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<Self> {
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::<Vec<_>>().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");
}
}
}

View file

@ -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]