Fix the net481 build break in AllocationHelper - #131417
#131417Merged
tannergooding merged 2 commits intoJul 27, 2026
dotnet:maindotnet/runtime:mainfrom
tannergooding:tannergooding-stunning-lamptannergooding/runtime:tannergooding-stunning-lampCopy head branch name to clipboard
Merged
Fix the net481 build break in AllocationHelper#131417tannergooding merged 2 commits intodotnet:maindotnet/runtime:mainfrom tannergooding:tannergooding-stunning-lamptannergooding/runtime:tannergooding-stunning-lampCopy head branch name to clipboard
tannergooding merged 2 commits into
dotnet:maindotnet/runtime:mainfrom
tannergooding:tannergooding-stunning-lamptannergooding/runtime:tannergooding-stunning-lampCopy head branch name to clipboard
Conversation
`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: 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. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-memory |
Contributor
There was a problem hiding this comment.
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.TryAllocNativeto round-tripIntPtrthrough a pointer when converting tonuint, avoiding anet481compile error. - Add a minimal
System.Runtime.InteropServices.NativeMemorypolyfill undersrc/libraries/Common/testsfor .NET Framework test targets. - Link the polyfill into
Microsoft.Bcl.Memory.Testsonly for.NETFrameworkbuilds via the project file.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Memory/tests/AllocationHelper.cs | Adjusts the IntPtr→nuint 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
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>
vcsjones
approved these changes
Jul 27, 2026
tannergooding
enabled auto-merge (squash)
July 27, 2026 15:44
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #131390.
Microsoft.Bcl.Memory.TestslinksSystem.Memory/tests/AllocationHelper.csand targetsnet481, whereNativeMemorydoesn't exist. #131280 switched that helper fromMarshal.AllocHGlobaltoNativeMemorywithout accounting for the shared compile, breakingwindows-x64 Release Libraries_NET481.Rather than reverting to
Marshal.AllocHGlobal-- which isLocalAllocon Windows and would pessimize every modern .NET consumer to fix anet481-only compile error -- this adds a test-onlyNativeMemorypolyfill underCommon/tests, alongside theRuntimeHelperspolyfill already linked into the same.NETFrameworkItemGroup.System.Memory.Testsis$(NetCoreAppCurrent)-only, soMicrosoft.Bcl.Memory.Testsis the only project that picks the polyfill up.The one unavoidable change in
AllocationHelperitself is the argument cast:IntPtr/UIntPtraren't the numeric types on .NETFramework, so(nuint)sizeisCS0030there -- 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:
Microsoft.Bcl.Memory.Testsnet481innerloopMicrosoft.Bcl.Memory.Testsnet481outerloopMicrosoft.Bcl.Memory.Testsnetcoreapp innerloopSystem.Memory.TestsinnerloopSystem.Memory.TestsouterloopThe outerloop runs cover the remaining
AllocationHelperconsumers (Overflow,Clear,BinarySearch,Base64,Base64Url). Stashing the fix reproduces the exactCS0103/CS0030errors from the issue.CC. @dotnet/area-system-memory
Note
This PR description and the code changes were drafted with GitHub Copilot.