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

Expose Environment Variables during Build #387

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
50 changes: 50 additions & 0 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ use std::{

const DEFAULT_FUZZ_DIR: &str = "fuzz";

/// The name of the environment variable that is exposed to indicate a
/// cargo-fuzz build is occurring.
const BUILD_ENV_FLAG: &str = "CARGO_FUZZ";
cwshugg marked this conversation as resolved.
Show resolved Hide resolved

/// The name of the environment variable that exposes the cargo fuzz manifest
/// path during builds.
const BUILD_ENV_MANIFEST_DIR: &str = "CARGO_FUZZ_MANIFEST";

pub struct FuzzProject {
/// The project with fuzz targets
fuzz_dir: PathBuf,
Expand Down Expand Up @@ -320,6 +328,39 @@ impl FuzzProject {
}
}

// Helper function for `exec_build()` used to expose cargo-fuzz information
// via environment variables. Such environment variables can be used by fuzz
// target dependencies' build scripts to detect whether or not cargo-fuzz is
// responsible for the build.
//
// This is called directly before the `cargo build ...` command is executed.
fn build_env_expose(
&self,
_mode: options::BuildMode,
_build: &options::BuildOptions,
_fuzz_target: Option<&str>,
) -> Result<()> {
// expose a flag environment variable to allow the detection of cargo-fuzz
env::set_var(BUILD_ENV_FLAG, "1");

// expose the path to the cargo-fuzz manifest
let manifest_path = self.manifest_path();
env::set_var(BUILD_ENV_MANIFEST_DIR, manifest_path.as_os_str());
cwshugg marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

// Helper function for `exec_build()` used to un-expose cargo-fuzz
// information that was previously exposed in environment variables during
// `build_env_expose()`.
//
// This is called directly after the `cargo build ...` command is executed.
fn build_env_unexpose(&self) -> Result<()> {
env::remove_var(BUILD_ENV_FLAG);
env::remove_var(BUILD_ENV_MANIFEST_DIR);
Ok(())
}

pub fn exec_build(
&self,
mode: options::BuildMode,
Expand All @@ -342,13 +383,22 @@ impl FuzzProject {
cmd.arg("--target-dir").arg(target_dir);
}

// expose build information via environment variables, before executing
// the build command
self.build_env_expose(mode, build, fuzz_target)
.expect("Failed to set cargo-fuzz build environment variables.");

let status = cmd
.status()
.with_context(|| format!("failed to execute: {:?}", cmd))?;
if !status.success() {
bail!("failed to build fuzz script: {:?}", cmd);
}

// un-expose build information, after the command has finished
self.build_env_unexpose()
.expect("Failed to un-set cargo-fuzz build environment variables.");

Ok(())
}

Expand Down
Loading