Skip to content

[mono] Fix interface call in case of reabstraction - #131219

Merged
BrzVlad merged 2 commits into
dotnet:mainfrom
BrzVlad:fix-mono-dim-reabstraction
Jul 28, 2026
Merged

[mono] Fix interface call in case of reabstraction#131219
BrzVlad merged 2 commits into
dotnet:mainfrom
BrzVlad:fix-mono-dim-reabstraction

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Jul 22, 2026

Copy link
Copy Markdown
Member

An interface is assigned an offset in the vtable of a class that implements it, so that a call through a method on that interface will do a call through the vtable slot. There are multiple places where this offset computation happens, for example mono_class_setup_count_virtual_methods. Method reabstraction doesn't create a new slot.

Interface calls are dispatched through imt slots that are placed before the vtable. A slot is initialized in build_imt_slots so that it calls through the corresponding vtable slot. For slots where we have collision of multiple interface methods, we emit some thunk that does repeated checks to dispatch to the right method. The build_imt_slots method didn't handle reabstraction so the vtable slot to be called for an interface method was incorrectly computed. This would appear to suggest that all cases of interface method reabstraction are broken. However, it wasn't the case because even though we incorrectly initialize the imt_slot with the wrong implementing method, mini_resolve_imt_method will end up patching the imt_slot if we don't have collisions, over-writting with the correct target.

Added a testcase with multiple reabstracted methods on a type.

Fixes #129496

An interface is assigned an offset in the vtable of a class that implements it, so that a call through a method on that interface will do a call through the vtable slot. There are multiple places where this offset computation happens, for example `mono_class_setup_count_virtual_methods`. Method reabstraction doesn't create a new slot.

Interface calls are dispatched through imt slots that are placed before the vtable. A slot is initialized in `build_imt_slots` so that it calls through the corresponding vtable slot. For slots where we have collision of multiple interface methods, we emit some thunk that does repeated checks to dispatch to the right method. The `build_imt_slots` method didn't handle reabstraction so the vtable slot to be called for an interface method was incorrectly computed. This would appear to suggest that all cases of interface method reabstraction are broken. However, it wasn't the case because even though we incorrectly initialize the imt_slot with the wrong implementing method, `mini_resolve_imt_method` will end up patching the imt_slot if we don't have collisions, over-writting with the correct target.

Added a testcase with multiple reabstracted methods on a type.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 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: @steveisok, @vitek-karas
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Mono’s IMT slot construction to ignore reabstracted interface methods when computing the vtable slot to call, preventing incorrect interface dispatch for IMT slots that require collision trampolines. It also adds a regression test that defines an interface with many reabstracted members to validate dispatch stability.

Changes:

  • Skip reabstracted interface methods in build_imt_slots (both the generic-interface fast path and the normal path) so they don’t affect vt_slot accounting or IMT entry construction.
  • Add a new regression test project and test case exercising interface dispatch in the presence of many reabstracted members.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/mono/mono/metadata/object.c Skips reabstracted methods while building IMT entries so vtable slot selection remains correct, especially for collision trampolines.
src/tests/Loader/classloader/DefaultInterfaceMethods/regressions/ReabstractedInterfaceMethodIMT.cs Adds a regression test that invokes many interface methods on a type whose interface includes many reabstracted members.
src/tests/Loader/classloader/DefaultInterfaceMethods/regressions/ReabstractedInterfaceMethodIMT.csproj Adds the test project file for building/running the new regression test.

@lateralusX lateralusX 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! nit: maybe add a generic version of the test to hit the generic path as well?

Copilot AI review requested due to automatic review settings July 27, 2026 15:54
@BrzVlad
BrzVlad force-pushed the fix-mono-dim-reabstraction branch from 1c2977a to 8e751a7 Compare July 27, 2026 15:54

@lateralusX lateralusX 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!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@BrzVlad

BrzVlad commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

/ba-g unrelated wasm

@BrzVlad
BrzVlad merged commit a7bb6df into dotnet:main Jul 28, 2026
118 of 120 checks passed
@BrzVlad

BrzVlad commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.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.

Inconsistent behavior and segfaults on Android Release build

3 participants