Skip to content

Generalize check for dynamic scopes when saving EH clauses #116217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025

Conversation

jakobbotsch
Copy link
Member

@jakobbotsch jakobbotsch commented Jun 2, 2025

The Task returning thunks generated for runtime async methods have a catch (Exception ex) in them. During EH we were lazily trying to resolve the dynamic type token to a type handle using the metadata scope from the original IL method. Ensure we do this during JIT time instead, using the right resolver.

Fix #115779

The Task returning thunks generated for runtime async methods have a
`catch (Exception ex)` in them. During EH we were lazily trying to
resolve the dynamic type token to a type handle using the metadata scope
from the original IL method. Ensure we do this during JIT time instead,
using the right resolver.
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jun 2, 2025
@jakobbotsch jakobbotsch added area-VM-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jun 2, 2025
Copy link
Contributor

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

@jakobbotsch jakobbotsch marked this pull request as ready for review June 2, 2025 15:02
@jakobbotsch jakobbotsch requested review from Copilot, jkotas and VSadov June 2, 2025 15:02
Copy link
Contributor

@Copilot 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 generalizes exception handling (EH) clause token resolution by switching from a method-specific dynamic check to a scope-based approach, ensuring the correct resolver is used at JIT time.

  • Replace m_pMethodBeingCompiled->IsDynamicMethod() with IsDynamicScope(m_MethodInfo.scope)
  • Switch from AsDynamicMethodDesc()->GetResolver() to GetDynamicResolver(m_MethodInfo.scope)
  • Resolve dynamic type tokens during JIT compilation rather than lazily at EH time
Comments suppressed due to low confidence (2)

src/coreclr/vm/jitinterface.cpp:12780

  • Add tests that cover EH clause resolution for both dynamic and non-dynamic scopes to verify ResolveToken is invoked only when intended.
    if (IsDynamicScope(m_MethodInfo.scope) &&

src/coreclr/vm/jitinterface.cpp:12785

  • Potential null dereference: GetDynamicResolver may return null if the scope isn't dynamic. Consider adding a null check or assertion before calling ResolveToken.
        GetDynamicResolver(m_MethodInfo.scope)->ResolveToken(clause->ClassToken, &resolved);

@VSadov
Copy link
Member

VSadov commented Jun 2, 2025

I have a testcase for the repro in https://github.com/VSadov/runtime/tree/asyncEhBug. May be useful to cherry-pick into this PR.

@VSadov
Copy link
Member

VSadov commented Jun 2, 2025

The test is passing on my machine now (with this change).

Copy link
Member

@VSadov VSadov left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@jakobbotsch
Copy link
Member Author

I have a testcase for the repro in https://github.com/VSadov/runtime/tree/asyncEhBug. May be useful to cherry-pick into this PR.

I have not generally been adding regression tests for these very basic issues found by Fuzzlyn. Fuzzlyn finds these issues in a few seconds, so I think it would be better to get the coverage by adding Fuzzlyn testing of runtime async to the CI (it's on my TODO list...).

@jakobbotsch jakobbotsch merged commit ca376e3 into dotnet:main Jun 3, 2025
100 checks passed
@jakobbotsch jakobbotsch deleted the fix-115779 branch June 3, 2025 10:16
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.

[runtime-async] Runtime crash on Windows when throwing exceptions
3 participants