GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add opt-in non-reflective object comparer for NativeAOT by hydraxman · Pull Request #2308 · MessagePack-CSharp/MessagePack-CSharp

Conversation

@hydraxman

@hydraxman hydraxman commented Oct 8, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Summary

Add an explicitly opt-in, non-reflective object-key comparer for NativeAOT while leaving the default security behavior unchanged.

The existing ObjectFallbackEqualityComparer.GetHashCode(object) dynamically invokes the virtual generic comparer factory for the key's runtime type. NativeAOT diagnoses that MakeGenericMethod path with IL2060 and IL3050. Preserving arbitrary existing generic overrides requires keeping the legacy behavior available; replacing it automatically on NativeAOT would change an existing contract.

This PR adds:

  • A default-off MessagePack.Security.UseNonReflectiveObjectComparer feature switch, annotated on the net9.0 library asset.
  • A separate object comparer selected by that switch, allowing the legacy implementation to be eliminated at publish time.
  • One protected overload, IEqualityComparer GetHashCollisionResistantEqualityComparer(Type type), for statically written custom comparer dispatch.
  • Contract regression tests and a standalone JIT/native executable.

The diff is limited to four files: one production file, the existing unit test file, and the standalone fixture's project and program. No README, workflow, or solution changes are included. The standalone native fixture is run explicitly; the existing CI is unchanged and does not run it automatically.

Related: #1666 (broader trimming compatibility) and #1202 (preserving the legacy extension contract). This PR addresses only the security object-comparer path; it does not claim whole-library NativeAOT compatibility or resolve the broader scope of #1666.

Explicit opt-in and compatibility

<ItemGroup>
  <RuntimeHostConfigurationOption
      Include="MessagePack.Security.UseNonReflectiveObjectComparer"
      Value="true"
      Trim="true" />
</ItemGroup>

This is an application-wide publish-time choice, not a per-instance setting. Native elimination requires the annotated net9.0 library asset and a .NET 9 or later NativeAOT toolchain. Consuming an older asset or setting AppContext only at runtime does not provide that guarantee. Configure the switch before MessagePack initializes; runtime toggling is unsupported.

With the switch off, the existing implementation remains unchanged, including JIT generic overrides and NativeAOT applications that already make the needed native instantiations available.

With the switch on:

  • Primitive keys, Guid, DateTime and DateTimeOffset retain their keyed hashes and normalization. Primitive generic overrides still participate, with per-type caching.
  • Boxed enums retain keyed hashing of the full underlying integer, not ordinary Enum.GetHashCode.
  • Arbitrary custom types are rejected unless the new Type overload explicitly returns a comparer.
  • Boxed enum-specific generic overrides also require an explicit branch in that overload. Typed generic calls are unchanged.
  • Object equality, both comparer interfaces, the parameterless factory, and Clone remain available.

Custom security subclasses can connect an existing collision-resistant generic comparer through a statically known type:

protected override System.Collections.IEqualityComparer
    GetHashCollisionResistantEqualityComparer(Type type)
{
    return type == typeof(MyKey)
        ? (System.Collections.IEqualityComparer)this.GetHashCollisionResistantEqualityComparer<MyKey>()
        : base.GetHashCollisionResistantEqualityComparer(type);
}

Use the same pattern for an enum-specific comparer. The returned comparer must implement non-generic IEqualityComparer, as the legacy object fallback also requires. User-supplied comparers retain the existing obligation to provide collision resistance and equality consistency.

No collision protection is disabled. Applications must still select MessagePackSecurity.UntrustedData when appropriate. No serializer payload format, library dependency, target framework, release workflow, or tracked version is changed. No warning suppressions, linker roots, or dependency annotations are added.

Validation

The following local results were obtained on Windows using SDK 10.0.401 at commit 56fafcdd02b33dbf43599d4fde9b482b321c1c60. Subsequent changes remove documentation, workflow additions, and solution registration, and shorten code comments; executable code and test project configuration are unchanged. The repository's pinned SDK was not changed.

Check Result
net9.0 NativeAOT, opt-in, ILCompiler 9.0.20 98 assertions passed; zero build warnings
net10.0 NativeAOT, opt-in, ILCompiler 10.0.12 98 assertions passed; zero build warnings
net10.0 NativeAOT, legacy/default 96 assertions passed; original one IL2060 and one IL3050 remain
net10.0 ARM64 NativeAOT, opt-in Compiled and linked with zero warnings; not executed
net9.0 JIT, legacy / opt-in 96 / 98 assertions passed
Main MessagePack.Tests, net9.0 1086 passed, 6 existing skips, zero failures
Main MessagePack.Tests, net472 1055 passed, 6 existing skips, zero failures
All five library target frameworks Build passed with zero warnings/errors

The opt-in native scan graphs contain zero nodes for ObjectFallbackEqualityComparer. The legacy negative control retains that implementation and its diagnostics, rather than suppressing them.

The native fixture covers all eight enum underlying types, a boxed-only enum with no typed enum factory call, primitive hash parity, equivalent floating-point/date representations, ordinary-hash collision fixtures, custom/enum overrides, cache reuse, clone isolation, unsupported-type rejection, and an exact-wire-format untrusted map round trip. The legacy checks use ordinary typed calls to demonstrate the existing NativeAOT behavior when the native generic instantiations are available; they do not claim support for arbitrary unseen types.

Linux execution and the full solution test suite were not performed locally.

Run the standalone fixture

From the repository root:

dotnet run --project tests\MessagePack.Security.NativeAotTests -c Release -p:PublishAot=false -p:NonReflectiveSecurity=false -- legacy
dotnet run --project tests\MessagePack.Security.NativeAotTests -c Release -p:PublishAot=false -p:NonReflectiveSecurity=true -- opt-in
dotnet publish tests\MessagePack.Security.NativeAotTests -c Release -r win-x64 -p:NonReflectiveSecurity=true -warnaserror -o bin\security-optin
.\bin\security-optin\MessagePack.Security.NativeAotTests.exe opt-in

The fixture defaults to net9.0. -p:SecurityTestTargetFramework=net10.0 selects a newer executable target without retargeting the library. For the legacy native negative control, publish with -p:NonReflectiveSecurity=false, omit -warnaserror, and run with legacy. Add -p:IlcGenerateDgmlFile=true to inspect reachability.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Nathan Bu and others added 2 commits October 5, 2026 00:12
Nathan Bu Nathan Bu
Add opt-in non-reflective object comparer for NativeAOT
7608d6b
Preserve the legacy default while adding an application-wide publish-time feature switch and a non-generic comparer extension point. Retain keyed hashing and unsupported-type rejection, document custom and boxed-enum migration, and add JIT/native regression coverage.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 786fa0e8-83da-46dc-a693-b17d03ded372
Nathan Bu Nathan Bu
Cover non-reflective comparer factory argument validation
56fafcd
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 786fa0e8-83da-46dc-a693-b17d03ded372
@hydraxman

hydraxman commented Oct 8, 2026

Copy link
Copy Markdown
Author

@dotnet-policy-service agree [company="microsoft"]

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hydraxman

hydraxman commented Oct 8, 2026

Copy link
Copy Markdown
Author

@dotnet-policy-service agree company="microsoft"

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hydraxman
hydraxman marked this pull request as ready for review October 8, 2026 23:48
@hydraxman

hydraxman commented Oct 8, 2026

Copy link
Copy Markdown
Author

@AArnott I've marked this ready for review. Could you take a look and approve the pending .NET and Unity workflow runs? The CLA check is now passing.

The production change is confined to MessagePackSecurity.cs. The feature is off by default, preserving the existing behavior; opt-in consumers can use the new Type-based hook for statically known custom and enum comparers. Keyed hashing and unsupported-type rejection remain intact.

Local Windows validation covers .NET 9/10 NativeAOT, both JIT modes, and the net9.0/net472 unit tests, with details in the PR description. The opt-in native fixture eliminates IL2060/IL3050 without suppressions. I'd particularly appreciate your feedback on the opt-in contract and the new extension point. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Nathan Bu added 2 commits October 9, 2026 08:07
Nathan Bu Nathan Bu
Keep NativeAOT comparer PR focused on code and tests
a359037
Remove added README content, workflow job, and standalone fixture solution registration. Keep production and regression test contents unchanged; retain usage and validation details in the PR description.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 786fa0e8-83da-46dc-a693-b17d03ded372
Nathan Bu Nathan Bu
Shorten NativeAOT comparer comments
3bde1ab
Keep only concise API, enum hashing, and legacy test intent comments. Leave executable code unchanged.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 786fa0e8-83da-46dc-a693-b17d03ded372
@AArnott
AArnott requested a balanced review from Copilot October 12, 2026 17:46
Copilot started reviewing on behalf of AArnott October 12, 2026 17:46
View session
Copilot AI reviewed Oct 12, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

🟡 Changes recommended

The NativeAOT fixture is not executed by CI, leaving the core feature without automated regression coverage.

1 open finding
  • Medium severity Add CI coverage for NativeAOT opt-in fixture · New
What changed in this PR

Adds an opt-in NativeAOT-compatible object comparer while preserving legacy behavior by default.

Changes:

  • Adds non-reflective primitive, enum, and custom comparer dispatch.
  • Adds regression tests and a standalone NativeAOT fixture.
  • Configures the feature switch for NativeAOT publishing.
File Description
src/​MessagePack/​MessagePackSecurity.cs Implements feature-switched comparer dispatch.
tests/​MessagePack.Tests/​MessagePackSecurityTests.cs Adds comparer contract tests.
tests/​MessagePack.Security.NativeAotTests/​Program.cs Exercises JIT and NativeAOT behavior.
tests/​MessagePack.Security.NativeAotTests/​MessagePack.Security.NativeAotTests.csproj Configures the standalone AOT fixture.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/MessagePack.Security.NativeAotTests/MessagePack.Security.NativeAotTests.csproj Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AArnott
Add new test project to solution
63e9727
AArnott
AArnott approved these changes Oct 12, 2026
View reviewed changes
@AArnott
AArnott enabled auto-merge October 12, 2026 18:31
@AArnott AArnott added this to the v3.1 milestone Oct 12, 2026
@AArnott
AArnott merged commit 552b626 into MessagePack-CSharp:master Oct 12, 2026
4 checks passed

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Oct 12, 2026
build: Bump the nuget-minor-and-patch group with 5 updates ManOguaR/ObsWebSocket#4
Merged
Bump MessagePack from 3.1.11 to 3.1.12 loresoft/FluentCommand#797
Merged
chore(deps): Bump the dotnet-minor-patch group with 22 updates sanaullahmohammed/NexusOps#107
Merged
chore(deps): Bump coverlet.collector and 20 others sanaullahmohammed/NexusOps#108
Closed
Bump the dependencies group with 10 updates sevensolutions/nomad-iis#194
Merged
Bump the nuget-minor-patch group with 27 updates rleeson24/life-insurance-crm-api#45
Merged
Bump Microsoft.AspNetCore.Mvc.Testing and 18 others rleeson24/life-insurance-crm-api#46
Merged
Bump the nuget group with 6 updates OpenShock/API#376
Merged
Bump the messaging group with 2 updates KiarashMinoo/ThunderPropagator.FormatSerializers#14
Merged
Bump MessagePack and MessagePack.Annotations Snappyup/Snappyup.Aspire.Hosting.Dragonfly#37
Open
Bump MessagePack.Annotations from 3.1.8 to 3.1.12 Snappyup/Snappyup.Aspire.Hosting.Dragonfly#38
Open
Bump MessagePack from 3.1.11 to 3.1.12 akkadotnet/akka.net#8803
Merged
Bump the nuget-all group with 2 updates Analogy-LogViewer/Analogy.LogViewer#2439
Merged
Bump MessagePack and MessagePackAnalyzer Freeesia/korenan#299
Merged
This was referenced Nov 16, 2026
Bump AWSSDK.S3 and 16 others PauloVianaR/ViverAppWeb#27
Closed
Bump the dotnet-dependencies group with 17 updates PauloVianaR/ViverAppWeb#28
Open
Bump MessagePack from 2.5.307 to 3.1.12 afernandes/DiscoveryDeviceAndroid#5
Open
Bump the patch-updates group with 12 updates mpcs2013/currency-tracker#442
Open
Bump MessagePack from 2.5.307 to 3.1.12 ErikEJ/SqlServer.Rules#820
Closed
Bump the dotnet group with 16 updates danialtoor/tickwire#28
Closed
Bump MessagePack from 2.5.307 to 3.1.12 thekim1/kalla#837
Closed
Bump the nuget-major group with 9 updates Tjeerd-menno/nats-manager-ui#386
Closed
Bump the dotnet-sidecars group with 13 updates Nimblesite/SharpLsp#600
Merged
Bump MessagePack from 2.5.307 to 3.1.12 jonnymuir/Heliograph#34
Closed
Bump AWSSDK.S3 and 15 others PauloVianaR/ViverAppWeb#30
Open
Bump MessagePack from 3.1.9 to 3.1.12 q871958549/SwapMid#17
Open
Bump the nuget-shipped group with 2 updates alexFinn97/scaffolder-for-optimizely#8
Closed
Bump the nuget group with 17 updates NikiforovAll/adaptive-rate-limiting-dotnet#17
Closed
Bump MessagePack from 2.5.307 to 3.1.12 Tjeerd-menno/nats-manager-ui#398
Merged
Bump AWSSDK.S3 and 30 others NikolozPapaskiri/ticketing-platform#92
Open
Bump the nuget group with 32 updates NikolozPapaskiri/ticketing-platform#93
Open
Bump the nuget group with 3 updates TanmayS0406/SSMSClaude-Script#17
Open
Bump the nuget group with 26 updates Vulcaine/Altruist#118
Open
Bump BCrypt.Net-Next and 25 others Vulcaine/Altruist#119
Closed
Bump MessagePack and 7 others chelomama-dev/finances-hub#34
Open
This was referenced Nov 23, 2026
Bump AWSSDK.S3 and 15 others PauloVianaR/ViverAppWeb#32
Closed
Bump the dotnet-dependencies group with 16 updates PauloVianaR/ViverAppWeb#33
Open
Bump the dotnet group with 17 updates danialtoor/tickwire#30
Open
Bump coverlet.collector and 15 others danialtoor/tickwire#31
Open
Bump MessagePack from 2.5.303 to 3.1.12 mortenbrudvik/Obfy#43
Closed
deps: Bump the "weekly" group with 1 update across multiple ecosystems ftichy/MiniShop#192
Merged
Bump the minor-and-patch group with 22 updates dotnet/crank#928
Open
Bump MessagePack from 2.5.303 to 3.1.12 D-WIS/DWIS-Blackboard-Tests#45
Open
Bump the nuget group with 17 updates NikiforovAll/adaptive-rate-limiting-dotnet#18
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@AArnott AArnott AArnott approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

v3.1

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@hydraxman @AArnott
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.