-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fuzzing] per-target binary for integration with google oss-fuzz
Google OSS-Fuzz project (google allow open source projects to be fuzzed by them) requires that each fuzzer be its own binary. To avoid having to maintain additional files, and having to have to add a new binary file everytime one adds a fuzzer, this intelligently creates a binary based on an env variable. How does it work? 1. a build file retrieves an environment variable telling it what fuzzer to create 2. it generates a file in `OUT_DIR` that contains the name of the fuzzer 3. that file is included in the binary code, and used to figure out what's the target Check the README for more information. Closes: diem#2721 Approved by: sunshowers
- Loading branch information
1 parent
460001f
commit 82993d8
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) The Libra Core Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::{env, fs::File, io::prelude::*, path::PathBuf}; | ||
|
||
fn main() { | ||
// needed to build different binaries based on env var | ||
println!("cargo:rerun-if-env-changed=SINGLE_FUZZ_TARGET"); | ||
println!("cargo:rerun-if-env-changed=RUSTFLAGS"); | ||
let fuzz_target = match env::var("SINGLE_FUZZ_TARGET") { | ||
Ok(x) => x, | ||
// default value for build to work | ||
Err(_) => "vm_value".to_string(), | ||
}; | ||
|
||
// fuzzer file to write | ||
let fuzzer_content = format!( | ||
"const FUZZ_TARGET: &str = \"{fuzz_target}\";", | ||
fuzz_target = fuzz_target, | ||
); | ||
|
||
// path of file to create (OUT_DIR/fuzzer.rs) | ||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
let out_path = out_path.join("fuzzer.rs"); | ||
|
||
// write to file | ||
let mut file = File::create(out_path).unwrap(); | ||
file.write_all(fuzzer_content.as_bytes()).unwrap(); | ||
} |
15 changes: 15 additions & 0 deletions
15
testsuite/libra-fuzzer/fuzz/google-oss-fuzz/fuzzer_builder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) The Libra Core Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![no_main] | ||
|
||
use libfuzzer_sys::fuzz_target; | ||
use libra_fuzzer::FuzzTarget; | ||
|
||
// contains FUZZ_TARGET | ||
include!(concat!(env!("OUT_DIR"), "/fuzzer.rs")); | ||
|
||
fuzz_target!(|data: &[u8]| { | ||
let fuzzer = FuzzTarget::by_name(FUZZ_TARGET).unwrap(); | ||
fuzzer.fuzz(data); | ||
}); |