Skip to content

[C#] A failed ReservedCapacity change leaves FileMappedResizableDirectMemory unmapped (Pointer = 0) #97

Backlog

Description

@konard

Problem

OnReservedCapacityChanged unmaps the file, calls FileHelpers.SetSize and then maps it again. If SetSize (or the remap) throws, the object is left with no mapping: Pointer == IntPtr.Zero, while ReservedCapacity still reports the old value. The next access dereferences null, or garbage when Pointer is cached.

mem-rs fixed the same class of bug in linksplatform/mem-rs#38: on error, the old mapping stays valid.

Reproduction (probe P3)

memory.ReservedCapacity = long.MaxValue / 2 →
ArgumentOutOfRangeException: Specified file length was too large for the file system. Afterwards Pointer = 0x0, ReservedCapacity = 4096, file length 4096.

Proposed solution

  • Validate the new size before unmapping. Reserve and extend the file first (RandomAccess.SetLength / FileStream.SetLength), and only then remap.
  • On failure, restore the previous mapping (try/catch → MapFile(oldCapacity)) and rethrow. Base-class state must change only after success.
  • Add a regression test that forces a failure, for example a too-large capacity or a read-only file, and checks that Pointer is still usable.

Part of #94 (catch up with mem-rs). Analysis and evidence: case study.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcsharpmem-rs-parity

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions