sharpemu icon indicating copy to clipboard operation
sharpemu copied to clipboard

fix(vfs): harden getdirentries and APR filepath resolution

Open MikeSaito opened this issue 4 months ago • 0 comments

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, update basep second (when non-null) via TryWriteUInt64Compat, and advance directory.NextIndex only after both guest writes succeed.
  • Removed the early basep write 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 on MEMORY_FAULT at bufferAddress.
  • On EOF (currentIndex >= Entries.Length), write basep with the final offset and return 0 without mutating NextIndex, matching FreeBSD getdirentries(2) semantics. For KernelGetdents, basePointerAddress is 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 EFAULT or NOT_FOUND, guest arrays are untouched and AmprFileRegistry is not updated.
  • Pack localSizes and localIds into contiguous byte buffers and write each output array with one TryWriteCompat call instead of per-element TryWriteUInt32Compat / TryWriteUInt64Compat pairs.
  • AmprFileRegistry.Register runs only after guest writes succeed. AmprFileRegistry.ComputeFileId is internal so ids can be computed during the resolve loop without premature registry mutation. Removed the uint.MaxValue placeholder pre-write.

Out of scope: NetCtl connected-state stubs; writable range validation pre-probes for split outputs.

Files: KernelMemoryCompatExports.cs, AmprFileRegistry.cs

MikeSaito avatar Jul 12 '26 09:07 MikeSaito