Skip to content

fix: restore original function bytes on x86_64 when mock is dropped#123

Closed
mazong1123 wants to merge 1 commit intomainfrom
fix/restore-function-bytes-on-drop
Closed

fix: restore original function bytes on x86_64 when mock is dropped#123
mazong1123 wants to merge 1 commit intomainfrom
fix/restore-function-bytes-on-drop

Conversation

@mazong1123
Copy link
Collaborator

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

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>
@mazong1123 mazong1123 closed this Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant