From c90da636fa52707aed5c4e3b7dc27c42e49295df Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Fri, 17 Nov 2023 06:42:30 -0500 Subject: [PATCH] Eliminate use of `base` directory (e.g., `OUT_DIR`) --- cargo-afl/src/common.rs | 27 +++++++++++---------------- cargo-afl/src/config.rs | 26 +++++++++++++------------- cargo-afl/src/main.rs | 10 +++++----- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/cargo-afl/src/common.rs b/cargo-afl/src/common.rs index 1168c7081..f57aa3819 100644 --- a/cargo-afl/src/common.rs +++ b/cargo-afl/src/common.rs @@ -9,7 +9,7 @@ fn xdg_dir(toolchain: Option<&str>) -> xdg::BaseDirectories { xdg::BaseDirectories::with_prefix(prefix).unwrap() } -fn data_dir(base: Option<&Path>, toolchain: Option<&str>, dir_name: &str) -> PathBuf { +fn data_dir(toolchain: Option<&str>, dir_name: &str) -> PathBuf { // For docs.rs builds, use OUT_DIR. // For other cases, use a XDG data directory. // It is necessary to use OUT_DIR for docs.rs builds, @@ -17,13 +17,8 @@ fn data_dir(base: Option<&Path>, toolchain: Option<&str>, dir_name: &str) -> Pat // The Cargo documentation recommends that build scripts // place their generated files at OUT_DIR too, but we // don't change that for now for normal builds. - if let Some(base) = base { - let path = base.join(dir_name); - std::fs::create_dir_all(&path).unwrap(); - path - } else { - xdg_dir(toolchain).create_data_directory(dir_name).unwrap() - } + // smoelius: AFL++ is no longer built on docs.rs. + xdg_dir(toolchain).create_data_directory(dir_name).unwrap() } const SHORT_COMMIT_HASH_LEN: usize = 7; @@ -69,24 +64,24 @@ fn pkg_version() -> String { #[allow(dead_code)] #[must_use] -pub fn afl_dir(base: Option<&Path>, toolchain: Option<&str>) -> PathBuf { - data_dir(base, toolchain, "afl") +pub fn afl_dir(toolchain: Option<&str>) -> PathBuf { + data_dir(toolchain, "afl") } #[allow(dead_code)] #[must_use] -pub fn afl_llvm_dir(base: Option<&Path>, toolchain: Option<&str>) -> PathBuf { - data_dir(base, toolchain, "afl-llvm") +pub fn afl_llvm_dir(toolchain: Option<&str>) -> PathBuf { + data_dir(toolchain, "afl-llvm") } #[allow(dead_code)] #[must_use] -pub fn object_file_path(base: Option<&Path>, toolchain: Option<&str>) -> PathBuf { - afl_llvm_dir(base, toolchain).join("libafl-llvm-rt.o") +pub fn object_file_path(toolchain: Option<&str>) -> PathBuf { + afl_llvm_dir(toolchain).join("libafl-llvm-rt.o") } #[allow(dead_code)] #[must_use] -pub fn archive_file_path(base: Option<&Path>, toolchain: Option<&str>) -> PathBuf { - afl_llvm_dir(base, toolchain).join("libafl-llvm-rt.a") +pub fn archive_file_path(toolchain: Option<&str>) -> PathBuf { + afl_llvm_dir(toolchain).join("libafl-llvm-rt.a") } diff --git a/cargo-afl/src/config.rs b/cargo-afl/src/config.rs index 28b347c43..30194ccba 100644 --- a/cargo-afl/src/config.rs +++ b/cargo-afl/src/config.rs @@ -44,7 +44,7 @@ pub struct Args { pub fn build(args: &Args) { assert!(args.default || args.toolchain.is_some()); - if !args.force && common::archive_file_path(None, args.toolchain.as_deref()).exists() { + if !args.force && common::archive_file_path(args.toolchain.as_deref()).exists() { let version = common::afl_rustc_version(args.toolchain.as_deref()); eprintln!( "AFL LLVM runtime was already built for Rust {version}; run `cargo \ @@ -78,23 +78,23 @@ pub fn build(args: &Args) { let work_dir = tempdir.path(); - build_afl(args, work_dir, None); - build_afl_llvm_runtime(args, work_dir, None); + build_afl(args, work_dir); + build_afl_llvm_runtime(args, work_dir); if args.plugins { - copy_afl_llvm_plugins(args, work_dir, None); + copy_afl_llvm_plugins(args, work_dir); } eprintln!( "Artifacts written to {}", - common::afl_dir(None, args.toolchain.as_deref()) + common::afl_dir(args.toolchain.as_deref()) .parent() .unwrap() .display() ) } -fn build_afl(args: &Args, work_dir: &Path, base: Option<&Path>) { +fn build_afl(args: &Args, work_dir: &Path) { // if you had already installed cargo-afl previously you **must** clean AFL++ let mut command = Command::new("make"); command @@ -102,7 +102,7 @@ fn build_afl(args: &Args, work_dir: &Path, base: Option<&Path>) { .args(["clean", "install"]) // skip the checks for the legacy x86 afl-gcc compiler .env("AFL_NO_X86", "1") - .env("DESTDIR", common::afl_dir(base, args.toolchain.as_deref())) + .env("DESTDIR", common::afl_dir(args.toolchain.as_deref())) .env("PREFIX", "") .env_remove("DEBUG"); @@ -126,23 +126,23 @@ fn build_afl(args: &Args, work_dir: &Path, base: Option<&Path>) { assert!(status.success()); } -fn build_afl_llvm_runtime(args: &Args, work_dir: &Path, base: Option<&Path>) { +fn build_afl_llvm_runtime(args: &Args, work_dir: &Path) { std::fs::copy( work_dir.join("afl-compiler-rt.o"), - common::object_file_path(base, args.toolchain.as_deref()), + common::object_file_path(args.toolchain.as_deref()), ) .expect("Couldn't copy object file"); let status = Command::new(AR_CMD) .arg("r") - .arg(common::archive_file_path(base, args.toolchain.as_deref())) - .arg(common::object_file_path(base, args.toolchain.as_deref())) + .arg(common::archive_file_path(args.toolchain.as_deref())) + .arg(common::object_file_path(args.toolchain.as_deref())) .status() .expect("could not run 'ar'"); assert!(status.success()); } -fn copy_afl_llvm_plugins(args: &Args, work_dir: &Path, base: Option<&Path>) { +fn copy_afl_llvm_plugins(args: &Args, work_dir: &Path) { // Iterate over the files in the directory. for result in work_dir.read_dir().unwrap() { let entry = result.unwrap(); @@ -153,7 +153,7 @@ fn copy_afl_llvm_plugins(args: &Args, work_dir: &Path, base: Option<&Path>) { // Attempt to copy the shared object file. std::fs::copy( work_dir.join(&file_name), - common::afl_llvm_dir(base, args.toolchain.as_deref()).join(&file_name), + common::afl_llvm_dir(args.toolchain.as_deref()).join(&file_name), ) .unwrap_or_else(|error| { panic!("Couldn't copy shared object file {file_name:?}: {error}") diff --git a/cargo-afl/src/main.rs b/cargo-afl/src/main.rs index 3bfd10aea..a1cb49d97 100644 --- a/cargo-afl/src/main.rs +++ b/cargo-afl/src/main.rs @@ -123,7 +123,7 @@ fn main() { }; if !matches!(afl_args.subcmd, Some(AflSubcommand::Config(..))) - && !common::archive_file_path(None, None).exists() + && !common::archive_file_path(None).exists() { let version = common::afl_rustc_version(None); eprintln!( @@ -182,7 +182,7 @@ where S: AsRef, { let no_sudo = env::var("NO_SUDO").is_ok(); - let cmd_path = common::afl_dir(None, None).join("bin").join(tool); + let cmd_path = common::afl_dir(None).join("bin").join(tool); let mut cmd = if !no_sudo && tool == "afl-system-config" { let mut cmd = Command::new("sudo"); cmd.args([OsStr::new("--reset-timestamp"), cmd_path.as_os_str()]); @@ -238,7 +238,7 @@ where // `-C codegen-units=1` is needed to work around link errors // https://github.com/rust-fuzz/afl.rs/pull/193#issuecomment-933550430 - let binding = common::afl_llvm_dir(None, None); + let binding = common::afl_llvm_dir(None); let p = binding.display(); let mut rustflags = format!( @@ -304,7 +304,7 @@ where rustflags.push_str(&format!( "-l afl-llvm-rt \ -L {} ", - common::afl_llvm_dir(None, None).display() + common::afl_llvm_dir(None).display() )); // add user provided flags @@ -332,7 +332,7 @@ fn is_nightly() -> bool { } fn plugins_available() -> bool { - let afl_llvm_dir = common::afl_llvm_dir(None, None); + let afl_llvm_dir = common::afl_llvm_dir(None); for result in afl_llvm_dir.read_dir().unwrap() { let entry = result.unwrap(); let file_name = entry.file_name();