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]
libfuzzer-sys = "0.4.7"
unsafe-libyaml = { path = ".." }
libyaml-safer = { path = ".." }
[[bin]]
name = "scan"

View file

@ -1,47 +1,20 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use std::cmp;
use std::ffi::c_void;
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,
use libyaml_safer::{
yaml_document_get_root_node, yaml_parser_load, yaml_parser_new, yaml_parser_set_input,
};
fuzz_target!(|data: &[u8]| unsafe { fuzz_target(data) });
unsafe fn fuzz_target(mut data: &[u8]) {
let mut parser = MaybeUninit::<yaml_parser_t>::uninit();
let parser = parser.as_mut_ptr();
assert!(yaml_parser_initialize(parser).ok);
yaml_parser_set_input(parser, read_from_slice, addr_of_mut!(data).cast());
let mut parser = yaml_parser_new();
yaml_parser_set_input(&mut parser, &mut data);
let mut document = MaybeUninit::<yaml_document_t>::uninit();
let document = document.as_mut_ptr();
while yaml_parser_load(parser, document).ok {
let done = yaml_document_get_root_node(document).is_null();
yaml_document_delete(document);
while let Ok(mut document) = yaml_parser_load(&mut parser) {
let done = yaml_document_get_root_node(&mut document).is_none();
if done {
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]
use libfuzzer_sys::fuzz_target;
use std::cmp;
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,
};
use libyaml_safer::{yaml_parser_new, yaml_parser_parse, yaml_parser_set_input, EventData};
fuzz_target!(|data: &[u8]| unsafe { fuzz_target(data) });
unsafe fn fuzz_target(mut data: &[u8]) {
let mut parser = MaybeUninit::<yaml_parser_t>::uninit();
let parser = parser.as_mut_ptr();
assert!(yaml_parser_initialize(parser).ok);
yaml_parser_set_input(parser, read_from_slice, addr_of_mut!(data).cast());
let mut parser = yaml_parser_new();
yaml_parser_set_input(&mut parser, &mut data);
let mut event = MaybeUninit::<yaml_event_t>::uninit();
let event = event.as_mut_ptr();
while yaml_parser_parse(parser, event).ok {
let type_ = (*event).type_;
yaml_event_delete(event);
if type_ == YAML_STREAM_END_EVENT {
while let Ok(event) = yaml_parser_parse(&mut parser) {
let is_end = matches!(event.data, EventData::StreamEnd);
if is_end {
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]
use libfuzzer_sys::fuzz_target;
use std::cmp;
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,
};
use libyaml_safer::{yaml_parser_new, yaml_parser_scan, yaml_parser_set_input, TokenData};
fuzz_target!(|data: &[u8]| unsafe { fuzz_target(data) });
unsafe fn fuzz_target(mut data: &[u8]) {
let mut parser = MaybeUninit::<yaml_parser_t>::uninit();
let parser = parser.as_mut_ptr();
assert!(yaml_parser_initialize(parser).ok);
yaml_parser_set_input(parser, read_from_slice, addr_of_mut!(data).cast());
fn fuzz_target(mut data: &[u8]) {
let mut parser = yaml_parser_new();
yaml_parser_set_input(&mut parser, &mut data);
let mut token = MaybeUninit::<yaml_token_t>::uninit();
let token = token.as_mut_ptr();
while yaml_parser_scan(parser, token).ok {
let type_ = (*token).type_;
yaml_token_delete(token);
if type_ == YAML_STREAM_END_TOKEN {
while let Ok(token) = yaml_parser_scan(&mut parser) {
let is_end = matches!(token.data, TokenData::StreamEnd);
if is_end {
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::{
AliasData, Break, Document, Emitter, EmitterState, Encoding, Event, EventData, MappingStyle,
Mark, Node, NodeItem, NodePair, Parser, ParserState, ScalarStyle, SequenceStyle, SimpleKey,
TagDirective, Token, VersionDirective,
TagDirective, Token, TokenData, VersionDirective,
};
#[doc(hidden)]
pub use crate::yaml::{