Proposal: use span and ref-based interop instead of `GetPointer`

Đang mở
#3,976 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
18/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
computer-graphics

Hướng nghiên cứu

Start by reading the GetPointer overloads in UnsafeUtilities (Stride.Core) and the bug report in #3571 together with the call-site fix in #3573 to see why an untracked pointer over managed memory is unsafe. Then read the Silk.NET ref/ref readonly overloads used by the Direct3D 11 UpdateSubResource path and PipelineState.Direct3D11.EncodeSemanticName with its stackalloc caller, plus the three StringMarshal.GetString overloads. Done means both GetPointer overloads are marked [Obsolete] with a message pointing at the ref-based idiom, EncodeSemanticName returns a byte count instead of a pointer, and GetString uses Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span)). The [Obsolete]-versus-removal and empty-span questions are maintainer decisions, not something to settle in the patch.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

area-Core area-Graphics enhancement

Problem

UnsafeUtilities.GetPointer(this Span<T>) and GetPointer(this ReadOnlySpan<T>) in Stride.Core return Unsafe.AsPointer(ref span.GetReference()). The span's reference is tracked by the GC: when a compacting GC moves an array, every span over it is updated. The pointer is not tracked, and nothing pins the memory. So the pointer is only valid while the memory can't move: stack memory, native memory, or an array that is already pinned.

A span doesn't say which kind of memory it covers, so a method that takes any span and returns a pointer can't know whether its result is safe. #3571 is the bug this causes: Direct3D 11 UpdateSubResource gave the driver a GetPointer address for a span over a managed array, and a GC during the native call made the driver read stale memory. #3573 fixes that call site.

Proposed idiom

Interop code passes managed memory to native code without converting it to a pointer by hand:

  • Pass a reference to the first element (in span[0], or ref/in of a value) to the generic ref/ref readonly overloads that Silk.NET generates for native calls. These overloads pin the reference with fixed for the duration of the call.
  • Use fixed only where a pointer has to stay valid for longer than one call, and keep the pointer inside the fixed block.
  • Helpers that fill a buffer write into a Span<T> and return a length or offset, not a pointer into it.

Proposed changes

  1. Mark both GetPointer overloads [Obsolete], with a message that points to the idiom above. Remove them in a later major version. They are public in Stride.Core, so removing them now would break user code.
  2. Change the remaining call sites:
    • PipelineState.Direct3D11.EncodeSemanticName(string, ref Span<byte>) returns a pointer into its buffer. Its only caller passes a stackalloc buffer, so it is safe today, but nothing stops a heap-backed span. Proposed signature: int EncodeSemanticName(string semanticName, Span<byte> semanticNameBytes), returning the number of bytes written; the caller pins or uses its stackalloc buffer to get the addresses.
    • StringMarshal.GetString(this ReadOnlySpan<byte>), GetString(this ReadOnlySpan<ushort>) and GetString(this ReadOnlySpan<char>) use GetPointer() only to check for null; the pointer is never dereferenced, so there is no GC bug. Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span)) gives the same result without a pointer. Whether an empty span should also return null is a behaviour change and a separate decision.
  3. Review other conversions with the same shape, for example StringMarshal.GetUtf8Span(in byte source, int maxLength), which builds a span from Unsafe.AsPointer when the length is unknown. Its byte* overload forwards native memory to it, which can't move.

Open questions

  • Is [Obsolete] now and removal in the next major version the preferred path for a public Stride.Core method?
  • Should StringMarshal.GetString return null for an empty span, or only for a span with no reference (default)?
Ngôn ngữ chính
C#
Star
8k
Fork
1.2k
Merge trung bình
2 ngày 7 giờ
Pull request đã merge (30 ngày)
316

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của stride3d/stride

Tất cả issue của stride3d/stride

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.