Skip to content

Conversation

@AaronRobinsonMSFT
Copy link
Member

Backport of #119972 to release/9.0-staging

/cc @AaronRobinsonMSFT

Customer Impact

  • Customer reported
  • Found internally

The UnsafeAccessorAttribute doesn't properly differentiate ambiguity for between the two signatures below. Both are void(*)(void) and in that case the generic calling convention isn't used to indicate distinct signatures.

class C
{
    private static void M() { }
    private static void M<T>() { }
}

Customer reported in #119875.

Regression

  • Yes
  • No

This is regression from .NET 8 because we didn't support generics method with UnsafeAccessorAttribute. When it was added in .NET 9, a test case was missed. A new test was added to validate this scenario.

Testing

A test was added.

Risk

Low.

Handle the case where there is an ambiguous name and signature match, but one is generic and the other isn't.
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 9.0.x milestone Sep 23, 2025
@AaronRobinsonMSFT AaronRobinsonMSFT added the Servicing-consider Issue for next servicing release review label Sep 23, 2025
Copilot AI review requested due to automatic review settings September 23, 2025 17:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue with UnsafeAccessor where it couldn't properly differentiate between ambiguous method signatures that have the same calling convention but different generic parameter counts. The fix ensures that both generic and non-generic methods with the same name can be properly distinguished.

Key changes:

  • Added proper generic parameter count validation in signature matching
  • Enhanced test coverage to verify ambiguous method name resolution with generics
  • Updated existing test cases to use more descriptive naming

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs Added comprehensive test cases for ambiguous method names and updated existing tests with clearer naming
src/coreclr/vm/prestub.cpp Fixed signature matching logic to properly handle generic calling conventions and parameter counts
src/coreclr/tools/Common/TypeSystem/IL/UnsafeAccessors.cs Added generic parameter count validation in method signature matching

@dotnet-policy-service
Copy link
Contributor

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

@jkotas
Copy link
Member

jkotas commented Sep 23, 2025

This fix will make the following code throw MissingMethodException instead of InvalidProgramException. Is that correct?

using System.Runtime.CompilerServices;

class Program
{
    static void Main()
    {
        // Throws System.Reflection.AmbiguousMatchException: 'Ambiguity in binding of UnsafeAccessorAttribute.'
        Example(null);
    }

    [UnsafeAccessor(UnsafeAccessorKind.StaticMethod)]
    private static extern void Example(ContainingClass? _);
}

class ContainingClass
{
    private static void Example<T>() { }
}

I am not sure whether it meets the servicing bar. If there is a code out there that implemented a workaround for this corner-case bug by catching InvalidProgramException, it is going to break.

@AaronRobinsonMSFT
Copy link
Member Author

This fix will make the following code throw MissingMethodException instead of InvalidProgramException. Is that correct?

I think this is fine and the correct exception in this case.

@AaronRobinsonMSFT AaronRobinsonMSFT added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Sep 25, 2025
@AaronRobinsonMSFT
Copy link
Member Author

/ba-g timeouts

@AaronRobinsonMSFT AaronRobinsonMSFT merged commit d8b00b9 into dotnet:release/9.0-staging Sep 25, 2025
99 of 106 checks passed
@AaronRobinsonMSFT AaronRobinsonMSFT deleted the pr-119972-to-release/9.0-staging branch September 25, 2025 21:22
@github-actions github-actions bot locked and limited conversation to collaborators Oct 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants