Xuanwo commented on code in PR #2609: URL: https://github.com/apache/incubator-opendal/pull/2609#discussion_r1259226256
########## core/fuzz/README.md: ########## @@ -0,0 +1,44 @@ +# Fuzz Test for OpenDAL + +fuzz test are used to test the robustness of the code. + +## Setup + +To run the fuzz tests, you need to switch your rust version to nightly. You can do this by running the following command: + +```bash +rustup override set nightly Review Comment: It's not a good idea to let our contributor use `rustup override set nightly`. I prefer to use `cargo +nightly xxx` instead. ########## core/fuzz/Cargo.toml: ########## @@ -0,0 +1,43 @@ +# 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. + +[package] +edition = "2021" +name = "opendal-fuzz" +publish = false +version = "0.0.0" + +[package.metadata] +cargo-fuzz = true + +[dependencies] +anyhow = "1.0.71" Review Comment: Please make sure `Cargo.lock`'s change has been committed. ########## 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), Review Comment: The usage for seek offset is wrong. We will miss the possible like `Current(-1024)` and `End(-1024)` ########## 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() { Review Comment: We can't control how many actions we will take, I prefer to use `int_in_range` to pick a good loops here. For example, [128..=1024]. ########## 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; Review Comment: 1000 doesn't make sense. We need to test range [1..16 * 1024 * 1024] to make sure we not always hit buffer. ########## 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 Review Comment: Read size can be large than data size. But to avoid using too much memory, we can limit to `data_size * 2`. ########## 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: Seems we didn't verify the result? ########## 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) Review Comment: We need to test two cases: - `reader`: the full range. - `range_reader`: the random picked range reader (we can do this in another PR). ########## 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 { Review Comment: Please don't put a large block in function call. We can write like the following: ```rust async fn my_fn(op: Operator, xx: yyy) -> Result<()> { xxxx } fn fuzz_reader(name: &str, op: &Operator, input: FuzzInput) { let len = input.data.len(); let runtime = tokio::runtime::Runtime::new().unwrap(); runtime.block_on(async { my_fn(op, xx).await }).unwrap() } ``` -- 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]
