Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Move a half-written Ghidra project aside instead of deleting it under a running analysis (#1045)

kuseidosf7bf0b29c837
diff --git a/crates/engines/src/ghidra/mod.rs b/crates/engines/src/ghidra/mod.rs
index 5dbbcf2b..6dada785 100644
--- a/crates/engines/src/ghidra/mod.rs
+++ b/crates/engines/src/ghidra/mod.rs
@@ -500,6 +500,31 @@ fn lock_is_fresh(lock: &Path) -> bool {
std::fs::metadata(lock).and_then(|m| m.modified()).is_ok_and(|t| t.elapsed().is_ok_and(|age| age < LOCK_STALE))
}
+/// A project folder a stale lock was guarding, moved out of the way instead of removed: a Ghidra
+/// that outlived the app that started it keeps writing into its own folder, not into the one this
+/// analysis is about to fill (#951). Dropping it frees the room once this analysis has its own.
+struct Aside(PathBuf);
+
+impl Drop for Aside {
+ /// Free the room once this analysis has a folder of its own.
+ fn drop(&mut self) {
+ let _ = std::fs::remove_dir_all(&self.0);
+ }
+}
+
+/// Move `project_dir` aside if anything is there, and say where. Fails where the folder cannot be
+/// moved — Windows refuses while a process has files open in it — which is exactly the case a
+/// caller must read as *another analysis is still going* rather than as *nothing to do*.
+fn set_aside(project_dir: &Path) -> std::io::Result<Option<Aside>> {
+ if !project_dir.exists() {
+ return Ok(None);
+ }
+ let token = random_token();
+ let aside = project_dir.with_extension(format!("orphaned-{}", token.get(..8).unwrap_or(&token)));
+ std::fs::rename(project_dir, &aside)?;
+ Ok(Some(Aside(aside)))
+}
+
/// The start of the folder names Ghidra gives this system's native programs (`mac_arm_64`, ...).
/// None where we don't know them, so nothing is refused there.
fn native_folder_prefix() -> Option<&'static str> {
@@ -571,7 +596,8 @@ pub fn analyse(
let lock = lock_path(&project_dir);
// The lock sits beside the project, in the cache folder a fresh store doesn't have yet.
make_private_dir(lock.parent().unwrap_or(&root))?;
- loop {
+ // The loop ends by handing over what it moved aside, if anything.
+ let kept_aside = loop {
if is_analysed(&project_dir) {
return Ok(project_dir);
}
@@ -589,19 +615,30 @@ pub fn analyse(
// Stale or none: take it (create_new, so only one of two racing processes wins).
let _ = std::fs::remove_file(&lock);
match std::fs::OpenOptions::new().write(true).create_new(true).open(&lock) {
- Ok(_) => break,
+ Ok(_) => {}
Err(e) if e.kind() != std::io::ErrorKind::AlreadyExists => {
return Err(GhidraError::Launch(format!("taking the analysis lock {}: {e}", lock.display())));
}
- Err(_) => {}
+ // Another process won the race: look at the folder again.
+ Err(_) => continue,
}
- }
+ // Whatever the dead process left is moved, not removed, so a Ghidra that outlived it keeps
+ // its own folder. A refusal to move it means that Ghidra is still working: wait for it.
+ match set_aside(&project_dir) {
+ Ok(aside) => break aside,
+ Err(e) => {
+ let _ = std::fs::remove_file(&lock);
+ progress(0.05, &format!("waiting for the analysis of this program already under way ({e})"));
+ std::thread::sleep(Duration::from_secs(2));
+ }
+ }
+ };
let held = HeldLock(lock.clone());
+ let _aside = kept_aside;
let scratch = Scratch::new(&root, "ghidra-analysis")?;
let program = format!("target-{}", target_sha256.get(..12).unwrap_or(target_sha256));
// Beside the saved analysis, so it can be read after a failure.
let log = project_dir.with_extension("log");
- remove_soon(&project_dir);
make_private_dir(&project_dir)?;
let script_dir = scratch.0.join("scripts");
make_private_dir(&script_dir)?;
diff --git a/crates/engines/src/ghidra/tests.rs b/crates/engines/src/ghidra/tests.rs
index be174165..2960083c 100644
--- a/crates/engines/src/ghidra/tests.rs
+++ b/crates/engines/src/ghidra/tests.rs
@@ -423,7 +423,8 @@ fn a_second_analysis_waits_for_the_running_one_and_a_dead_ones_lock_is_taken_ove
compiler_spec: None,
memory_gb: None,
};
- let (sha, digest) = ("ab".repeat(32), "cd".repeat(32));
+ // The bytes and their real sha, so this reaches the launch instead of failing on a mismatch.
+ let (sha, digest) = (craft_anything_evidence::sha256_hex(b"x"), "cd".repeat(32));
// A temp folder can be under a dot folder (`/tmp/.tmpXXXX`): the project is then reached
// through the store's link, which is the path analyse() gives back.
let (_link, root) = LinkGuard::root(d.path());
@@ -443,13 +444,48 @@ fn a_second_analysis_waits_for_the_running_one_and_a_dead_ones_lock_is_taken_ove
assert_eq!(got.ok(), Some(project.clone()), "waited for the running analysis instead of starting another");
// A lock left by a process that died goes stale and is taken over (here: an attempt to run).
+ // What that process had half-written is moved aside, never deleted under it (#951).
std::fs::remove_dir_all(&project).unwrap();
+ std::fs::create_dir_all(&project).unwrap();
+ std::fs::write(project.join("half-written.gpr"), "the other analysis may still be filling this").unwrap();
let lock = std::fs::File::options().write(true).open(lock_path(&project)).unwrap();
lock.set_modified(std::time::SystemTime::now() - LOCK_STALE - Duration::from_secs(5)).unwrap();
drop(lock);
- let err = analyse(&cfg, b"x", &sha, &digest, &|| false, &|_, _| {}).unwrap_err();
+ // Seen while the analysis runs, not after: by then both paths have cleaned up.
+ // The project sits beside its lock, which the store may reach through a link.
+ let store = project.parent().unwrap().to_path_buf();
+ let saw_aside = AtomicBool::new(false);
+ let progress = |_: f32, msg: &str| {
+ if msg.contains("Ghidra is analysing") {
+ let aside = std::fs::read_dir(&store).unwrap().flatten().any(|e| e.file_name().to_string_lossy().contains(".orphaned-"));
+ saw_aside.store(aside, Ordering::Relaxed);
+ }
+ };
+ let err = analyse(&cfg, b"x", &sha, &digest, &|| false, &progress).unwrap_err();
assert!(matches!(err, GhidraError::Launch(_)), "took over and tried to run Ghidra: {err:?}");
assert!(!lock_path(&project).exists(), "its own lock is released when it fails");
+ assert!(saw_aside.load(Ordering::Relaxed), "the folder the other analysis was filling must still be on disk, under its own name");
+ assert!(!project.join("half-written.gpr").exists(), "it wrote into the folder the other analysis was filling");
+}
+
+/// #951: the folder a stale lock was guarding is moved, not removed, so a Ghidra that outlived the
+/// app that started it keeps writing into its own folder instead of into this analysis's.
+#[test]
+fn a_half_written_project_is_moved_aside_with_its_files() {
+ let d = tempfile::tempdir().unwrap();
+ let project = d.path().join("target-abcd");
+ std::fs::create_dir_all(&project).unwrap();
+ std::fs::write(project.join("half-written.gpr"), "still being filled").unwrap();
+
+ let aside = set_aside(&project).unwrap().expect("there was a folder to move");
+ assert!(!project.exists(), "the folder this analysis needs is free");
+ let moved =
+ std::fs::read_dir(d.path()).unwrap().flatten().map(|e| e.path()).find(|p| p != &project).expect("the folder is still on disk, under a name of its own");
+ assert!(moved.join("half-written.gpr").is_file(), "it kept the files the other analysis left");
+
+ drop(aside);
+ assert!(!moved.exists(), "and it goes once this analysis has its own folder");
+ assert!(set_aside(&project).unwrap().is_none(), "nothing to move is not a failure");
}
#[test]