Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): add fuzz target for writer #2706

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ doc = false
name = "fuzz_reader"
path = "fuzz_reader.rs"
test = false

[[bin]]
doc = false
name = "fuzz_writer"
path = "fuzz_writer.rs"
test = false
39 changes: 28 additions & 11 deletions core/fuzz/fuzz_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@

#![no_main]

mod utils;

use std::io::SeekFrom;

use bytes::Bytes;
use libfuzzer_sys::arbitrary::Arbitrary;
use libfuzzer_sys::arbitrary::Result;
use libfuzzer_sys::arbitrary::Unstructured;
use libfuzzer_sys::fuzz_target;
use opendal::raw::oio::ReadExt;
use opendal::Operator;
use sha2::Digest;
use sha2::Sha256;

use opendal::raw::oio::ReadExt;
use opendal::Operator;

mod utils;

const MAX_DATA_SIZE: usize = 16 * 1024 * 1024;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -163,7 +164,12 @@ impl ReaderFuzzerChecker {
}
}

async fn fuzz_reader_process(input: FuzzInput, op: &Operator, name: &str) -> Result<()> {
async fn fuzz_reader_process(
input: FuzzInput,
op: &Operator,
name: &str,
is_range: bool,
) -> Result<()> {
let len = input.data.len();
let path = uuid::Uuid::new_v4().to_string();

Expand All @@ -172,10 +178,15 @@ async fn fuzz_reader_process(input: FuzzInput, op: &Operator, name: &str) -> Res
.await
.unwrap_or_else(|_| panic!("{} write must succeed", name));

let mut o = op
.range_reader(&path, 0..len as u64)
.await
.unwrap_or_else(|_| panic!("{} init range_reader must succeed", name));
let mut o = if is_range {
op.range_reader(&path, 0..len as u64)
dqhl76 marked this conversation as resolved.
Show resolved Hide resolved
.await
.unwrap_or_else(|_| panic!("{} init range_reader must succeed", name))
} else {
op.reader(&path)
.await
.unwrap_or_else(|_| panic!("{} init reader must succeed", name))
};

for action in input.actions {
match action {
Expand Down Expand Up @@ -213,9 +224,15 @@ fn fuzz_reader(name: &str, op: &Operator, input: FuzzInput) {
let runtime = tokio::runtime::Runtime::new().unwrap();

runtime.block_on(async {
fuzz_reader_process(input, op, name)
fuzz_reader_process(input.clone(), op, name, true)
.await
.unwrap_or_else(|_| panic!("{} fuzz range reader must succeed", name));
});

runtime.block_on(async {
fuzz_reader_process(input, op, name, false)
.await
.unwrap_or_else(|_| panic!("{} fuzz_reader must succeed", name));
.unwrap_or_else(|_| panic!("{} fuzz reader must succeed", name));
});
}

Expand Down
150 changes: 150 additions & 0 deletions core/fuzz/fuzz_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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 bytes::Bytes;
use libfuzzer_sys::arbitrary::Arbitrary;
use libfuzzer_sys::arbitrary::Result;
use libfuzzer_sys::arbitrary::Unstructured;
use libfuzzer_sys::fuzz_target;
use sha2::Digest;
use sha2::Sha256;

use opendal::Operator;

mod utils;

const MAX_DATA_SIZE: usize = 16 * 1024 * 1024;

#[derive(Debug, Clone)]
enum WriterAction {
Write { data: Bytes },
}

#[derive(Debug, Clone)]
struct FuzzInput {
actions: Vec<WriterAction>,
}

impl Arbitrary<'_> for FuzzInput {
fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> {
let mut actions = vec![];
let mut action_count = u.int_in_range(128..=1024)?;

while action_count != 0 {
action_count -= 1;
let data_len = u.int_in_range(1..=MAX_DATA_SIZE)?;
let data: Vec<u8> = u.bytes(data_len)?.to_vec();
actions.push(WriterAction::Write {
data: Bytes::from(data),
});
}

Ok(FuzzInput { actions })
}
}

struct WriterFuzzChecker {
data: Vec<u8>,
}

impl WriterFuzzChecker {
fn new(input: FuzzInput) -> Self {
let mut data = vec![];

for action in input.actions {
match action {
WriterAction::Write { data: d } => {
data.extend_from_slice(&d);
}
}
}

WriterFuzzChecker { data }
}

fn check(&self, actual: &[u8]) {
assert_eq!(
format!("{:x}", Sha256::digest(actual)),
format!("{:x}", Sha256::digest(&self.data)),
"check failed: result is not expected"
)
}
}

async fn fuzz_writer_process(input: FuzzInput, op: &Operator, name: &str) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();

let checker = WriterFuzzChecker::new(input.clone());

let mut writer = op
.writer(&path)
.await
.unwrap_or_else(|_| panic!("{} create must succeed", name));

for action in input.actions {
match action {
WriterAction::Write { data } => {
writer
.write(data)
.await
.unwrap_or_else(|_| panic!("{} write must succeed", name));
}
}
}
writer
.close()
.await
.unwrap_or_else(|_| panic!("{} close must succeed", name));

let result = op
.read(&path)
.await
.unwrap_or_else(|_| panic!("{} read must succeed", name));

checker.check(&result);

op.delete(&path)
.await
.unwrap_or_else(|_| panic!("{} delete must succeed", name));
Ok(())
}

fn fuzz_writer(name: &str, op: &Operator, input: FuzzInput) {
let runtime = tokio::runtime::Runtime::new().unwrap();

runtime.block_on(async {
fuzz_writer_process(input, op, name)
.await
.unwrap_or_else(|_| panic!("{} fuzz writer must succeed", name));
});
}

fuzz_target!(|input: FuzzInput| {
let _ = dotenvy::dotenv();

for service in utils::init_services() {
if service.1.is_none() {
continue;
}

let op = service.1.unwrap();

fuzz_writer(service.0, &op, input.clone());
}
});