From d723f18ee183a6f7d060198def964c33c7494357 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 18 Sep 2024 08:49:08 +0100 Subject: [PATCH 1/3] Disable LTO Enabling LTO results in link errors, see #384 for more info. --- src/project.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/project.rs b/src/project.rs index c62bfe9..a75a344 100644 --- a/src/project.rs +++ b/src/project.rs @@ -142,7 +142,15 @@ impl FuzzProject { .arg(&build.triple); // we default to release mode unless debug mode is explicitly requested if !build.dev { - cmd.args(["--release", "--config", "profile.release.debug=true"]); + cmd.args([ + "--release", + "--config", + "profile.release.debug=true", + "--config", + "profile.release.lto=false", + ]); + } else { + cmd.args(["--config", "profile.dev.lto=false"]); } if build.verbose { cmd.arg("--verbose"); From 971aee9e02bc3fb3e3857fc2fef9d0fcd1f6ebda Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 18 Sep 2024 08:59:02 +0100 Subject: [PATCH 2/3] Refactor determination of profile name --- src/options.rs | 11 +++++++++++ src/project.rs | 25 ++++++++++++------------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/options.rs b/src/options.rs index 677b0e7..677b632 100644 --- a/src/options.rs +++ b/src/options.rs @@ -167,6 +167,17 @@ pub struct BuildOptions { pub disable_branch_folding: Option, } +impl BuildOptions { + pub fn profile_name(&self) -> &str { + // we default to release mode unless debug mode is explicitly requested + if !self.dev { + "release" + } else { + "dev" + } + } +} + impl stdfmt::Display for BuildOptions { fn fmt(&self, f: &mut stdfmt::Formatter) -> stdfmt::Result { if self.dev { diff --git a/src/project.rs b/src/project.rs index a75a344..2a7b72a 100644 --- a/src/project.rs +++ b/src/project.rs @@ -140,18 +140,16 @@ impl FuzzProject { // --target= won't pass rustflags to build scripts .arg("--target") .arg(&build.triple); - // we default to release mode unless debug mode is explicitly requested - if !build.dev { - cmd.args([ - "--release", - "--config", - "profile.release.debug=true", - "--config", - "profile.release.lto=false", - ]); - } else { - cmd.args(["--config", "profile.dev.lto=false"]); - } + let profile = build.profile_name(); + + cmd.args([ + &format!("--profile={profile}"), + "--config", + &format!("profile.{profile}.debug=true"), + "--config", + &format!("profile.{profile}.lto=false"), + ]); + if build.verbose { cmd.arg("--verbose"); } @@ -748,7 +746,8 @@ impl FuzzProject { corpus_dir: &Path, ) -> Result<(Command, tempfile::TempDir)> { let bin_path = { - let profile_subdir = if coverage.build.dev { + // See https://doc.rust-lang.org/cargo/guide/build-cache.html for the mapping. + let profile_subdir = if coverage.build.profile_name() == "dev" { "debug" } else { "release" From f556347a7820f1dddb6a0eb5f5a11f4a580acb88 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 18 Sep 2024 09:10:43 +0100 Subject: [PATCH 3/3] Allow use of custom profiles --- src/options.rs | 23 +++++++++++++++++++---- src/project.rs | 8 ++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/options.rs b/src/options.rs index 677b632..a653ad9 100644 --- a/src/options.rs +++ b/src/options.rs @@ -50,14 +50,17 @@ pub enum BuildMode { #[derive(Clone, Debug, Eq, PartialEq, Parser)] pub struct BuildOptions { - #[arg(short = 'D', long, conflicts_with = "release")] + #[arg(short = 'D', long, conflicts_with_all = ["release", "profile"])] /// Build artifacts in development mode, without optimizations pub dev: bool, - #[arg(short = 'O', long, conflicts_with = "dev")] + #[arg(short = 'O', long, conflicts_with_all = ["dev", "profile"])] /// Build artifacts in release mode, with optimizations pub release: bool, + #[arg(long, conflicts_with_all = ["dev", "release"])] + pub profile: Option, + #[arg(short = 'a', long)] /// Build artifacts with debug assertions and overflow checks enabled (default if not -O) pub debug_assertions: bool, @@ -169,8 +172,11 @@ pub struct BuildOptions { impl BuildOptions { pub fn profile_name(&self) -> &str { - // we default to release mode unless debug mode is explicitly requested - if !self.dev { + // if no explicit profile is given then we default to release + // mode unless debug mode is explicitly requested. + if let Some(profile) = &self.profile { + profile + } else if !self.dev { "release" } else { "dev" @@ -188,6 +194,10 @@ impl stdfmt::Display for BuildOptions { write!(f, " -O")?; } + if let Some(profile) = &self.profile { + write!(f, " --profile {profile}")?; + } + if self.debug_assertions { write!(f, " -a")?; } @@ -260,6 +270,7 @@ mod test { let default_opts = BuildOptions { dev: false, release: false, + profile: None, debug_assertions: false, verbose: false, no_default_features: false, @@ -288,6 +299,10 @@ mod test { release: true, ..default_opts.clone() }, + BuildOptions { + profile: Some("fuzz".to_string()), + ..default_opts.clone() + }, BuildOptions { debug_assertions: true, ..default_opts.clone() diff --git a/src/project.rs b/src/project.rs index 2a7b72a..1afd983 100644 --- a/src/project.rs +++ b/src/project.rs @@ -747,10 +747,10 @@ impl FuzzProject { ) -> Result<(Command, tempfile::TempDir)> { let bin_path = { // See https://doc.rust-lang.org/cargo/guide/build-cache.html for the mapping. - let profile_subdir = if coverage.build.profile_name() == "dev" { - "debug" - } else { - "release" + let profile_subdir = match coverage.build.profile_name() { + "dev" | "test" => "debug", + "release" | "bench" => "release", + other => other, }; let target_dir = self