Skip to content

Keep buffers containing interior pointers as native pointers - #1840

Merged
Jevan Saks (jevansaks) merged 5 commits into
mainfrom
jevansaks-interior-pointer-safety
Oct 7, 2026
Merged

Jevan Saks (jevansaks) merged 5 commits into
mainfrom
jevansaks-interior-pointer-safety

Conversation

@jevansaks

@jevansaks Jevan Saks (jevansaks) commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Keep caller-owned buffers containing interior pointers as native pointers so callers can control their storage and pinning lifetime through output consumption.

  • Suppress span, managed-array, and in/ref/out buffer projections for annotated parameters, including buffers sharing their capacity. Keep capacities explicit, including optional size pointers.
  • Preserve friendly projections for unrelated buffers, handles, and scalar outputs.
  • Add synthetic-metadata and package-backed regression coverage, and document the buffer lifetime requirements.

Upgrade Microsoft.Windows.SDK.Win32Metadata to 71.0.30-preview to consume ContainsInteriorPointersAttribute and the buffer annotations from microsoft/win32metadata#2355.

Examples

GetPrinterW at level 2 returns a PRINTER_INFO_2W whose pPrinterName and other pointers refer to data in the output buffer. GetTokenInformation with TokenUser returns a TOKEN_USER whose User.Sid points into its output buffer. Both buffers are now annotated with ContainsInteriorPointersAttribute.

Representative overloads with default settings (GetPrinterW is named GetPrinter; namespaces, attributes, and bodies omitted):

Before: the span overload pins only for the native call.

static unsafe BOOL GetPrinter(
    SafeHandle hPrinter, uint Level,
    Span<byte> pPrinter, out uint pcbNeeded);

static unsafe BOOL GetTokenInformation(
    SafeHandle TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass,
    Span<byte> TokenInformation, out uint ReturnLength);

After: the buffer and capacity stay explicit; SafeHandle and scalar out projections remain.

static unsafe BOOL GetPrinter(
    SafeHandle hPrinter, uint Level,
    byte* pPrinter, uint cbBuf, out uint pcbNeeded);

static unsafe BOOL GetTokenInformation(
    SafeHandle TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass,
    void* TokenInformation, uint TokenInformationLength, out uint ReturnLength);

A span backed by a managed array could move after the temporary pin ended, invalidating the returned printer-name or SID pointers. Callers must now supply storage that stays at the same address through both the call and consumption of those pointers.

Related: #1839.

@jevansaks
Jevan Saks (jevansaks) marked this pull request as ready for review October 7, 2026 03:14
Comment thread docfx/docs/features.md Resolved
@jevansaks
Jevan Saks (jevansaks) enabled auto-merge (squash) October 7, 2026 16:50
@jevansaks
Jevan Saks (jevansaks) merged commit 2f5e045 into main Oct 7, 2026
20 checks passed
@jevansaks
Jevan Saks (jevansaks) deleted the jevansaks-interior-pointer-safety branch October 7, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants