Parcourir la source

Merge pull request #245 from Dufftwt/fix/import-trap-in-crash-log

Loader: a trapped unimplemented import goes into the crash log
Marvis il y a 3 jours
Parent
commit
56d503228a
1 fichiers modifiés avec 42 ajouts et 4 suppressions
  1. 42 4
      crates/victory-loader/src/thunks.rs

+ 42 - 4
crates/victory-loader/src/thunks.rs

@@ -62,10 +62,11 @@ extern "system" fn unimplemented_import(site: &'static ImportSite) -> u64 {
         }
         UnimplementedPolicy::Trap => {
             tracing::error!(dll = %site.dll, symbol = %site.symbol, "unimplemented import called");
-            eprintln!(
-                "VIctory: unimplemented import {}!{} called",
-                site.dll, site.symbol
-            );
+            // Into the crash log too: `abort` ends the process without
+            // running the handlers that write it.
+            let (dll, symbol) = (&site.dll, &site.symbol);
+            let text = format!("VIctory: unimplemented import {dll}!{symbol} called");
+            crate::log_crash(&text);
             // SAFETY: plain Win32 calls without preconditions.
             unsafe {
                 if IsDebuggerPresent() != 0 {
@@ -147,3 +148,40 @@ impl ThunkBlock {
         Ok(())
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    /// In a child process of the next test: calls an unimplemented import
+    /// that traps, with the crash log where `VICTORY_TRAP_LOG` says.
+    #[test]
+    fn trap_child() {
+        let Some(log) = std::env::var_os("VICTORY_TRAP_LOG") else {
+            return;
+        };
+        crate::log_crashes_to(log.into());
+        let site = Box::leak(Box::new(ImportSite {
+            dll: "probe.dll".into(),
+            symbol: "Missing".into(),
+            policy: UnimplementedPolicy::Trap,
+            calls: AtomicU64::new(0),
+        }));
+        unimplemented_import(site);
+    }
+
+    #[test]
+    fn a_trapped_import_is_in_the_crash_log() {
+        let dir = tempfile::tempdir().unwrap();
+        let log = dir.path().join("crashes.log");
+        let out = std::process::Command::new(std::env::current_exe().unwrap())
+            .args(["--exact", "thunks::tests::trap_child", "--nocapture"])
+            .env("VICTORY_TRAP_LOG", &log)
+            .output()
+            .unwrap();
+        assert!(!out.status.success(), "the trap ends the process");
+        let text = std::fs::read_to_string(&log).unwrap_or_default();
+        let named = text.contains("VIctory: unimplemented import probe.dll!Missing called");
+        assert!(named, "crash log: {text:?}");
+    }
+}