Proposal: use span and ref-based interop instead of `GetPointer`
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 18/100
- issue の種類
- リファクタリング
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- csharp
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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], orref/inof a value) to the genericref/ref readonlyoverloads that Silk.NET generates for native calls. These overloads pin the reference withfixedfor the duration of the call. - Use
fixedonly where a pointer has to stay valid for longer than one call, and keep the pointer inside thefixedblock. - Helpers that fill a buffer write into a
Span<T>and return a length or offset, not a pointer into it.
Proposed changes
- Mark both
GetPointeroverloads[Obsolete], with a message that points to the idiom above. Remove them in a later major version. They are public inStride.Core, so removing them now would break user code. - Change the remaining call sites:
PipelineState.Direct3D11.EncodeSemanticName(string, ref Span<byte>)returns a pointer into its buffer. Its only caller passes astackallocbuffer, 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 itsstackallocbuffer to get the addresses.StringMarshal.GetString(this ReadOnlySpan<byte>),GetString(this ReadOnlySpan<ushort>)andGetString(this ReadOnlySpan<char>)useGetPointer()only to check fornull; 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 returnnullis a behaviour change and a separate decision.
- Review other conversions with the same shape, for example
StringMarshal.GetUtf8Span(in byte source, int maxLength), which builds a span fromUnsafe.AsPointerwhen the length is unknown. Itsbyte*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 publicStride.Coremethod? - Should
StringMarshal.GetStringreturnnullfor an empty span, or only for a span with no reference (default)?
- 主要言語
- C#
- スター
- 8k
- フォーク
- 1.2k
- 平均マージ
- 2日 7時間
- マージ済み PR(30日)
- 316
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
stride3d/stride のほかの issue
-
`.gitignore` isn't created for sample projects対応中かも @ferafiks が 1 日前に担当しました。 オープンarea-Asset area-GameStudio area-Samples bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
area-Build bug
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
area-Samples enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
stride3d/stride#3386 · コメント 7 件 ·
メンテナーはふだん 1 日以内に返信
-
area-Physics bug
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
stride3d/stride#3384 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area-Samples bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
stride3d/stride#3066 · コメント 6 件 ·
メンテナーはふだん 1 日以内に返信
stride3d/stride の issue をすべて見る
似ている issue
-
要トリアージ
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
unoplatform/uno#26709 ·
メンテナーはふだん 1 日以内に返信
-
area-Tools-ILLink untriaged
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
dotnet/runtime#138255 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 2 日以内に返信
-
area-fundamentals bug untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
dotnet/extensions#7979 ·
メンテナーはふだん 1 日以内に返信