dqhl76 commented on code in PR #2609: URL: https://github.com/apache/incubator-opendal/pull/2609#discussion_r1260547908
########## core/fuzz/fuzz_reader.rs: ########## @@ -0,0 +1,124 @@ +// 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] + +mod utils; + +use libfuzzer_sys::arbitrary::{Arbitrary, Result, Unstructured}; +use libfuzzer_sys::fuzz_target; +use opendal::raw::oio::ReadExt; +use opendal::Operator; +use std::io::SeekFrom; + +const MAX_DATA_SIZE: usize = 1000; + +#[derive(Debug, Clone)] +enum ReaderAction { + Read { size: usize }, + Seek(SeekFrom), + Next, +} + +#[derive(Debug, Clone)] +struct FuzzInput { + actions: Vec<ReaderAction>, + data: Vec<u8>, +} + +impl Arbitrary<'_> for FuzzInput { + fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> { + // Choose a suitable range for the data length + let data_len = u.int_in_range(1..=MAX_DATA_SIZE)?; + let data: Vec<u8> = u.bytes(data_len)?.to_vec(); + let mut actions = vec![]; + while !u.is_empty() { + 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 => { + let offset = u.int_in_range(0..=data_len)?; + let seek_from = match u.int_in_range(0..=2)? { + 0 => SeekFrom::Start(offset as u64), + 1 => SeekFrom::End(offset as i64), + _ => SeekFrom::Current(offset as i64), + }; + actions.push(ReaderAction::Seek(seek_from)); + } + _ => actions.push(ReaderAction::Next), + } + } + Ok(FuzzInput { actions, data }) + } +} + +fn fuzz_reader(name: &str, op: &Operator, input: FuzzInput) { + let len = input.data.len(); + let result: anyhow::Result<()> = tokio::runtime::Runtime::new().unwrap().block_on(async { + let path = uuid::Uuid::new_v4().to_string(); + op.write(&path, input.data) + .await + .expect(format!("{} write must succeed", name).as_str()); + let mut o = op + .range_reader(&path, 0..len as u64) + .await + .expect(format!("{} init range_reader must succeed", name).as_str()); + + for action in input.actions { Review Comment: I will add the verification for this pr and leave the `reader` `range_reader` cases for next one. The reason is that if we add the Current(negative value), it may trigger the panic `Error: Unexpected (permanent) at Reader::seek => invalid seek to a negative or overflowing position`. I will handle this in the verfication process.(to ensure the error happen successfully by tracking the current position) (is not a very mature plan. feel free to give me some advice) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
