Skip to content

Commit

Permalink
Print error message if archive file can't be found. (#124)
Browse files Browse the repository at this point in the history
Fixes #122.
  • Loading branch information
frewsxcv committed Nov 25, 2017
1 parent 2cf8544 commit d65c9cb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
19 changes: 8 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use std::process::Command;

static AFL_SRC_PATH: &str = "afl-2.52b";

#[path = "src/dirs.rs"]
mod dirs;
#[path = "src/common.rs"]
mod common;

fn main() {
build_afl(&dirs::afl());
build_afl_llvm_runtime(&dirs::afl_llvm_rt());
build_afl(&common::afl_dir());
build_afl_llvm_runtime();
}

fn build_afl(out_dir: &Path) {
Expand All @@ -29,10 +29,7 @@ fn build_afl(out_dir: &Path) {
assert!(status.success());
}

fn build_afl_llvm_runtime(out_dir: &Path) {
let object_file_path = out_dir.join("libafl-llvm-rt.o");
let archive_file_path = out_dir.join("libafl-llvm-rt.a");

fn build_afl_llvm_runtime() {
let status = Command::new("cc")
.current_dir(AFL_SRC_PATH)
.arg("-c")
Expand All @@ -41,15 +38,15 @@ fn build_afl_llvm_runtime(out_dir: &Path) {
.arg("-fno-omit-frame-pointer")
.arg("llvm_mode/afl-llvm-rt.o.c")
.arg("-fpermissive")
.args(&[OsStr::new("-o"), object_file_path.as_os_str()])
.args(&[OsStr::new("-o"), common::object_file_path().as_os_str()])
.status()
.expect("could not run 'gcc'");
assert!(status.success());

let status = Command::new("ar")
.arg("r")
.arg(archive_file_path)
.arg(object_file_path)
.arg(common::archive_file_path())
.arg(common::object_file_path())
.status()
.expect("could not run 'ar'");
assert!(status.success());
Expand Down
15 changes: 11 additions & 4 deletions src/bin/cargo-afl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ use std::env;
use std::ffi::OsStr;
use std::process::{self, Command};

#[path = "../dirs.rs"]
mod dirs;
#[path = "../common.rs"]
mod common;

fn main() {
if !common::archive_file_path().exists() {
let version = common::rustc_version();
eprintln!("AFL LLVM runtime is not built with Rust {}, run `cargo \
install --force afl` to build it.", version);
process::exit(1);
}

let _ = clap_app().get_matches();

let mut args = env::args().skip(2).peekable(); // skip `cargo` and `afl`
Expand Down Expand Up @@ -127,7 +134,7 @@ where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let cmd_path = dirs::afl().join("bin").join(cmd);
let cmd_path = common::afl_dir().join("bin").join(cmd);
let status = Command::new(cmd_path)
.args(args.into_iter().skip(1)) // skip afl sub-command
.status()
Expand All @@ -149,7 +156,7 @@ where
-C panic=abort \
-l afl-llvm-rt \
-L {}",
dirs::afl_llvm_rt().display()
common::afl_llvm_rt_dir().display()
);
let status = Command::new(cargo_path)
.args(args) // skip `cargo` and `afl`
Expand Down
15 changes: 12 additions & 3 deletions src/dirs.rs → src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn xdg_dir() -> xdg::BaseDirectories {
xdg::BaseDirectories::with_prefix(prefix).unwrap()
}

fn rustc_version() -> String {
pub fn rustc_version() -> String {
let mut ret = String::from("rustc-");
ret.push_str(&rustc_version::version().unwrap().to_string());
ret
Expand All @@ -26,10 +26,19 @@ fn pkg_version() -> String {
ret
}

pub fn afl() -> PathBuf {
pub fn afl_dir() -> PathBuf {
xdg_dir().create_data_directory("afl").unwrap()
}

pub fn afl_llvm_rt() -> PathBuf {
pub fn afl_llvm_rt_dir() -> PathBuf {
xdg_dir().create_data_directory("afl-llvm-rt").unwrap()
}

#[allow(dead_code)]
pub fn object_file_path() -> PathBuf {
afl_llvm_rt_dir().join("libafl-llvm-rt.o")
}

pub fn archive_file_path() -> PathBuf {
afl_llvm_rt_dir().join("libafl-llvm-rt.a")
}

0 comments on commit d65c9cb

Please sign in to comment.