fix: restore original function bytes on x86_64 when mock is dropped#123
Closed
mazong1123 wants to merge 1 commit intomainfrom
Closed
fix: restore original function bytes on x86_64 when mock is dropped#123mazong1123 wants to merge 1 commit intomainfrom
mazong1123 wants to merge 1 commit intomainfrom
Conversation
When a ThreadRegistration is dropped and ref_count reaches 0, restore the original function bytes, free the JIT dispatcher and trampoline memory, and remove the registry entry. This eliminates the permanent dispatcher overhead for unmocked functions. Previously, after a mock was dropped, the JIT dispatcher remained installed at the function's entry point. While functionally correct (the dispatcher returned the original behavior via a trampoline), this caused stack overflow in downstream projects when running 500+ tests sequentially. Deep call chains through previously-patched functions accumulated dispatcher overhead (get_thread_target + catch_unwind + TLS lookup per call) that exceeded the default stack. The restoration is gated behind cfg(target_arch = "x86_64") since the ARM64/ARM32 concern about asynchronous instruction cache invalidation does not apply to x86_64's coherent instruction cache. Includes regression tests that verify: - Function machine code bytes are restored after mock is dropped - Repeated patch/drop cycles work correctly (registry cleanup) - Function behavior is correct across the mock lifecycle Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
When a ThreadRegistration is dropped and ref_count reaches 0, restore the original function bytes, free the JIT dispatcher and trampoline memory, and remove the registry entry. This eliminates the permanent dispatcher overhead for unmocked functions.
Previously, after a mock was dropped, the JIT dispatcher remained installed at the function's entry point. While functionally correct (the dispatcher returned the original behavior via a trampoline), this caused stack overflow in downstream projects when running 500+ tests sequentially. Deep call chains through previously-patched functions accumulated dispatcher overhead (get_thread_target + catch_unwind + TLS lookup per call) that exceeded the default stack.
The restoration is gated behind cfg(target_arch = "x86_64") since the ARM64/ARM32 concern about asynchronous instruction cache invalidation does not apply to x86_64's coherent instruction cache.
Includes regression tests that verify: