From 0236ec6ba94e486c1d5a719e4d2086290136e7cd Mon Sep 17 00:00:00 2001 From: zachdoxsee Date: Wed, 25 Nov 2026 02:00:04 +0100 Subject: [PATCH] loader: a failed fopen leaves the drive untouched (#402) fopen created a missing file before it allocated the FILE object, so a failed allocation left an empty file behind. The FILE object is now allocated first and released again if the drive cannot take the file. A test fills the heap and checks that neither a new file appears nor an existing one loses its contents. Signed-off-by: zachdoxsee --- crates/loader/src/services/crt_files.rs | 21 +++++++------- crates/loader/tests/crt_files.rs | 37 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/crates/loader/src/services/crt_files.rs b/crates/loader/src/services/crt_files.rs index 8c50299..463dd0b 100644 --- a/crates/loader/src/services/crt_files.rs +++ b/crates/loader/src/services/crt_files.rs @@ -140,22 +140,23 @@ impl FileStreams { if self.open.len() >= MAX_OPEN_STREAMS { return Err(EMFILE); } - match drive.drive_entry(&path) { + let create = match drive.drive_entry(&path) { DriveEntry::Directory => return Err(EACCES), - DriveEntry::File => {} + DriveEntry::File => false, DriveEntry::Missing { .. } if mode.existing => return Err(ENOENT), DriveEntry::Missing { parent: false } => return Err(ENOENT), - DriveEntry::Missing { parent: true } => { - if !drive.drive_create(&path) { - return Err(ENOSPC); - } - } - } - // The FILE object is allocated before the contents are destroyed, so - // a failure here leaves an existing file as it was. + DriveEntry::Missing { parent: true } => true, + }; + // The FILE object is allocated before the drive is touched, so a + // failed fopen neither creates a file nor destroys the contents of + // one. let file = memory .map_heap_allocation(PROCESS_HEAP, 8) .map_err(|_| ENOMEM)?; + if create && !drive.drive_create(&path) { + memory.release_heap_allocation(PROCESS_HEAP, file); + return Err(ENOSPC); + } if mode.truncate { drive.drive_truncate(&path); } diff --git a/crates/loader/tests/crt_files.rs b/crates/loader/tests/crt_files.rs index f206180..2127daa 100644 --- a/crates/loader/tests/crt_files.rs +++ b/crates/loader/tests/crt_files.rs @@ -29,6 +29,8 @@ const IMPORTS: &[(&str, &str)] = &[ (UCRT, "fgets"), (UCRT, "fputs"), (UCRT, "_errno"), + (UCRT, "malloc"), + (UCRT, "free"), ]; const GENERIC_READ: u64 = 0x8000_0000; @@ -38,6 +40,7 @@ const OPEN_EXISTING: u64 = 3; const INVALID_HANDLE: u64 = u64::MAX; const ENOENT: u32 = 2; const EBADF: u32 = 9; +const ENOMEM: u32 = 12; const EACCES: u32 = 13; // Scratch layout: a wide name, a narrow name, a mode, a transfer buffer and a @@ -224,3 +227,37 @@ fn a_stream_whose_file_was_deleted_fails_cleanly() { assert_eq!(fopen(&mut guest, "gone.txt", "r"), 0); assert_eq!(errno(&mut guest), ENOENT); } + +/// A `FILE` object comes from the process heap. When the heap has no room for +/// one, `fopen` fails with `ENOMEM` and has neither created the file nor +/// destroyed the contents of an existing one. The heap holds 4,096 blocks. +#[test] +fn an_fopen_that_cannot_allocate_leaves_the_drive_alone() { + let mut guest = guest(); + write_through_win32(&mut guest, "kept.txt", b"keep me"); + // Fill the heap one byte at a time until it refuses. + let mut blocks = Vec::new(); + loop { + let block = guest.call("malloc", &[1]); + if block == 0 { + break; + } + blocks.push(block); + assert!(blocks.len() <= 4096, "the heap never filled"); + } + for (name, mode) in [ + ("new.txt", "w"), + ("new.txt", "a"), + ("kept.txt", "w"), + ("kept.txt", "r"), + ] { + assert_eq!(fopen(&mut guest, name, mode), 0, "{name} {mode}"); + assert_eq!(errno(&mut guest), ENOMEM, "{name} {mode}"); + } + // With room for one block again: the new file was never created, and the + // existing one still has its contents. + guest.call("free", &[blocks.pop().unwrap()]); + assert_eq!(fopen(&mut guest, "new.txt", "r"), 0); + assert_eq!(errno(&mut guest), ENOENT); + assert_eq!(read_through_win32(&mut guest, "kept.txt"), b"keep me"); +}