Skip to content

Use NativeMemory instead of AllocHGlobal in WindowsIdentity and EventProvider - #136706

Merged
jkotas merged 3 commits into
dotnet:mainfrom
lagudafuadtosin:localalloc-54297
Oct 31, 2026
Merged

jkotas merged 3 commits into
dotnet:mainfrom
lagudafuadtosin:localalloc-54297

Conversation

@lagudafuadtosin

Copy link
Copy Markdown
Contributor

contributes to #54297

WindowsIdentity and EtwEventProvider.GetSessionInfo allocate with AllocHGlobal, so a dotnet new webapiaot exe imports LocalAlloc. this moves them to NativeMemory. with #126544 too, the exe no longer imports LocalAlloc.

tested: Principal.Windows 120 and Tracing 55 tests pass (Tracing run as admin so the ETW tests run), dumpbin /imports before and after.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 30, 2026
@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: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@jkotas jkotas changed the title use NativeMemory instead of AllocHGlobal in WindowsIdentity and EventProvider Use NativeMemory instead of AllocHGlobal in WindowsIdentity and EventProvider Oct 31, 2026

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@jkotas
jkotas requested review from jkotas and a balanced review from Copilot October 31, 2026 02:00

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.

🟢 Approval recommended

No active review issues are recorded for the Windows identity and ETW native-memory allocation changes.

No open issues 🎉

What changed in this PR

This PR advances the allocation audit in #54297 by replacing local-heap allocations in Windows identity and ETW tracing code with NativeMemory. Together with #126544, it aims to remove the LocalAlloc import from a NativeAOT Web API executable.

Changes:

  • Adds a SafeNativeMemoryHandle and updates token-information interop and project references.
  • Migrates Windows identity buffers to native memory and pins the source-name buffer instead of allocating a copy.
  • Replaces ETW session-buffer allocation and cleanup with NativeMemory.Alloc and Free.
File Description
src/​libraries/​System.Security.Principal.Windows/​src/​System/​Security/​Principal/​WindowsIdentity.cs Migrates authentication and token buffers; pins source-name bytes.
src/​libraries/​System.Security.Principal.Windows/​src/​System.Security.Principal.Windows.csproj Includes the new handle and updated interop file.
src/​libraries/​System.Private.CoreLib/​src/​System/​Diagnostics/​Tracing/​EventProvider.cs Uses native memory for ETW session buffers.
src/​libraries/​Common/​src/​Microsoft/​Win32/​SafeHandles/​SafeNativeMemoryHandle.cs Adds a safe buffer backed by NativeMemory.
src/​libraries/​Common/​src/​Interop/​Windows/​Advapi32/​Interop.GetTokenInformation_SafeNativeMemoryHandle.cs Updates token-information overloads to accept the new handle.

Review effort: Balanced

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.

🟢 Approval recommended

No active review issues remain for the native-allocation migration.

No open issues 🎉

Review effort: Balanced

@jkotas
jkotas enabled auto-merge (squash) October 31, 2026 06:14

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@jkotas
jkotas merged commit b94adb1 into dotnet:main Oct 31, 2026
133 of 135 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Interop-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants