mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 03:38:29 +00:00
Refactor parser fuzz testing
This commit is contained in:
parent
51323a852a
commit
e734190c24
@ -4,14 +4,14 @@ name = "ra_syntax-fuzz"
|
|||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
authors = ["rust-analyzer developers"]
|
authors = ["rust-analyzer developers"]
|
||||||
publish = false
|
publish = false
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
cargo-fuzz = true
|
cargo-fuzz = true
|
||||||
|
|
||||||
[dependencies.ra_syntax]
|
[dependencies]
|
||||||
path = ".."
|
ra_syntax = { path = ".." }
|
||||||
[dependencies.libfuzzer-sys]
|
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" }
|
||||||
git = "https://github.com/rust-fuzz/libfuzzer-sys.git"
|
|
||||||
|
|
||||||
# Prevent this from interfering with workspaces
|
# Prevent this from interfering with workspaces
|
||||||
[workspace]
|
[workspace]
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
#![no_main]
|
#![no_main]
|
||||||
#[macro_use] extern crate libfuzzer_sys;
|
use libfuzzer_sys::fuzz_target;
|
||||||
extern crate ra_syntax;
|
use ra_syntax::fuzz::check_parser;
|
||||||
|
|
||||||
fuzz_target!(|data: &[u8]| {
|
fuzz_target!(|data: &[u8]| {
|
||||||
if let Ok(text) = std::str::from_utf8(data) {
|
if let Ok(text) = std::str::from_utf8(data) {
|
||||||
ra_syntax::check_fuzz_invariants(text)
|
check_parser(text)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
12
crates/ra_syntax/src/fuzz.rs
Normal file
12
crates/ra_syntax/src/fuzz.rs
Normal file
@ -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);
|
||||||
|
}
|
@ -29,6 +29,8 @@ mod ptr;
|
|||||||
|
|
||||||
pub mod algo;
|
pub mod algo;
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub mod fuzz;
|
||||||
|
|
||||||
pub use rowan::{SmolStr, TextRange, TextUnit};
|
pub use rowan::{SmolStr, TextRange, TextUnit};
|
||||||
pub use ra_parser::SyntaxKind;
|
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
|
/// This test does not assert anything and instead just shows off the crate's
|
||||||
/// API.
|
/// API.
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -8,7 +8,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use test_utils::{project_dir, dir_tests, read_text, collect_tests};
|
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]
|
#[test]
|
||||||
fn lexer_tests() {
|
fn lexer_tests() {
|
||||||
@ -47,7 +47,7 @@ fn parser_tests() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn parser_fuzz_tests() {
|
fn parser_fuzz_tests() {
|
||||||
for (_, text) in collect_tests(&test_data_dir(), &["parser/fuzz-failures"]) {
|
for (_, text) in collect_tests(&test_data_dir(), &["parser/fuzz-failures"]) {
|
||||||
check_fuzz_invariants(&text)
|
fuzz::check_parser(&text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user