[release/9.0-staging] Fix UnsafeAccessor scenario for modopts/modreqs when comparing field sigs. by AaronRobinsonMSFT · Pull Request #111675 · dotnet/runtime
Backport of #111648 to release/9.0-staging
Customer Impact
- Customer reported
- Found internally
Consume custom modifiers and ByRef in RetType signature prior to comparing field signature. Specifically signatures have contain modopts/modreqs. This is follow-up for #109694, which was also a .NET 9 servicing for a regression, but missed a specific scenario involving readonly and was testing using a UnsafeAccessor declaration that did not properly validate the scenario.
Fixes #111647
Regression
- Yes
- No
Issue #109665 was fixed with #109694 and assumed to also address issues with readonly and so tests were added with in #109944 - these tests do pass.
The issue is best showcased with the following:
static class Accessors { [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<obj>P")] public static extern ref readonly object MEMBER(ref readonly Foo value); public static void A() { [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<obj>P")] static extern ref readonly object LOCAL(ref readonly Foo value); } }
The MEMBER and LOCAL are the same definition, but MEMBER uses an modreq when LOCAL doesn't. For testing, the LOCAL scenario was used for validation and this created a false sense of correctness in the tests. The result is the modopt/modreq issues can still occur for fields depending on how the UnsafeAccessor is defined. This means we need to service .NET 9 again, see #109709, and make the correct fix in .NET 10 along with updating the testing so we validate both types of definition.
Testing
New tests have been added and the existing tests have been updated to cover this scenario.
Risk
Medium. This is likely "Low" from a regression standpoint, but increases the Risk since this is a second attempt at a fix. There is much more confidence in this fix after uncovering the metadata differences in signature declaration.