sharpemu
sharpemu copied to clipboard
fix(vfs): harden getdirentries and APR filepath resolution
Follow-up VFS hardening applying the same guest-writes-first discipline from the time subsystem to directory enumeration and APR filepath resolution.
Getdirentries (KernelGetdirentriesCore):
- Write the 512-byte dirent buffer first via
TryWriteCompat, updatebasepsecond (when non-null) viaTryWriteUInt64Compat, and advancedirectory.NextIndexonly after both guest writes succeed. - Removed the early
basepwrite at method entry that could mutate guest memory before buffer validation and advance the host cursor before a successful dirent delivery, causing permanent entry loss onMEMORY_FAULTatbufferAddress. - On EOF (
currentIndex >= Entries.Length), writebasepwith the final offset and return 0 without mutatingNextIndex, matching FreeBSDgetdirentries(2)semantics. ForKernelGetdents,basePointerAddressis 0, so only the buffer write and host cursor advance run.
APR (sceKernelAprResolveFilepathsToIdsAndFileSizes):
- Stopped writing ids and sizes into guest memory inside the loop; path resolution and file size lookup fill host-side local buffers first (stackalloc for small counts, heap arrays when count exceeds the thresholds). On
EFAULTorNOT_FOUND, guest arrays are untouched andAmprFileRegistryis not updated. - Pack
localSizesandlocalIdsinto contiguous byte buffers and write each output array with oneTryWriteCompatcall instead of per-elementTryWriteUInt32Compat/TryWriteUInt64Compatpairs. AmprFileRegistry.Registerruns only after guest writes succeed.AmprFileRegistry.ComputeFileIdis internal so ids can be computed during the resolve loop without premature registry mutation. Removed theuint.MaxValueplaceholder pre-write.
Out of scope: NetCtl connected-state stubs; writable range validation pre-probes for split outputs.
Files: KernelMemoryCompatExports.cs, AmprFileRegistry.cs