Repository navigation
Fix fetching of this pointer from interpreted filters - #137778
Merged
Merged
Conversation
Normally, the `this` pointer is the 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 sime 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, at the 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.
|
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 |
Member
Author
|
/backport to release/11.0 |
Contributor
|
Started backporting to |
2 of 4 tasks
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.
Normally,
thispointer is stored in the first stack slot of an interpreted frame. Some complexity arises if the method accesses its generic context fromthispointer. The runtime expects this context to be located in the first stack slot as well, but the problem is that code can modifythisviastargandldargafollowed by a store at the address. The way we solve this problem is by having a separate slot forthis, allocated after IL locals.m_paramArgIndexvar 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 rarestargandldargapatterns, we will make the var 0 point to offset 0 as well, sothisvar andm_paramArgIndexvar 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
thiswhile inside a filter handler, we would hardcode the offset of var 0. This offset is not known precisely at this point. Ifm_shadowCopyOfThisPointerActuallyNeededis false (method withoutstarg,ldarga), we will avoid using the shadowthisslot and make var 0 point to the normal 0 offset. Filter code would still use the old offset, readingthisfrom incorrect memory. As a fix we introduce a separate intermediary opcode,INTOP_LDIND_THIS_OFFSET, which resolves the offset ofthislater during code emit, when it is precisely known.Synchronized methods also use the shadow
thiscopy, to prevent different objects being passed to monitor Enter/Exit, if the method body were to alterthis. Thereforefiltersin these methods would hit the same issue.Fixes #137632