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 18, 2023
1 parent fc7f02d commit 65a6e71
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
27 changes: 11 additions & 16 deletions cargo-afl/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ fn xdg_dir() -> xdg::BaseDirectories {
xdg::BaseDirectories::with_prefix(prefix).unwrap()
}

fn data_dir(base: Option<&Path>, dir_name: &str) -> PathBuf {
fn data_dir(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().create_data_directory(dir_name).unwrap()
}
// smoelius: AFL++ is no longer built on docs.rs.
xdg_dir().create_data_directory(dir_name).unwrap()
}

const SHORT_COMMIT_HASH_LEN: usize = 7;
Expand Down Expand Up @@ -51,24 +46,24 @@ fn pkg_version() -> String {

#[allow(dead_code)]
#[must_use]
pub fn afl_dir(base: Option<&Path>) -> PathBuf {
data_dir(base, "afl")
pub fn afl_dir() -> PathBuf {
data_dir("afl")
}

#[allow(dead_code)]
#[must_use]
pub fn afl_llvm_dir(base: Option<&Path>) -> PathBuf {
data_dir(base, "afl-llvm")
pub fn afl_llvm_dir() -> PathBuf {
data_dir("afl-llvm")
}

#[allow(dead_code)]
#[must_use]
pub fn object_file_path(base: Option<&Path>) -> PathBuf {
afl_llvm_dir(base).join("libafl-llvm-rt.o")
pub fn object_file_path() -> PathBuf {
afl_llvm_dir().join("libafl-llvm-rt.o")
}

#[allow(dead_code)]
#[must_use]
pub fn archive_file_path(base: Option<&Path>) -> PathBuf {
afl_llvm_dir(base).join("libafl-llvm-rt.a")
pub fn archive_file_path() -> PathBuf {
afl_llvm_dir().join("libafl-llvm-rt.a")
}
26 changes: 13 additions & 13 deletions cargo-afl/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct Args {
}

pub fn config(args: &Args) {
if !args.force && common::archive_file_path(None).exists() {
if !args.force && common::archive_file_path().exists() {
let version = common::afl_rustc_version();
eprintln!(
"AFL LLVM runtime was already built for Rust {version}; run `cargo \
Expand Down Expand Up @@ -68,20 +68,20 @@ pub fn config(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).parent().unwrap().display()
common::afl_dir().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++
// smoelius: AFL++ is now copied to a temporary directory before being built. So `make clean`
// is no longer necessary.
Expand All @@ -91,7 +91,7 @@ fn build_afl(args: &Args, work_dir: &Path, base: Option<&Path>) {
.arg("install")
// skip the checks for the legacy x86 afl-gcc compiler
.env("AFL_NO_X86", "1")
.env("DESTDIR", common::afl_dir(base))
.env("DESTDIR", common::afl_dir())
.env("PREFIX", "")
.env_remove("DEBUG");

Expand All @@ -113,18 +113,18 @@ 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),
common::object_file_path(),
)
.expect("Couldn't copy object file");

let mut command = Command::new(AR_CMD);
command
.arg("r")
.arg(common::archive_file_path(base))
.arg(common::object_file_path(base));
.arg(common::archive_file_path())
.arg(common::object_file_path());

if !args.verbose {
command.stdout(Stdio::null());
Expand All @@ -135,7 +135,7 @@ fn build_afl_llvm_runtime(args: &Args, work_dir: &Path, base: Option<&Path>) {
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();
Expand All @@ -146,7 +146,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).join(&file_name),
common::afl_llvm_dir().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 @@ -123,7 +123,7 @@ fn main() {
};

if !matches!(afl_args.subcmd, Some(AflSubcommand::Config(..)))
&& !common::archive_file_path(None).exists()
&& !common::archive_file_path().exists()
{
let version = common::afl_rustc_version();
eprintln!(
Expand Down Expand Up @@ -182,7 +182,7 @@ where
S: AsRef<OsStr>,
{
let no_sudo = env::var("NO_SUDO").is_ok();
let cmd_path = common::afl_dir(None).join("bin").join(tool);
let cmd_path = common::afl_dir().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 @@ -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);
let binding = common::afl_llvm_dir();
let p = binding.display();

let mut rustflags = format!(
Expand Down Expand Up @@ -304,7 +304,7 @@ where
rustflags.push_str(&format!(
"-l afl-llvm-rt \
-L {} ",
common::afl_llvm_dir(None).display()
common::afl_llvm_dir().display()
));

// add user provided flags
Expand Down Expand Up @@ -332,7 +332,7 @@ fn is_nightly() -> bool {
}

fn plugins_available() -> bool {
let afl_llvm_dir = common::afl_llvm_dir(None);
let afl_llvm_dir = common::afl_llvm_dir();
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 65a6e71

Please sign in to comment.