Skip to content

Fix fetching of this pointer from interpreted filters - #137778

Merged
BrzVlad merged 1 commit into
dotnet:mainfrom
BrzVlad:fix-clrinterp-filter-gencontext
Nov 20, 2026
Merged

BrzVlad merged 1 commit into
dotnet:mainfrom
BrzVlad:fix-clrinterp-filter-gencontext

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

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

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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @BrzVlad, @janvorli
See info in area-owners.md if you want to be subscribed.

@janvorli janvorli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you!

@BrzVlad
BrzVlad merged commit ce0532f into dotnet:main Nov 20, 2026
110 of 112 checks passed
@BrzVlad

BrzVlad commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

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.

[clr-interp] ldarg.0 in an exception filter reads a zeroed, never-assigned slot (null this) when the method's this shadow copy is elided

2 participants