Skip to content

Commit

Permalink
Eliminate use of base directory (e.g., OUT_DIR)
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Nov 17, 2023
1 parent 6b0662e commit 1404f59
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
27 changes: 11 additions & 16 deletions cargo-afl/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ 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,
// as that is the only place where we can write to.
// 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;
Expand Down Expand Up @@ -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")
}
24 changes: 12 additions & 12 deletions cargo-afl/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Args {
}

pub fn build(args: &Args) {
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 \
Expand Down Expand Up @@ -64,23 +64,23 @@ pub fn build(args: &Args) {
#[cfg(unix)]
let _file = sys::lock_path(work_dir).unwrap();

build_afl(args, work_dir, None, args.toolchain.as_deref());
build_afl_llvm_runtime(work_dir, None, args.toolchain.as_deref());
build_afl(args, work_dir, args.toolchain.as_deref());
build_afl_llvm_runtime(work_dir, args.toolchain.as_deref());

if args.plugins {
copy_afl_llvm_plugins(work_dir, None, args.toolchain.as_deref());
copy_afl_llvm_plugins(work_dir, args.toolchain.as_deref());
}
}

fn build_afl(args: &Args, work_dir: &Path, base: Option<&Path>, toolchain: Option<&str>) {
fn build_afl(args: &Args, work_dir: &Path, toolchain: Option<&str>) {
// if you had already installed cargo-afl previously you **must** clean AFL++
let mut command = Command::new("make");
command
.current_dir(work_dir)
.args(["clean", "install"])
// skip the checks for the legacy x86 afl-gcc compiler
.env("AFL_NO_X86", "1")
.env("DESTDIR", common::afl_dir(base, toolchain))
.env("DESTDIR", common::afl_dir(toolchain))
.env("PREFIX", "")
.env_remove("DEBUG");

Expand All @@ -99,23 +99,23 @@ fn build_afl(args: &Args, work_dir: &Path, base: Option<&Path>, toolchain: Optio
assert!(status.success());
}

fn build_afl_llvm_runtime(work_dir: &Path, base: Option<&Path>, toolchain: Option<&str>) {
fn build_afl_llvm_runtime(work_dir: &Path, toolchain: Option<&str>) {
std::fs::copy(
work_dir.join("afl-compiler-rt.o"),
common::object_file_path(base, toolchain),
common::object_file_path(toolchain),
)
.expect("Couldn't copy object file");

let status = Command::new(AR_CMD)
.arg("r")
.arg(common::archive_file_path(base, toolchain))
.arg(common::object_file_path(base, toolchain))
.arg(common::archive_file_path(toolchain))
.arg(common::object_file_path(toolchain))
.status()
.expect("could not run 'ar'");
assert!(status.success());
}

fn copy_afl_llvm_plugins(work_dir: &Path, base: Option<&Path>, toolchain: Option<&str>) {
fn copy_afl_llvm_plugins(work_dir: &Path, toolchain: Option<&str>) {
// Iterate over the files in the directory.
for result in work_dir.read_dir().unwrap() {
let entry = result.unwrap();
Expand All @@ -126,7 +126,7 @@ fn copy_afl_llvm_plugins(work_dir: &Path, base: Option<&Path>, toolchain: Option
// Attempt to copy the shared object file.
std::fs::copy(
work_dir.join(&file_name),
common::afl_llvm_dir(base, toolchain).join(&file_name),
common::afl_llvm_dir(toolchain).join(&file_name),
)
.unwrap_or_else(|error| {
panic!("Couldn't copy shared object file {file_name:?}: {error}")
Expand Down
10 changes: 5 additions & 5 deletions cargo-afl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,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!(
Expand Down Expand Up @@ -179,7 +179,7 @@ where
S: AsRef<OsStr>,
{
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()]);
Expand Down Expand Up @@ -235,7 +235,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!(
Expand Down Expand Up @@ -301,7 +301,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
Expand Down Expand Up @@ -329,7 +329,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();
Expand Down

0 comments on commit 1404f59

Please sign in to comment.