From 7b2da1bd22272b4bd38562374825336fb98f1b38 Mon Sep 17 00:00:00 2001 From: Bryce Van Dyk Date: Sun, 21 Oct 2018 14:54:25 -0400 Subject: [PATCH] Set RUSTDOCFLAGS to avoid issues with linkage for doctests. Per issue #142, `cargo afl test` and other commands that would run doctests would encounter issues with linkage. This commit changes `cargo afl` to set RUSTDOCFLAGS so rustdoc will be able to find the required libs. Further options are passed to ensure doctests are built and run in as similar a fashion to other code. This avoids other gotchas like cfg fuzzing being set for normal builds, but not for doctests. --- src/bin/cargo-afl.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/bin/cargo-afl.rs b/src/bin/cargo-afl.rs index fa85ec56c..d96fd6a5a 100644 --- a/src/bin/cargo-afl.rs +++ b/src/bin/cargo-afl.rs @@ -172,12 +172,32 @@ where common::afl_llvm_rt_dir().display() ); + // RUSTFLAGS are not used by rustdoc, instead RUSTDOCFLAGS are used. Since + // doctests will try to link against afl-llvm-rt, set up RUSTDOCFLAGS to + // have doctests built the same as other code to avoid issues with doctests. + let mut rustdocflags = format!( + "--cfg fuzzing \ + -C debug-assertions \ + -C overflow_checks \ + -C passes=sancov \ + -C llvm-args=-sanitizer-coverage-level=3 \ + -C llvm-args=-sanitizer-coverage-trace-pc-guard \ + -C llvm-args=-sanitizer-coverage-prune-blocks=0 \ + -C opt-level=3 \ + -C target-cpu=native \ + -C debuginfo=0 \ + -L {} ", + common::afl_llvm_rt_dir().display() + ); + // add user provided flags rustflags.push_str(&env::var("RUSTFLAGS").unwrap_or_default()); + rustdocflags.push_str(&env::var("RUSTDOCFLAGS").unwrap_or_default()); let status = Command::new(cargo_path) .args(args) // skip `cargo` and `afl` .env("RUSTFLAGS", &rustflags) + .env("RUSTDOCFLAGS", &rustdocflags) .env("ASAN_OPTIONS", asan_options) .env("TSAN_OPTIONS", tsan_options) .status()