Repository navigation
[release/11.0] Fix fetching of this pointer from interpreted filters - #138007
Open
github-actions[bot] wants to merge 1 commit into
Open
github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
Normally, `this` pointer is stored in the first stack slot of an interpreted frame. Some complexity arises if the method accesses its generic context from `this` pointer. The runtime expects this context to be located in the first stack slot as well, but the problem is that code can modify `this` via `starg` and `ldarga` followed by a store at the address. The way we solve this problem is by having a separate slot for `this`, allocated after IL locals. `m_paramArgIndex` var will point to offset 0 and var 0 will point to this offset after IL locals. Once we finish emitting code, if the method doesn't have these rare `starg` and `ldarga` patterns, we will make the var 0 point to offset 0 as well, so `this` var and `m_paramArgIndex` var point to the same area on the stack, the first slot. For a call chain M1 -> M2 -> M3, if we have an exception thrown and M1 has a filter, this filter will run while M2 and M3 frames are still active on the stack. This means that the filter excecution can't happen in the stack frame of M1 because calls made from the filter would overwrite the stack frames of M2 and M3. Therefore, filters execute in their separate stack frame, on top of the stack, and locals from the method frame are accessed with indirection through the frame pointer passed explicitly to the filter execution, see `INTOP_LOAD_FRAMEVAR`. The bug was that, when loading `this` while inside a filter handler, we would hardcode the offset of var 0. This offset is not known precisely at this point. If `m_shadowCopyOfThisPointerActuallyNeeded` is false (method without `starg`, `ldarga`), we will avoid using the shadow `this` slot and make var 0 point to the normal 0 offset. Filter code would still use the old offset, reading `this` from incorrect memory. As a fix we introduce a separate intermediary opcode, `INTOP_LDIND_THIS_OFFSET`, which resolves the offset of `this` later during code emit, when it is precisely known. Synchronized methods also use the shadow `this` copy, to prevent different objects being passed to monitor Enter/Exit, if the method body were to alter `this`. Therefore `filters` in these methods would hit the same issue. Fixes #137632
github-actions
Bot
requested review from
BrzVlad and
janvorli
as code owners
November 20, 2026 15:42
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @BrzVlad, @janvorli |
BrzVlad
approved these changes
Nov 20, 2026
This was referenced Nov 20, 2026
This branch has not been deployed
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport of #137778 to release/11.0
/cc @BrzVlad
Customer Impact
On iOS, filters belonging to interpreted methods can fail to obtain the correct
thispointer from the frame. This was happening for synchronized methods and for gshared instance methods on generic types (which access their generic context fromthispointer). Within filter executionthiswas read for an incorrect stack slot, resulting in crash or invalid execution.Regression
Issue specific to CoreCLR interpreter, which is used for the first time on MAUI iOS in .NET11.
Testing
Reproduced the issue and added testcase that is now passing.
Risk
Low. The issue was caused by obtaining the offset of
thistoo early in the compilation, before its computation was finished. The offset of the variable is now obtained later in the compilation process. The fix is limited to code generation inside filters.