Update fuzz testers

This commit is contained in:
Simon Ask Ulsnes 2024-02-03 10:34:55 +01:00
parent b865e1459b
commit c3d1aba783
5 changed files with 21 additions and 106 deletions

View file

@ -10,7 +10,7 @@ cargo-fuzz = true
[dependencies] [dependencies]
libfuzzer-sys = "0.4.7" libfuzzer-sys = "0.4.7"
unsafe-libyaml = { path = ".." } libyaml-safer = { path = ".." }
[[bin]] [[bin]]
name = "scan" name = "scan"

View file

@ -1,47 +1,20 @@
#![no_main] #![no_main]
use libfuzzer_sys::fuzz_target; use libfuzzer_sys::fuzz_target;
use std::cmp; use libyaml_safer::{
use std::ffi::c_void; yaml_document_get_root_node, yaml_parser_load, yaml_parser_new, yaml_parser_set_input,
use std::mem::MaybeUninit;
use std::ptr;
use std::ptr::addr_of_mut;
use unsafe_libyaml::{
yaml_document_delete, yaml_document_get_root_node, yaml_document_t, yaml_parser_delete,
yaml_parser_initialize, yaml_parser_load, yaml_parser_set_input, yaml_parser_t,
}; };
fuzz_target!(|data: &[u8]| unsafe { fuzz_target(data) }); fuzz_target!(|data: &[u8]| unsafe { fuzz_target(data) });
unsafe fn fuzz_target(mut data: &[u8]) { unsafe fn fuzz_target(mut data: &[u8]) {
let mut parser = MaybeUninit::<yaml_parser_t>::uninit(); let mut parser = yaml_parser_new();
let parser = parser.as_mut_ptr(); yaml_parser_set_input(&mut parser, &mut data);
assert!(yaml_parser_initialize(parser).ok);
yaml_parser_set_input(parser, read_from_slice, addr_of_mut!(data).cast());
let mut document = MaybeUninit::<yaml_document_t>::uninit(); while let Ok(mut document) = yaml_parser_load(&mut parser) {
let document = document.as_mut_ptr(); let done = yaml_document_get_root_node(&mut document).is_none();
while yaml_parser_load(parser, document).ok {
let done = yaml_document_get_root_node(document).is_null();
yaml_document_delete(document);
if done { if done {
break; break;
} }
} }
yaml_parser_delete(parser);
}
unsafe fn read_from_slice(
data: *mut c_void,
buffer: *mut u8,
size: u64,
size_read: *mut u64,
) -> i32 {
let data = data.cast::<&[u8]>();
let input = data.read();
let n = cmp::min(input.len(), size as usize);
ptr::copy_nonoverlapping(input.as_ptr(), buffer, n);
data.write(&input[n..]);
*size_read = n as u64;
1
} }

View file

@ -1,47 +1,18 @@
#![no_main] #![no_main]
use libfuzzer_sys::fuzz_target; use libfuzzer_sys::fuzz_target;
use std::cmp; use libyaml_safer::{yaml_parser_new, yaml_parser_parse, yaml_parser_set_input, EventData};
use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::ptr;
use std::ptr::addr_of_mut;
use unsafe_libyaml::{
yaml_event_delete, yaml_event_t, yaml_parser_delete, yaml_parser_initialize, yaml_parser_parse,
yaml_parser_set_input, yaml_parser_t, YAML_STREAM_END_EVENT,
};
fuzz_target!(|data: &[u8]| unsafe { fuzz_target(data) }); fuzz_target!(|data: &[u8]| unsafe { fuzz_target(data) });
unsafe fn fuzz_target(mut data: &[u8]) { unsafe fn fuzz_target(mut data: &[u8]) {
let mut parser = MaybeUninit::<yaml_parser_t>::uninit(); let mut parser = yaml_parser_new();
let parser = parser.as_mut_ptr(); yaml_parser_set_input(&mut parser, &mut data);
assert!(yaml_parser_initialize(parser).ok);
yaml_parser_set_input(parser, read_from_slice, addr_of_mut!(data).cast());
let mut event = MaybeUninit::<yaml_event_t>::uninit(); while let Ok(event) = yaml_parser_parse(&mut parser) {
let event = event.as_mut_ptr(); let is_end = matches!(event.data, EventData::StreamEnd);
while yaml_parser_parse(parser, event).ok { if is_end {
let type_ = (*event).type_;
yaml_event_delete(event);
if type_ == YAML_STREAM_END_EVENT {
break; break;
} }
} }
yaml_parser_delete(parser);
}
unsafe fn read_from_slice(
data: *mut c_void,
buffer: *mut u8,
size: u64,
size_read: *mut u64,
) -> i32 {
let data = data.cast::<&[u8]>();
let input = data.read();
let n = cmp::min(input.len(), size as usize);
ptr::copy_nonoverlapping(input.as_ptr(), buffer, n);
data.write(&input[n..]);
*size_read = n as u64;
1
} }

View file

@ -1,47 +1,18 @@
#![no_main] #![no_main]
use libfuzzer_sys::fuzz_target; use libfuzzer_sys::fuzz_target;
use std::cmp; use libyaml_safer::{yaml_parser_new, yaml_parser_scan, yaml_parser_set_input, TokenData};
use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::ptr;
use std::ptr::addr_of_mut;
use unsafe_libyaml::{
yaml_parser_delete, yaml_parser_initialize, yaml_parser_scan, yaml_parser_set_input,
yaml_parser_t, yaml_token_delete, yaml_token_t, YAML_STREAM_END_TOKEN,
};
fuzz_target!(|data: &[u8]| unsafe { fuzz_target(data) }); fuzz_target!(|data: &[u8]| unsafe { fuzz_target(data) });
unsafe fn fuzz_target(mut data: &[u8]) { fn fuzz_target(mut data: &[u8]) {
let mut parser = MaybeUninit::<yaml_parser_t>::uninit(); let mut parser = yaml_parser_new();
let parser = parser.as_mut_ptr(); yaml_parser_set_input(&mut parser, &mut data);
assert!(yaml_parser_initialize(parser).ok);
yaml_parser_set_input(parser, read_from_slice, addr_of_mut!(data).cast());
let mut token = MaybeUninit::<yaml_token_t>::uninit(); while let Ok(token) = yaml_parser_scan(&mut parser) {
let token = token.as_mut_ptr(); let is_end = matches!(token.data, TokenData::StreamEnd);
while yaml_parser_scan(parser, token).ok { if is_end {
let type_ = (*token).type_;
yaml_token_delete(token);
if type_ == YAML_STREAM_END_TOKEN {
break; break;
} }
} }
yaml_parser_delete(parser);
}
unsafe fn read_from_slice(
data: *mut c_void,
buffer: *mut u8,
size: u64,
size_read: *mut u64,
) -> i32 {
let data = data.cast::<&[u8]>();
let input = data.read();
let n = cmp::min(input.len(), size as usize);
ptr::copy_nonoverlapping(input.as_ptr(), buffer, n);
data.write(&input[n..]);
*size_read = n as u64;
1
} }

View file

@ -65,7 +65,7 @@ pub use crate::writer::yaml_emitter_flush;
pub use crate::yaml::{ pub use crate::yaml::{
AliasData, Break, Document, Emitter, EmitterState, Encoding, Event, EventData, MappingStyle, AliasData, Break, Document, Emitter, EmitterState, Encoding, Event, EventData, MappingStyle,
Mark, Node, NodeItem, NodePair, Parser, ParserState, ScalarStyle, SequenceStyle, SimpleKey, Mark, Node, NodeItem, NodePair, Parser, ParserState, ScalarStyle, SequenceStyle, SimpleKey,
TagDirective, Token, VersionDirective, TagDirective, Token, TokenData, VersionDirective,
}; };
#[doc(hidden)] #[doc(hidden)]
pub use crate::yaml::{ pub use crate::yaml::{