Skip to content

Commit

Permalink
Make fixes for oss-fuzz (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia authored Mar 11, 2025
1 parent 274f5f3 commit 5e30d44
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 36 deletions.
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Use the rust builder
FROM gcr.io/oss-fuzz-base/base-builder-rust

# Use nightly
ENV RUSTUP_TOOLCHAIN=nightly

# Set necessary linking flags
ENV RUSTFLAGS="-C link-args=-lc++ -C link-args=-lc++abi"

# Install nim
RUN curl https://nim-lang.org/choosenim/init.sh -sSf | sh -s -- -y
ENV PATH=/root/.nimble/bin:$PATH

# Clone ckzg
RUN git clone --depth 1 https://github.com/ethereum/c-kzg-4844.git $SRC
# Clone the c-kzg-4844 repository
RUN git clone --recursive --depth=1 https://github.com/ethereum/c-kzg-4844.git

# Set our build script as the entry point
ENTRYPOINT "c-kzg-4844/build.sh"
4 changes: 2 additions & 2 deletions bindings/rust/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ mod tests {
let dir_path = root_dir
.join("fuzz")
.join("corpus")
.join("fuzz_verify_cell_kzg_proof");
.join("fuzz_verify_kzg_proof");
fs::create_dir_all(&dir_path).unwrap();
let file_path = dir_path.join(format!("data_{}.bin", index));
let mut file = File::create(&file_path).unwrap();
Expand Down Expand Up @@ -1210,7 +1210,7 @@ mod tests {
let dir_path = root_dir
.join("fuzz")
.join("corpus")
.join("fuzz_verify_cell_kzg_proof");
.join("fuzz_verify_blob_kzg_proof");
fs::create_dir_all(&dir_path).unwrap();
let file_path = dir_path.join(format!("data_{}.bin", index));
let mut file = File::create(&file_path).unwrap();
Expand Down
15 changes: 15 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#!/bin/bash -eu

cd "$SRC/c-kzg-4844"

echo "Copying the trusted setup"
mkdir -p "$OUT/src" && cp src/trusted_setup.txt "$OUT/src/"

echo "Generating seed corpuses"
cargo test --features generate-fuzz-corpus
for target in $(cargo fuzz list); do
pushd fuzz/corpus/$target
echo "Zipping seed corpus for $target"
zip -r "$OUT/${target}_seed_corpus.zip" .
popd
done

echo "Building fuzz targets"
cargo fuzz build --release
for target in $(cargo fuzz list); do
cp "fuzz/target/x86_64-unknown-linux-gnu/release/$target" "$OUT/"
Expand Down
24 changes: 20 additions & 4 deletions fuzz/fuzz_targets/fuzz_blob_to_kzg_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,34 @@ extern crate core;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use std::cell::UnsafeCell;
use std::env;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};

///////////////////////////////////////////////////////////////////////////////
// Helper Functions
///////////////////////////////////////////////////////////////////////////////

fn get_root_dir() -> PathBuf {
if let Ok(manifest) = env::var("CARGO_MANIFEST_DIR") {
// When running locally
PathBuf::from(manifest)
.parent()
.expect("CARGO_MANIFEST_DIR has no parent")
.to_path_buf()
} else {
// When running with oss-fuzz
env::current_dir().expect("Failed to get current directory")
}
}

///////////////////////////////////////////////////////////////////////////////
// CKZG Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: c_kzg::KzgSettings = {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
c_kzg::KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
}
Expand Down Expand Up @@ -47,8 +64,7 @@ impl SafeEthKzgContext {
static CONSTANTINE_CTX: OnceLock<Arc<SafeEthKzgContext>> = OnceLock::new();

fn initialize_constantine_ctx() -> Arc<SafeEthKzgContext> {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
let eth_kzg_context =
constantine::EthKzgContext::load_trusted_setup(&trusted_setup_file).unwrap();
Arc::new(SafeEthKzgContext::new(eth_kzg_context))
Expand Down
24 changes: 20 additions & 4 deletions fuzz/fuzz_targets/fuzz_compute_blob_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,32 @@ use libfuzzer_sys::fuzz_target;
use std::cell::UnsafeCell;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};
use std::env;

///////////////////////////////////////////////////////////////////////////////
// Helper Functions
///////////////////////////////////////////////////////////////////////////////

fn get_root_dir() -> PathBuf {
if let Ok(manifest) = env::var("CARGO_MANIFEST_DIR") {
// When running locally
PathBuf::from(manifest)
.parent()
.expect("CARGO_MANIFEST_DIR has no parent")
.to_path_buf()
} else {
// When running with oss-fuzz
env::current_dir().expect("Failed to get current directory")
}
}

///////////////////////////////////////////////////////////////////////////////
// CKZG Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: c_kzg::KzgSettings = {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
c_kzg::KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
}
Expand Down Expand Up @@ -48,8 +65,7 @@ impl SafeEthKzgContext {
static CONSTANTINE_CTX: OnceLock<Arc<SafeEthKzgContext>> = OnceLock::new();

fn initialize_constantine_ctx() -> Arc<SafeEthKzgContext> {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
let eth_kzg_context =
constantine::EthKzgContext::load_trusted_setup(&trusted_setup_file).unwrap();
Arc::new(SafeEthKzgContext::new(eth_kzg_context))
Expand Down
29 changes: 27 additions & 2 deletions fuzz/fuzz_targets/fuzz_compute_cells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,42 @@ use c_kzg::KzgSettings;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use rust_eth_kzg::DASContext;
use std::env;
use std::path::PathBuf;

///////////////////////////////////////////////////////////////////////////////
// Helper Functions
///////////////////////////////////////////////////////////////////////////////

fn get_root_dir() -> PathBuf {
if let Ok(manifest) = env::var("CARGO_MANIFEST_DIR") {
// When running locally
PathBuf::from(manifest)
.parent()
.expect("CARGO_MANIFEST_DIR has no parent")
.to_path_buf()
} else {
// When running with oss-fuzz
env::current_dir().expect("Failed to get current directory")
}
}

///////////////////////////////////////////////////////////////////////////////
// Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: KzgSettings = {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
KzgSettings::load_trusted_setup_file(&trusted_setup_file, 8).unwrap()
};
static ref DAS_CONTEXT: DASContext = DASContext::default();
}

///////////////////////////////////////////////////////////////////////////////
// Fuzz Target
///////////////////////////////////////////////////////////////////////////////

fuzz_target!(|blob: Blob| {
let ckzg_result = KZG_SETTINGS.compute_cells(&blob);
let rkzg_result = DAS_CONTEXT.compute_cells(&blob.into_inner());
Expand Down
29 changes: 27 additions & 2 deletions fuzz/fuzz_targets/fuzz_compute_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,42 @@ use c_kzg::KzgSettings;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use rust_eth_kzg::DASContext;
use std::env;
use std::path::PathBuf;

///////////////////////////////////////////////////////////////////////////////
// Helper Functions
///////////////////////////////////////////////////////////////////////////////

fn get_root_dir() -> PathBuf {
if let Ok(manifest) = env::var("CARGO_MANIFEST_DIR") {
// When running locally
PathBuf::from(manifest)
.parent()
.expect("CARGO_MANIFEST_DIR has no parent")
.to_path_buf()
} else {
// When running with oss-fuzz
env::current_dir().expect("Failed to get current directory")
}
}

///////////////////////////////////////////////////////////////////////////////
// Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: KzgSettings = {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
KzgSettings::load_trusted_setup_file(&trusted_setup_file, 8).unwrap()
};
static ref DAS_CONTEXT: DASContext = DASContext::default();
}

///////////////////////////////////////////////////////////////////////////////
// Fuzz Target
///////////////////////////////////////////////////////////////////////////////

fuzz_target!(|blob: Blob| {
let ckzg_result = KZG_SETTINGS.compute_cells_and_kzg_proofs(&blob);
let rkzg_result = DAS_CONTEXT.compute_cells_and_kzg_proofs(&blob.into_inner());
Expand Down
24 changes: 20 additions & 4 deletions fuzz/fuzz_targets/fuzz_compute_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,34 @@ use arbitrary::Arbitrary;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use std::cell::UnsafeCell;
use std::env;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};

///////////////////////////////////////////////////////////////////////////////
// Helper Functions
///////////////////////////////////////////////////////////////////////////////

fn get_root_dir() -> PathBuf {
if let Ok(manifest) = env::var("CARGO_MANIFEST_DIR") {
// When running locally
PathBuf::from(manifest)
.parent()
.expect("CARGO_MANIFEST_DIR has no parent")
.to_path_buf()
} else {
// When running with oss-fuzz
env::current_dir().expect("Failed to get current directory")
}
}

///////////////////////////////////////////////////////////////////////////////
// CKZG Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: c_kzg::KzgSettings = {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
c_kzg::KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
}
Expand Down Expand Up @@ -48,8 +65,7 @@ impl SafeEthKzgContext {
static CONSTANTINE_CTX: OnceLock<Arc<SafeEthKzgContext>> = OnceLock::new();

fn initialize_constantine_ctx() -> Arc<SafeEthKzgContext> {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
let eth_kzg_context =
constantine::EthKzgContext::load_trusted_setup(&trusted_setup_file).unwrap();
Arc::new(SafeEthKzgContext::new(eth_kzg_context))
Expand Down
29 changes: 27 additions & 2 deletions fuzz/fuzz_targets/fuzz_recover_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,42 @@ use c_kzg::BYTES_PER_CELL;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use rust_eth_kzg::DASContext;
use std::env;
use std::path::PathBuf;

///////////////////////////////////////////////////////////////////////////////
// Helper Functions
///////////////////////////////////////////////////////////////////////////////

fn get_root_dir() -> PathBuf {
if let Ok(manifest) = env::var("CARGO_MANIFEST_DIR") {
// When running locally
PathBuf::from(manifest)
.parent()
.expect("CARGO_MANIFEST_DIR has no parent")
.to_path_buf()
} else {
// When running with oss-fuzz
env::current_dir().expect("Failed to get current directory")
}
}

///////////////////////////////////////////////////////////////////////////////
// Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: KzgSettings = {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
static ref DAS_CONTEXT: DASContext = DASContext::default();
}

///////////////////////////////////////////////////////////////////////////////
// Fuzz Target
///////////////////////////////////////////////////////////////////////////////

#[derive(Arbitrary, Debug)]
struct Input {
cell_indices: Vec<u64>,
Expand Down
24 changes: 20 additions & 4 deletions fuzz/fuzz_targets/fuzz_verify_blob_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,34 @@ use arbitrary::Arbitrary;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use std::cell::UnsafeCell;
use std::env;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};

///////////////////////////////////////////////////////////////////////////////
// Helper Functions
///////////////////////////////////////////////////////////////////////////////

fn get_root_dir() -> PathBuf {
if let Ok(manifest) = env::var("CARGO_MANIFEST_DIR") {
// When running locally
PathBuf::from(manifest)
.parent()
.expect("CARGO_MANIFEST_DIR has no parent")
.to_path_buf()
} else {
// When running with oss-fuzz
env::current_dir().expect("Failed to get current directory")
}
}

///////////////////////////////////////////////////////////////////////////////
// CKZG Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: c_kzg::KzgSettings = {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
c_kzg::KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
}
Expand Down Expand Up @@ -48,8 +65,7 @@ impl SafeEthKzgContext {
static CONSTANTINE_CTX: OnceLock<Arc<SafeEthKzgContext>> = OnceLock::new();

fn initialize_constantine_ctx() -> Arc<SafeEthKzgContext> {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
let eth_kzg_context =
constantine::EthKzgContext::load_trusted_setup(&trusted_setup_file).unwrap();
Arc::new(SafeEthKzgContext::new(eth_kzg_context))
Expand Down
Loading

0 comments on commit 5e30d44

Please sign in to comment.