mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
Seek and resize files on the synthetic guest drive
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
1 parent
6a60749939
commit
2bc08a1851
5 files changed
+182
-9
No files matched your search
@@ -11,6 +11,8 @@ remains unsupported.
|
||||
|
||||
### Added
|
||||
|
||||
- Added file seeking and end-of-file resizing on the synthetic guest drive.
|
||||
|
||||
- Added a bounded synthetic guest drive with synchronous file open, read, write, and size queries.
|
||||
|
||||
- Added unnamed guest semaphores with checked release and immediate mixed-object waits.
|
||||
|
||||
@@ -971,13 +971,19 @@ impl SymbolDb {
|
||||
"CreateFile — stub returning INVALID_HANDLE_VALUE",
|
||||
);
|
||||
for module in [m, "kernel32.dll"] {
|
||||
for symbol in ["CreateFileW", "ReadFile", "WriteFile", "GetFileSizeEx"] {
|
||||
for symbol in [
|
||||
"CreateFileW",
|
||||
"ReadFile",
|
||||
"WriteFile",
|
||||
"GetFileSizeEx",
|
||||
"SetFilePointerEx",
|
||||
"SetEndOfFile",
|
||||
] {
|
||||
self.ins(module, symbol, Classification::StandardService);
|
||||
}
|
||||
}
|
||||
self.win32_stub(m, "CloseHandle", false, "CloseHandle — no-op stub");
|
||||
self.win32_stub(m, "GetFileSize", false, "GetFileSize — stub returning 0");
|
||||
self.win32_stub(m, "SetFilePointerEx", false, "SetFilePointerEx — stub");
|
||||
self.win32_stub(
|
||||
m,
|
||||
"GetFileAttributesA",
|
||||
|
||||
@@ -19,6 +19,7 @@ const FILE_NOT_FOUND: u32 = 2;
|
||||
const FILE_EXISTS: u32 = 80;
|
||||
const ALREADY_EXISTS: u32 = 183;
|
||||
const INVALID_NAME: u32 = 123;
|
||||
const NEGATIVE_SEEK: u32 = 131;
|
||||
const SHARING_VIOLATION: u32 = 32;
|
||||
const DISK_FULL: u32 = 112;
|
||||
const MAX_FILES: usize = 256;
|
||||
@@ -27,7 +28,7 @@ const MAX_TRANSFER: usize = 1024 * 1024;
|
||||
|
||||
pub(super) struct OpenFile {
|
||||
path: String,
|
||||
position: usize,
|
||||
position: u64,
|
||||
access: u32,
|
||||
share: u32,
|
||||
}
|
||||
@@ -156,6 +157,73 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
Ok(1)
|
||||
});
|
||||
|
||||
let seek = Arc::clone(state);
|
||||
registry.register(KERNEL32, "SetFilePointerEx", move |args, _, memory| {
|
||||
let mut objects = seek.objects.lock().expect("guest object state");
|
||||
let super::ObjectState { handles, files, .. } = &mut *objects;
|
||||
let Some(GuestObject::File(file)) = handles.get_mut(&args.rcx) else {
|
||||
return seek.fail(ERROR_INVALID_HANDLE, 0);
|
||||
};
|
||||
if file.access & (READ | WRITE) == 0 {
|
||||
return seek.fail(ERROR_ACCESS_DENIED, 0);
|
||||
}
|
||||
let origin = match args.r9 as u32 {
|
||||
0 => None,
|
||||
1 => Some(file.position),
|
||||
2 => Some(files.get(&file.path).expect("open file exists").len() as u64),
|
||||
_ => return seek.fail(ERROR_INVALID_PARAMETER, 0),
|
||||
};
|
||||
let position = match origin {
|
||||
None => args.rdx,
|
||||
Some(origin) => {
|
||||
let distance = args.rdx as i64;
|
||||
if distance.is_negative() && distance.unsigned_abs() > origin {
|
||||
return seek.fail(NEGATIVE_SEEK, 0);
|
||||
}
|
||||
let Some(position) = origin.checked_add_signed(distance) else {
|
||||
return seek.fail(ERROR_INVALID_PARAMETER, 0);
|
||||
};
|
||||
position
|
||||
}
|
||||
};
|
||||
if args.r8 != 0 && memory.write_u64(args.r8, position).is_err() {
|
||||
return seek.fail(ERROR_NOACCESS, 0);
|
||||
}
|
||||
file.position = position;
|
||||
Ok(1)
|
||||
});
|
||||
|
||||
let truncate = Arc::clone(state);
|
||||
registry.register(KERNEL32, "SetEndOfFile", move |args, _, _| {
|
||||
let mut objects = truncate.objects.lock().expect("guest object state");
|
||||
let super::ObjectState { handles, files, .. } = &mut *objects;
|
||||
let Some(GuestObject::File(file)) = handles.get(&args.rcx) else {
|
||||
return truncate.fail(ERROR_INVALID_HANDLE, 0);
|
||||
};
|
||||
if file.access & WRITE == 0 {
|
||||
return truncate.fail(ERROR_ACCESS_DENIED, 0);
|
||||
}
|
||||
if file.position > MAX_BYTES as u64 {
|
||||
return truncate.fail(DISK_FULL, 0);
|
||||
}
|
||||
let used = files.values().map(Vec::len).sum::<usize>();
|
||||
let data = files.get_mut(&file.path).expect("open file exists");
|
||||
let length = file.position as usize;
|
||||
let growth = length.saturating_sub(data.len());
|
||||
if growth > MAX_BYTES - used {
|
||||
return truncate.fail(DISK_FULL, 0);
|
||||
}
|
||||
data.try_reserve_exact(growth)
|
||||
.map_err(|_| ExitReason::MemoryFault(MemoryError::AllocationFailed))?;
|
||||
if length < data.len() {
|
||||
data.truncate(length);
|
||||
data.shrink_to_fit();
|
||||
} else {
|
||||
data.resize(length, 0);
|
||||
}
|
||||
Ok(1)
|
||||
});
|
||||
|
||||
for (symbol, writing) in [("ReadFile", false), ("WriteFile", true)] {
|
||||
let state = Arc::clone(state);
|
||||
registry.register(KERNEL32, symbol, move |args, _, memory| {
|
||||
@@ -190,7 +258,13 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
if count == 0 {
|
||||
0
|
||||
} else {
|
||||
let end = position + count;
|
||||
let Some(end) = position
|
||||
.checked_add(count as u64)
|
||||
.filter(|end| *end <= MAX_BYTES as u64)
|
||||
else {
|
||||
return state.fail(DISK_FULL, 0);
|
||||
};
|
||||
let end = end as usize;
|
||||
let growth = end.saturating_sub(data.len());
|
||||
if growth > MAX_BYTES - used {
|
||||
return state.fail(DISK_FULL, 0);
|
||||
@@ -204,11 +278,11 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
.map_err(|_| ExitReason::MemoryFault(MemoryError::AllocationFailed))?;
|
||||
data.resize(end, 0);
|
||||
}
|
||||
data[position..end].copy_from_slice(&input);
|
||||
data[position as usize..end].copy_from_slice(&input);
|
||||
count
|
||||
}
|
||||
} else {
|
||||
let start = position.min(data.len());
|
||||
let start = position.min(data.len() as u64) as usize;
|
||||
let transferred = count.min(data.len() - start);
|
||||
if transferred != 0
|
||||
&& (args.rdx == 0
|
||||
@@ -223,12 +297,19 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
memory
|
||||
.write_u32(args.r9, transferred as u32)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
file.position += transferred;
|
||||
file.position += transferred as u64;
|
||||
Ok(1)
|
||||
});
|
||||
}
|
||||
|
||||
for symbol in ["CreateFileW", "GetFileSizeEx", "ReadFile", "WriteFile"] {
|
||||
for symbol in [
|
||||
"CreateFileW",
|
||||
"GetFileSizeEx",
|
||||
"ReadFile",
|
||||
"WriteFile",
|
||||
"SetFilePointerEx",
|
||||
"SetEndOfFile",
|
||||
] {
|
||||
registry.alias(FILE_API, KERNEL32, symbol);
|
||||
registry.set_status(KERNEL32, symbol, ServiceStatus::Partial);
|
||||
}
|
||||
|
||||
@@ -1198,6 +1198,87 @@ fn test_guest_synthetic_file_io_through_iat() {
|
||||
let eof = call("ReadFile", &[reader, output + 8, 8, output, 0]);
|
||||
assert_eq!(eof.execution.final_state.get(Register::Rax), 1);
|
||||
assert_eq!(eof.image.memory.read_u32(output).unwrap(), 0);
|
||||
let seek = call("SetFilePointerEx", &[reader, (-2i64) as u64, output, 2]);
|
||||
assert_eq!(seek.execution.final_state.get(Register::Rax), 1);
|
||||
assert_eq!(seek.image.memory.read_u64(output).unwrap(), 2);
|
||||
let rejected = call("SetFilePointerEx", &[reader, (-3i64) as u64, output, 1]);
|
||||
assert_eq!(rejected.execution.final_state.get(Register::Rax), 0);
|
||||
let read = call("ReadFile", &[reader, output + 8, 8, output, 0]);
|
||||
assert_eq!(read.image.memory.read_u32(output).unwrap(), 2);
|
||||
assert_eq!(
|
||||
call("SetEndOfFile", &[reader])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
0
|
||||
);
|
||||
assert_eq!(
|
||||
call("CloseHandle", &[reader])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
1
|
||||
);
|
||||
|
||||
let writer = open("sample.dat", 0xc000_0000, 3);
|
||||
assert_ne!(writer, u64::MAX);
|
||||
assert_eq!(
|
||||
call("SetFilePointerEx", &[writer, 2, 0, 0])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
call("SetEndOfFile", &[writer])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
1
|
||||
);
|
||||
let size = call("GetFileSizeEx", &[writer, output]);
|
||||
assert_eq!(size.image.memory.read_u64(output).unwrap(), 2);
|
||||
assert_eq!(
|
||||
call("SetFilePointerEx", &[writer, 6, 0, 0])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
call("SetEndOfFile", &[writer])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
call("SetFilePointerEx", &[writer, 0, 0, 0])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
1
|
||||
);
|
||||
let read = call("ReadFile", &[writer, output + 8, 8, output, 0]);
|
||||
assert_eq!(read.image.memory.read_u32(output).unwrap(), 6);
|
||||
assert_eq!(read.image.memory.read_u64(output + 8).unwrap(), 0xffff);
|
||||
|
||||
assert_eq!(
|
||||
call("SetFilePointerEx", &[writer, u64::MAX, 0, 0])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
call("WriteFile", &[writer, output, 1, output + 8, 0])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
0
|
||||
);
|
||||
let size = call("GetFileSizeEx", &[writer, output]);
|
||||
assert_eq!(size.image.memory.read_u64(output).unwrap(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+4
-1
@@ -197,7 +197,10 @@ same rules with 64-bit results.
|
||||
The runtime exposes an in-memory C: drive through CreateFileW, ReadFile,
|
||||
WriteFile, GetFileSizeEx, and CloseHandle. It supports root-level UTF-16
|
||||
names, ASCII case folding, synchronous I/O, per-handle positions, and
|
||||
read/write sharing. Paths cannot access host files. The drive supports
|
||||
read/write sharing. SetFilePointerEx supports seeking from the beginning,
|
||||
current position, and end. SetEndOfFile truncates or zero-extends writable
|
||||
files at the current position. Seeks alone do not change file length.
|
||||
Paths cannot access host files. The drive supports
|
||||
256 files and 16 MiB of data, with a 1 MiB limit per transfer.
|
||||
Directories, persistence, asynchronous I/O, and file handle duplication
|
||||
remain outside this subset.
|
||||
|
||||
Reference in new issue
Block a user