Allow QEMU location and version to be specified in environment (#3226)

* Allow QEMU location and version to be specified in environment

* Rename things

* Change remote to url
This commit is contained in:
WorksButNotTested 2025-05-16 10:34:18 +01:00 committed by GitHub
parent 626812a7ce
commit b5ab6bad1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 16 deletions

View File

@ -9,9 +9,9 @@ use which::which;
use crate::cargo_add_rpath;
pub const QEMU_URL: &str = "https://github.com/AFLplusplus/qemu-libafl-bridge";
pub const QEMU_DIRNAME: &str = "qemu-libafl-bridge";
pub const QEMU_REVISION: &str = "0bea78a122b249cbffafdb130af04cc7331c9aee";
pub const LIBAFL_QEMU_GIT_URL: &str = "https://github.com/AFLplusplus/qemu-libafl-bridge";
pub const LIBAFL_QEMU_DIRNAME: &str = "qemu-libafl-bridge";
pub const LIBAFL_QEMU_GIT_REV: &str = "0bea78a122b249cbffafdb130af04cc7331c9aee";
pub struct BuildResult {
pub qemu_path: PathBuf,
@ -278,12 +278,18 @@ pub fn build(
env::var_os("LIBAFL_QEMU_CLONE_DIR").map(|x| x.to_string_lossy().to_string());
let libafl_qemu_force_configure = env::var("LIBAFL_QEMU_FORCE_CONFIGURE").is_ok();
let libafl_qemu_no_build = env::var("LIBAFL_QEMU_NO_BUILD").is_ok();
let libafl_qemu_git_url =
env::var_os("LIBAFL_QEMU_GIT_URL").map(|x| x.to_string_lossy().to_string());
let libafl_qemu_git_rev =
env::var_os("LIBAFL_QEMU_GIT_REV").map(|x| x.to_string_lossy().to_string());
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_DIR");
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_CLONE_DIR");
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_FORCE_BUILD");
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_FORCE_CONFIGURE");
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_NO_BUILD");
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_GIT_URL");
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_GIT_REV");
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = out_dir.to_string_lossy().to_string();
@ -299,29 +305,45 @@ pub fn build(
let cpp_compiler = cc::Build::new().cpp(true).get_compiler();
let libafl_qemu_dir = if let Some(qemu_dir) = libafl_qemu_dir.as_ref() {
if libafl_qemu_clone_dir.is_some() {
println!(
"cargo:warning=LIBAFL_QEMU_DIR and LIBAFL_QEMU_CLONE_DIR are both set. LIBAFL_QEMU_DIR will be considered in priority"
);
}
assert!(
libafl_qemu_clone_dir.is_none(),
"cargo:warning=LIBAFL_QEMU_DIR and LIBAFL_QEMU_CLONE_DIR are both set."
);
assert!(
libafl_qemu_git_url.is_none(),
"cargo:warning=LIBAFL_QEMU_DIR and LIBAFL_QEMU_GIT_URL are both set."
);
assert!(
libafl_qemu_git_rev.is_none(),
"cargo:warning=LIBAFL_QEMU_DIR and LIBAFL_QEMU_GIT_REV are both set."
);
Path::new(&qemu_dir).to_path_buf()
} else {
let qemu_path = if let Some(clone_dir) = &libafl_qemu_clone_dir {
PathBuf::from(clone_dir)
} else {
target_dir.join(QEMU_DIRNAME)
target_dir.join(LIBAFL_QEMU_DIRNAME)
};
let qemu_git_url = libafl_qemu_git_url
.as_deref()
.unwrap_or(LIBAFL_QEMU_GIT_URL);
let qemu_git_rev = libafl_qemu_git_rev
.as_deref()
.unwrap_or(LIBAFL_QEMU_GIT_REV);
let qemu_rev = target_dir.join("QEMU_REVISION");
if qemu_rev.exists()
&& fs::read_to_string(&qemu_rev).expect("Failed to read QEMU_REVISION") != QEMU_REVISION
&& fs::read_to_string(&qemu_rev).expect("Failed to read QEMU_REVISION") != qemu_git_rev
{
drop(fs::remove_dir_all(&qemu_path));
}
if !qemu_path.is_dir() {
println!("cargo:warning=Qemu not found, cloning with git ({QEMU_REVISION})...");
println!("cargo:warning=Qemu not found, cloning with git ({qemu_git_rev})...");
fs::create_dir_all(&qemu_path).unwrap();
assert!(
Command::new("git")
@ -337,7 +359,7 @@ pub fn build(
.arg("remote")
.arg("add")
.arg("origin")
.arg(QEMU_URL)
.arg(qemu_git_url)
.status()
.unwrap()
.success()
@ -349,7 +371,7 @@ pub fn build(
.arg("--depth")
.arg("1")
.arg("origin")
.arg(QEMU_REVISION)
.arg(qemu_git_rev)
.status()
.unwrap()
.success()
@ -363,7 +385,7 @@ pub fn build(
.unwrap()
.success()
);
fs::write(&qemu_rev, QEMU_REVISION).unwrap();
fs::write(&qemu_rev, qemu_git_rev).unwrap();
}
qemu_path

View File

@ -24,7 +24,7 @@ mod build;
pub use build::build;
#[rustversion::nightly]
use crate::build::QEMU_REVISION;
use crate::build::LIBAFL_QEMU_GIT_REV;
const LLVM_VERSION_MAX: i32 = 33;
@ -421,7 +421,7 @@ pub fn maybe_generate_stub_bindings(
None,
vec![
header.as_str(),
format!("/* qemu git hash: {QEMU_REVISION} */").as_str(),
format!("/* qemu git hash: {LIBAFL_QEMU_GIT_REV} */").as_str(),
],
false,
);