dqhl76 commented on code in PR #2609: URL: https://github.com/apache/incubator-opendal/pull/2609#discussion_r1257276394
########## core/fuzz/fuzz_targets/fuzz_reader_fs.rs: ########## @@ -0,0 +1,134 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#![no_main] + +use libfuzzer_sys::arbitrary::{Arbitrary, Result, Unstructured}; +use libfuzzer_sys::fuzz_target; +use opendal::raw::oio::ReadExt; +use opendal::{services, Operator}; +use std::env; + +#[derive(Debug)] +enum ReaderAction { + Read { size: usize }, + Seek { offset: usize, mode:SeekMode }, + Next, +} + +#[derive(Debug)] +enum SeekMode { + Start, + End, + Current, +} + +#[derive(Debug)] +struct FuzzInput { + actions: Vec<ReaderAction>, + data: Vec<u8>, +} + +impl Arbitrary<'_> for SeekMode { + fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> { + match u.int_in_range(0..=2)? { + 0 => Ok(SeekMode::Start), + 1 => Ok(SeekMode::End), + _ => Ok(SeekMode::Current), + } + } +} + +impl Arbitrary<'_> for FuzzInput { + fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> { + let data: Vec<u8> = { + // Choose a suitable range for the data length + let data_len = u.int_in_range(1..=100)?; + u.bytes(data_len)?.to_vec() + }; + let mut actions = vec![]; + while u.len() > 0 { + match u.int_in_range(0..=2)? { + 0 => { + // Ensure size is smaller than data size + let size = u.int_in_range(0..=data.len())?; + actions.push(ReaderAction::Read { size }); + } + 1 => { + // Ensure offset is smaller than data size + let mode = SeekMode::arbitrary(u)?; + let offset = u.int_in_range(0..=data.len())?; + actions.push(ReaderAction::Seek { mode, offset }); + } + _ => actions.push(ReaderAction::Next), + } + } + Ok(FuzzInput { actions, data }) + } +} + +fuzz_target!(|input: FuzzInput| { Review Comment: [libfuzzer](https://github.com/rust-fuzz/libfuzzer) not provide a simple way to do that without the macro. I think implement it by a function may make the implementation here complicated to read -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@opendal.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org