Skip to content

Commit b5ab6ba

Browse files
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
1 parent 626812a commit b5ab6ba

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

libafl_qemu/libafl_qemu_build/src/build.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use which::which;
99

1010
use crate::cargo_add_rpath;
1111

12-
pub const QEMU_URL: &str = "https://github.com/AFLplusplus/qemu-libafl-bridge";
13-
pub const QEMU_DIRNAME: &str = "qemu-libafl-bridge";
14-
pub const QEMU_REVISION: &str = "0bea78a122b249cbffafdb130af04cc7331c9aee";
12+
pub const LIBAFL_QEMU_GIT_URL: &str = "https://github.com/AFLplusplus/qemu-libafl-bridge";
13+
pub const LIBAFL_QEMU_DIRNAME: &str = "qemu-libafl-bridge";
14+
pub const LIBAFL_QEMU_GIT_REV: &str = "0bea78a122b249cbffafdb130af04cc7331c9aee";
1515

1616
pub struct BuildResult {
1717
pub qemu_path: PathBuf,
@@ -278,12 +278,18 @@ pub fn build(
278278
env::var_os("LIBAFL_QEMU_CLONE_DIR").map(|x| x.to_string_lossy().to_string());
279279
let libafl_qemu_force_configure = env::var("LIBAFL_QEMU_FORCE_CONFIGURE").is_ok();
280280
let libafl_qemu_no_build = env::var("LIBAFL_QEMU_NO_BUILD").is_ok();
281+
let libafl_qemu_git_url =
282+
env::var_os("LIBAFL_QEMU_GIT_URL").map(|x| x.to_string_lossy().to_string());
283+
let libafl_qemu_git_rev =
284+
env::var_os("LIBAFL_QEMU_GIT_REV").map(|x| x.to_string_lossy().to_string());
281285

282286
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_DIR");
283287
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_CLONE_DIR");
284288
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_FORCE_BUILD");
285289
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_FORCE_CONFIGURE");
286290
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_NO_BUILD");
291+
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_GIT_URL");
292+
println!("cargo:rerun-if-env-changed=LIBAFL_QEMU_GIT_REV");
287293

288294
let out_dir = env::var_os("OUT_DIR").unwrap();
289295
let out_dir = out_dir.to_string_lossy().to_string();
@@ -299,29 +305,45 @@ pub fn build(
299305
let cpp_compiler = cc::Build::new().cpp(true).get_compiler();
300306

301307
let libafl_qemu_dir = if let Some(qemu_dir) = libafl_qemu_dir.as_ref() {
302-
if libafl_qemu_clone_dir.is_some() {
303-
println!(
304-
"cargo:warning=LIBAFL_QEMU_DIR and LIBAFL_QEMU_CLONE_DIR are both set. LIBAFL_QEMU_DIR will be considered in priority"
305-
);
306-
}
308+
assert!(
309+
libafl_qemu_clone_dir.is_none(),
310+
"cargo:warning=LIBAFL_QEMU_DIR and LIBAFL_QEMU_CLONE_DIR are both set."
311+
);
312+
313+
assert!(
314+
libafl_qemu_git_url.is_none(),
315+
"cargo:warning=LIBAFL_QEMU_DIR and LIBAFL_QEMU_GIT_URL are both set."
316+
);
317+
318+
assert!(
319+
libafl_qemu_git_rev.is_none(),
320+
"cargo:warning=LIBAFL_QEMU_DIR and LIBAFL_QEMU_GIT_REV are both set."
321+
);
307322

308323
Path::new(&qemu_dir).to_path_buf()
309324
} else {
310325
let qemu_path = if let Some(clone_dir) = &libafl_qemu_clone_dir {
311326
PathBuf::from(clone_dir)
312327
} else {
313-
target_dir.join(QEMU_DIRNAME)
328+
target_dir.join(LIBAFL_QEMU_DIRNAME)
314329
};
315330

331+
let qemu_git_url = libafl_qemu_git_url
332+
.as_deref()
333+
.unwrap_or(LIBAFL_QEMU_GIT_URL);
334+
let qemu_git_rev = libafl_qemu_git_rev
335+
.as_deref()
336+
.unwrap_or(LIBAFL_QEMU_GIT_REV);
337+
316338
let qemu_rev = target_dir.join("QEMU_REVISION");
317339
if qemu_rev.exists()
318-
&& fs::read_to_string(&qemu_rev).expect("Failed to read QEMU_REVISION") != QEMU_REVISION
340+
&& fs::read_to_string(&qemu_rev).expect("Failed to read QEMU_REVISION") != qemu_git_rev
319341
{
320342
drop(fs::remove_dir_all(&qemu_path));
321343
}
322344

323345
if !qemu_path.is_dir() {
324-
println!("cargo:warning=Qemu not found, cloning with git ({QEMU_REVISION})...");
346+
println!("cargo:warning=Qemu not found, cloning with git ({qemu_git_rev})...");
325347
fs::create_dir_all(&qemu_path).unwrap();
326348
assert!(
327349
Command::new("git")
@@ -337,7 +359,7 @@ pub fn build(
337359
.arg("remote")
338360
.arg("add")
339361
.arg("origin")
340-
.arg(QEMU_URL)
362+
.arg(qemu_git_url)
341363
.status()
342364
.unwrap()
343365
.success()
@@ -349,7 +371,7 @@ pub fn build(
349371
.arg("--depth")
350372
.arg("1")
351373
.arg("origin")
352-
.arg(QEMU_REVISION)
374+
.arg(qemu_git_rev)
353375
.status()
354376
.unwrap()
355377
.success()
@@ -363,7 +385,7 @@ pub fn build(
363385
.unwrap()
364386
.success()
365387
);
366-
fs::write(&qemu_rev, QEMU_REVISION).unwrap();
388+
fs::write(&qemu_rev, qemu_git_rev).unwrap();
367389
}
368390

369391
qemu_path

libafl_qemu/libafl_qemu_build/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod build;
2424
pub use build::build;
2525

2626
#[rustversion::nightly]
27-
use crate::build::QEMU_REVISION;
27+
use crate::build::LIBAFL_QEMU_GIT_REV;
2828

2929
const LLVM_VERSION_MAX: i32 = 33;
3030

@@ -421,7 +421,7 @@ pub fn maybe_generate_stub_bindings(
421421
None,
422422
vec![
423423
header.as_str(),
424-
format!("/* qemu git hash: {QEMU_REVISION} */").as_str(),
424+
format!("/* qemu git hash: {LIBAFL_QEMU_GIT_REV} */").as_str(),
425425
],
426426
false,
427427
);

0 commit comments

Comments
 (0)