Fix the fuzz tests

This commit is contained in:
Kerollmops 2022-06-14 16:22:59 +02:00
parent e8297ad27e
commit 6d0498df24
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -7,10 +7,10 @@ use anyhow::{bail, Result};
use arbitrary_json::ArbitraryValue; use arbitrary_json::ArbitraryValue;
use heed::EnvOpenOptions; use heed::EnvOpenOptions;
use libfuzzer_sys::fuzz_target; use libfuzzer_sys::fuzz_target;
use milli::documents::{DocumentBatchBuilder, DocumentBatchReader}; use milli::documents::{DocumentsBatchBuilder, DocumentsBatchReader};
use milli::update::{IndexDocuments, IndexDocumentsConfig, IndexerConfig, Settings}; use milli::update::{IndexDocuments, IndexDocumentsConfig, IndexerConfig, Settings};
use milli::Index; use milli::Index;
use serde_json::Value; use serde_json::{Map, Value};
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
#[global_allocator] #[global_allocator]
@ -19,21 +19,26 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
/// reads json from input and write an obkv batch to writer. /// reads json from input and write an obkv batch to writer.
pub fn read_json(input: impl Read, writer: impl Write + Seek) -> Result<usize> { pub fn read_json(input: impl Read, writer: impl Write + Seek) -> Result<usize> {
let writer = BufWriter::new(writer); let writer = BufWriter::new(writer);
let mut builder = DocumentBatchBuilder::new(writer)?; let mut builder = DocumentsBatchBuilder::new(writer);
builder.extend_from_json(input)?;
if builder.len() == 0 { let values: Vec<Map<String, Value>> = serde_json::from_reader(input)?;
if builder.documents_count() == 0 {
bail!("Empty payload"); bail!("Empty payload");
} }
let count = builder.finish()?; for object in values {
builder.append_json_object(&object)?;
}
Ok(count) let count = builder.documents_count();
let vector = builder.into_inner()?;
Ok(count as usize)
} }
fn index_documents( fn index_documents(
index: &mut milli::Index, index: &mut milli::Index,
documents: DocumentBatchReader<Cursor<Vec<u8>>>, documents: DocumentsBatchReader<Cursor<Vec<u8>>>,
) -> Result<()> { ) -> Result<()> {
let config = IndexerConfig::default(); let config = IndexerConfig::default();
let mut wtxn = index.write_txn()?; let mut wtxn = index.write_txn()?;
@ -98,7 +103,7 @@ fuzz_target!(|batches: Vec<Vec<ArbitraryValue>>| {
// We ignore all malformed documents // We ignore all malformed documents
if let Ok(_) = read_json(json.as_bytes(), &mut documents) { if let Ok(_) = read_json(json.as_bytes(), &mut documents) {
documents.rewind().unwrap(); documents.rewind().unwrap();
let documents = DocumentBatchReader::from_reader(documents).unwrap(); let documents = DocumentsBatchReader::from_reader(documents).unwrap();
// A lot of errors can come out of milli and we don't know which ones are normal or not // A lot of errors can come out of milli and we don't know which ones are normal or not
// so we are only going to look for the unexpected panics. // so we are only going to look for the unexpected panics.
let _ = index_documents(&mut index, documents); let _ = index_documents(&mut index, documents);