Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix the net481 build break in AllocationHelper - #131417

#131417
Merged
tannergooding merged 2 commits into
dotnet:maindotnet/runtime:mainfrom
tannergooding:tannergooding-stunning-lamptannergooding/runtime:tannergooding-stunning-lampCopy head branch name to clipboard
Jul 27, 2026
Merged

Fix the net481 build break in AllocationHelper#131417
tannergooding merged 2 commits into
dotnet:maindotnet/runtime:mainfrom
tannergooding:tannergooding-stunning-lamptannergooding/runtime:tannergooding-stunning-lampCopy head branch name to clipboard

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Fixes #131390.

Microsoft.Bcl.Memory.Tests links System.Memory/tests/AllocationHelper.cs and targets net481, where NativeMemory doesn't exist. #131280 switched that helper from Marshal.AllocHGlobal to NativeMemory without accounting for the shared compile, breaking windows-x64 Release Libraries_NET481.

Rather than reverting to Marshal.AllocHGlobal -- which is LocalAlloc on Windows and would pessimize every modern .NET consumer to fix a net481-only compile error -- this adds a test-only NativeMemory polyfill under Common/tests, alongside the RuntimeHelpers polyfill already linked into the same .NETFramework ItemGroup.

System.Memory.Tests is $(NetCoreAppCurrent)-only, so Microsoft.Bcl.Memory.Tests is the only project that picks the polyfill up.


The one unavoidable change in AllocationHelper itself is the argument cast:

-                memory = (IntPtr)NativeMemory.Alloc((nuint)size);
+                memory = (IntPtr)NativeMemory.Alloc((nuint)(void*)size);

IntPtr/UIntPtr aren't the numeric types on .NETFramework, so (nuint)size is CS0030 there -- that's the second error in the issue, and it's independent of the polyfill. The pointer round-trip is legal on both and compiles to nothing.


Validated locally on Windows x64:

Leg Result
Microsoft.Bcl.Memory.Tests net481 innerloop 548 passed
Microsoft.Bcl.Memory.Tests net481 outerloop 2 passed
Microsoft.Bcl.Memory.Tests netcoreapp innerloop 548 passed
System.Memory.Tests innerloop 52,929 passed
System.Memory.Tests outerloop 31 passed

The outerloop runs cover the remaining AllocationHelper consumers (Overflow, Clear, BinarySearch, Base64, Base64Url). Stashing the fix reproduces the exact CS0103/CS0030 errors from the issue.

CC. @dotnet/area-system-memory

Note

This PR description and the code changes were drafted with GitHub Copilot.

`Microsoft.Bcl.Memory.Tests` compiles this shared file for `net481`, where
`NativeMemory` doesn't exist. Add a test-only polyfill next to the existing
`RuntimeHelpers` one rather than falling back to `Marshal.AllocHGlobal`, which
is `LocalAlloc` on Windows and would pessimize the modern .NET path.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a .NET Framework (net481) build break in Microsoft.Bcl.Memory.Tests caused by AllocationHelper’s use of NativeMemory, by adding a test-only NativeMemory polyfill for .NET Framework and adjusting the allocation cast to compile on net481.

Changes:

  • Update AllocationHelper.TryAllocNative to round-trip IntPtr through a pointer when converting to nuint, avoiding a net481 compile error.
  • Add a minimal System.Runtime.InteropServices.NativeMemory polyfill under src/libraries/Common/tests for .NET Framework test targets.
  • Link the polyfill into Microsoft.Bcl.Memory.Tests only for .NETFramework builds via the project file.
Show a summary per file
File Description
src/libraries/System.Memory/tests/AllocationHelper.cs Adjusts the IntPtrnuint conversion to compile on .NET Framework while keeping the existing allocation/locking behavior.
src/libraries/Microsoft.Bcl.Memory/tests/Microsoft.Bcl.Memory.Tests.csproj Links the new NativeMemory polyfill only for .NETFramework builds of the test project.
src/libraries/Common/tests/System/Runtime/InteropServices/NativeMemory.cs Introduces a minimal test-only NativeMemory implementation backed by Marshal.AllocHGlobal / FreeHGlobal.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

Comment thread src/libraries/System.Memory/tests/AllocationHelper.cs Outdated
The rewording was leftover from an earlier `Marshal.AllocHGlobal` approach and is
unrelated churn now that the helper is back on `NativeMemory`.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 27, 2026 13:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new

@tannergooding
tannergooding enabled auto-merge (squash) July 27, 2026 15:44
@tannergooding
tannergooding merged commit a3bdcb9 into dotnet:main Jul 27, 2026
84 checks passed
@tannergooding
tannergooding deleted the tannergooding-stunning-lamp branch July 27, 2026 15:47
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ci-scan] Build break: NativeMemory not available in Microsoft.Bcl.Memory.Tests on net481

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.