Skip to content

Conversation

@radekdoulik
Copy link
Member

@radekdoulik radekdoulik commented Dec 16, 2025

Return early for delegates to avoid setting the slot in SetCodeEntryPoint and to skip backpatch. The delegates are special case, where we have single implementation for all of them, and we don't want to break other parts, which depend on 1:1 mapping between implementation and method desc.

This fixes #121955, which was still happening a lot in the runtime tests (cca 90 occurences in the out of process tests on wasm)

We were getting the exception, because during the access checks we got private DelegateConstruct method from the MT slot.

Return early for delegates to avoid setting the slot in
`SetCodeEntryPoint` and to skip backpatch. The delegates are special
case, where we have single implementation for all of them, and we
don't want to break other parts, which depend on 1:1 mapping between
implementation and method desc.

This fixes dotnet#121955, which was
still happening a lot in the runtime tests (cca 90 occurences in the
out of process tests on wasm)

We were getting the exception, because during the access checks we got
private CreateDelegate method from the MT slot.
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

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 issue #121955 by adding special handling for delegates in the DoPrestub function to preserve the 1:1 mapping between implementation and method descriptors. The fix prevents delegates from having their code entry point set in the vtable slot and from being backpatched, which was causing access check failures where private delegate methods were incorrectly retrieved from MT slots.

Key Changes

  • Adds early return for delegates under FEATURE_PORTABLE_ENTRYPOINTS to skip SetCodeEntryPoint and DoBackpatch calls
  • Targets edge cases where delegate method descriptors have native code already set (pCode != NULL)

#ifdef FEATURE_PORTABLE_ENTRYPOINTS
if (pMT->IsDelegate())
{
return pCode;
Copy link
Member

Choose a reason for hiding this comment

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

If we early out here, does it mean that we will be going through PreStub every time the delegate constructor is called? We should not be going through PreStub in steady state as we have discussed...

Copy link
Member Author

@radekdoulik radekdoulik Dec 18, 2025

Choose a reason for hiding this comment

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

We are not going through PreStub again as the method already has interpreter code after 1st PreStub pass and so we don't call DoPreStub again. OTOH it will leave the method in different state. I will try to better understand the conditions under which we reach the issue and possibly find better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[wasm][coreclr] Attempt to access method System.Delegate.DelegateConstruct results in exception

2 participants