From e734190c24d2a5aca5b62c2b1ab7e6136017a25c Mon Sep 17 00:00:00 2001 From: pcpthm Date: Fri, 22 Mar 2019 02:05:12 +0900 Subject: Refactor parser fuzz testing --- crates/ra_syntax/fuzz/Cargo.toml | 8 ++++---- crates/ra_syntax/fuzz/fuzz_targets/parser.rs | 6 +++--- crates/ra_syntax/src/fuzz.rs | 12 ++++++++++++ crates/ra_syntax/src/lib.rs | 9 ++------- crates/ra_syntax/tests/test.rs | 4 ++-- 5 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 crates/ra_syntax/src/fuzz.rs diff --git a/crates/ra_syntax/fuzz/Cargo.toml b/crates/ra_syntax/fuzz/Cargo.toml index 4a255882e..c54d12813 100644 --- a/crates/ra_syntax/fuzz/Cargo.toml +++ b/crates/ra_syntax/fuzz/Cargo.toml @@ -4,14 +4,14 @@ name = "ra_syntax-fuzz" version = "0.0.1" authors = ["rust-analyzer developers"] publish = false +edition = "2018" [package.metadata] cargo-fuzz = true -[dependencies.ra_syntax] -path = ".." -[dependencies.libfuzzer-sys] -git = "https://github.com/rust-fuzz/libfuzzer-sys.git" +[dependencies] +ra_syntax = { path = ".." } +libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" } # Prevent this from interfering with workspaces [workspace] diff --git a/crates/ra_syntax/fuzz/fuzz_targets/parser.rs b/crates/ra_syntax/fuzz/fuzz_targets/parser.rs index 4667d5579..76a8b08d0 100644 --- a/crates/ra_syntax/fuzz/fuzz_targets/parser.rs +++ b/crates/ra_syntax/fuzz/fuzz_targets/parser.rs @@ -1,9 +1,9 @@ #![no_main] -#[macro_use] extern crate libfuzzer_sys; -extern crate ra_syntax; +use libfuzzer_sys::fuzz_target; +use ra_syntax::fuzz::check_parser; fuzz_target!(|data: &[u8]| { if let Ok(text) = std::str::from_utf8(data) { - ra_syntax::check_fuzz_invariants(text) + check_parser(text) } }); diff --git a/crates/ra_syntax/src/fuzz.rs b/crates/ra_syntax/src/fuzz.rs new file mode 100644 index 000000000..03f453a6e --- /dev/null +++ b/crates/ra_syntax/src/fuzz.rs @@ -0,0 +1,12 @@ +use crate::{SourceFile, validation, AstNode}; + +fn check_file_invariants(file: &SourceFile) { + let root = file.syntax(); + validation::validate_block_structure(root); + let _ = file.errors(); +} + +pub fn check_parser(text: &str) { + let file = SourceFile::parse(text); + check_file_invariants(&file); +} diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 7334d53ef..4f3020440 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -29,6 +29,8 @@ mod ptr; pub mod algo; pub mod ast; +#[doc(hidden)] +pub mod fuzz; pub use rowan::{SmolStr, TextRange, TextUnit}; pub use ra_parser::SyntaxKind; @@ -83,13 +85,6 @@ impl SourceFile { } } -pub fn check_fuzz_invariants(text: &str) { - let file = SourceFile::parse(text); - let root = file.syntax(); - validation::validate_block_structure(root); - let _ = file.errors(); -} - /// This test does not assert anything and instead just shows off the crate's /// API. #[test] diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 458740c13..3de4a65af 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -8,7 +8,7 @@ use std::{ }; use test_utils::{project_dir, dir_tests, read_text, collect_tests}; -use ra_syntax::{SourceFile, AstNode, check_fuzz_invariants}; +use ra_syntax::{SourceFile, AstNode, fuzz}; #[test] fn lexer_tests() { @@ -47,7 +47,7 @@ fn parser_tests() { #[test] fn parser_fuzz_tests() { for (_, text) in collect_tests(&test_data_dir(), &["parser/fuzz-failures"]) { - check_fuzz_invariants(&text) + fuzz::check_parser(&text) } } -- cgit v1.2.3