Add GCHandle double-free detection via asserts and sentinel checks#125291
Draft
Add GCHandle double-free detection via asserts and sentinel checks#125291
Conversation
- In HndDestroyHandle (handletable.cpp): assert that the handle's referent slot does not already contain the DEBUG_DestroyedHandleValue sentinel (0x7) when freeing. If it does, the handle was freed before without being reallocated, indicating a double-free. - In ~CEEInfo() (jitinterface.h): add a debug-only O(n²) loop to check for duplicate entries in m_pJitHandles before freeing. A duplicate entry means the same handle would be freed twice. Also null out m_pJitHandles and m_transientDetails after deletion for defensive robustness. Helps diagnose #117138. Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add assertions and sentinel values to catch double-free error
Add GCHandle double-free detection via asserts and sentinel checks
Mar 7, 2026
Member
|
/azp run runtime-coreclr gcstress-extra |
|
Azure Pipelines successfully started running 1 pipeline(s). |
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.
Description
Investigating a rarely-reproducible assert in issue #117138, suspected to be a double-free of an
OBJECTHANDLE/GCHandlein native VM code. Adds debug-build instrumentation to catch the violation on CI before it manifests as a hard-to-reproduce crash.HndDestroyHandle(handletable.cpp)The handle table already writes
DEBUG_DestroyedHandleValue(0x7) into a slot when it is freed (debug builds only). Added an assert at the top ofHndDestroyHandlethat fires if the slot already holds that sentinel — meaning the handle was freed a second time before being reallocated:~CEEInfo()(jitinterface.h)Added a
#ifdef _DEBUGO(n²) scan overm_pJitHandlesbefore the free loop to detect duplicate entries — a duplicate means the sameOBJECTHANDLEwould be freed twice. Also defensively nullsm_pJitHandlesandm_transientDetailsafter deletion.Both checks are zero-cost in release builds. The assert message convention (
_ASSERTE("message" && condition)) matches the existing style in the same files (e.g.,handletable.h,gchandleutilities.cpp).Changes
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.