[mono] Fix jit_mm mismatch in delegate trampoline creation for dynamic methods#125583
Merged
[mono] Fix jit_mm mismatch in delegate trampoline creation for dynamic methods#125583
Conversation
…c methods mono_create_delegate_trampoline_info used a merged memory manager (class + method) to insert into delegate_info_hash, but mono_jit_free_method used jit_mm_for_method (method-only) to look up and remove entries. When the merged mm differed from the method mm, cleanup would silently fail to remove the delegate_info_hash entry, and could also fail to find dpairs tracked in dyn_delegate_info_hash since they were stored in a different jit_mm. Additionally, the two hash table insertions (delegate_info_hash and dyn_delegate_info_hash) used separate lock/unlock regions, creating a window where a concurrent mono_jit_free_method could see an inconsistent state. Fix both issues: 1. For dynamic methods, use jit_mm_for_method consistently (matching the cleanup path in mono_jit_free_method). 2. Perform both hash table insertions under a single lock to eliminate the partial-state window. Fixes #120059 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Mono JIT runtime bookkeeping mismatch for delegate trampoline info associated with dynamic methods, ensuring the same MonoJitMemoryManager is used for both insertion and cleanup, and making the insertion atomic with respect to concurrent cleanup.
Changes:
- For dynamic methods, use
jit_mm_for_method(method)(instead of a merged class+method memory manager) when selecting thedelegate_info_hashowner. - Insert into
delegate_info_hashanddyn_delegate_info_hashunder a singlejit_mmlock to avoid transient partial state during concurrentmono_jit_free_method.
You can also share your feedback on Copilot code review. Take the survey.
BrzVlad
approved these changes
Mar 16, 2026
Member
Author
|
/ba-g ios failures are infra |
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.
Problem
mono_create_delegate_trampoline_infoused a merged memory manager (class + method) to select whichjit_mm->delegate_info_hashto insert into, butmono_jit_free_methodusedjit_mm_for_method(method-only) to look up and remove entries. When the merged mm differed from the method mm, cleanup would silently fail to remove thedelegate_info_hashentry and could trigger the assertion at mini-runtime.c:3006:Additionally, the two hash table insertions (
delegate_info_hashanddyn_delegate_info_hash) used separate lock/unlock regions, creating a window where a concurrentmono_jit_free_methodcould see an inconsistent state — finding a dpair indyn_delegate_info_hashthat hadn't been fully set up yet, or missing one that was already indelegate_info_hash.This manifests as a random assertion failure on WASM with AOT and threads enabled:
Fix
jit_mm_for_methodconsistently (matching the cleanup path inmono_jit_free_method) instead of the merged memory manager.delegate_info_hashanddyn_delegate_info_hash) under a single lock to eliminate the partial-state window.Fixes #120059