[clr-interp] Add support for synchronized methods#120006
Merged
davidwrighton merged 4 commits intodotnet:mainfrom Sep 24, 2025
Merged
[clr-interp] Add support for synchronized methods#120006davidwrighton merged 4 commits intodotnet:mainfrom
davidwrighton merged 4 commits intodotnet:mainfrom
Conversation
Member
davidwrighton
commented
Sep 23, 2025
- The general approach is to wrap the method is a try/finally block
- This is done by appending some IL to the method, and adding an EH clause
- This required implementing a few abstraction boundaries so that the code/eh clauses could be rewritten within the interpreter
- Synchronized methods also re-use the shadow this pointer logic
- existing IL which is encoded as a RET needs to actually have the semantics of a LEAVE to to the actual return from the method
- We also have a couple of "intrinsic" tokens which the our implementation of CALL now recognizes.
- The general approach is to wrap the method is a try/finally block - This is done by appending some IL to the method, and adding an EH clause - This required implementing a few abstraction boundaries so that the code/eh clauses could be rewritten within the interpreter - Synchronized methods also re-use the shadow this pointer logic - existing IL which is encoded as a RET needs to actually have the semantics of a LEAVE to to the actual return from the method - We also have a couple of "intrinsic" tokens which the our implementation of CALL now recognizes.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for synchronized methods in the CLR interpreter by implementing a try/finally pattern to handle monitor enter/exit operations.
Key changes:
- Wraps synchronized methods in try/finally blocks with additional IL opcodes for monitor operations
- Implements shadow this pointer logic for synchronized instance methods
- Converts RET instructions to LEAVE operations in synchronized methods to ensure proper cleanup
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/coreclr/vm/interpexec.cpp | Adds INTOP_CALL_HELPER_V_SA opcode handler and HELPER_FTN_V_PP typedef |
| src/coreclr/interpreter/intops.h | Defines intrinsic tokens for synchronized method implementation |
| src/coreclr/interpreter/inc/intops.def | Adds INTOP_CALL_HELPER_V_SA opcode definition |
| src/coreclr/interpreter/compiler.h | Adds synchronized method state tracking variables and helper methods |
| src/coreclr/interpreter/compiler.cpp | Implements synchronized method compilation logic, IL rewriting, and monitor operations |
69 tasks
Member
|
(Not a complaint about this implementation) Is there a reason we couldn't have the interpreter itself do the synchronization? |
kg
reviewed
Sep 23, 2025
kg
reviewed
Sep 23, 2025
kg
reviewed
Sep 23, 2025
kg
reviewed
Sep 23, 2025
kg
reviewed
Sep 23, 2025
kg
approved these changes
Sep 23, 2025
Member
kg
left a comment
There was a problem hiding this comment.
The complexity bothers me but I assume there's a good reason why it can't be simpler. Other than the comments I left it LGTM
janvorli
reviewed
Sep 23, 2025
janvorli
reviewed
Sep 23, 2025
This was referenced Sep 24, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.