Skip to content

[release/11.0] Fix fetching of this pointer from interpreted filters - #138007

Open
github-actions[bot] wants to merge 1 commit into
release/11.0from
backport/pr-137778-to-release/11.0
Open

github-actions[bot] wants to merge 1 commit into
release/11.0from
backport/pr-137778-to-release/11.0

Conversation

@github-actions

@github-actions github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #137778 to release/11.0

/cc @BrzVlad

Customer Impact

  • Customer reported
  • Found internally

On iOS, filters belonging to interpreted methods can fail to obtain the correct this pointer from the frame. This was happening for synchronized methods and for gshared instance methods on generic types (which access their generic context from this pointer). Within filter execution this was read for an incorrect stack slot, resulting in crash or invalid execution.

Regression

  • Yes
  • No

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 this too 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.

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
@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.

@BrzVlad BrzVlad added the Servicing-consider Issue for next servicing release review label Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant