Skip to content

Add two-stage ARM64 trampoline allocation with instruction expansion#26

Merged
MBurchard merged 1 commit intomainfrom
fix-arm64-trampoline-expansion
Mar 27, 2026
Merged

Add two-stage ARM64 trampoline allocation with instruction expansion#26
MBurchard merged 1 commit intomainfrom
fix-arm64-trampoline-expansion

Conversation

@MBurchard
Copy link
Copy Markdown
Owner

The previous fix used alloc_near with a fixed ±128MB range for ARM64 trampolines. This fails on some VM layouts where no free memory exists within that range.

  • Try ±128MB first (all relocations fit in-place), then ±4GB (covers ADRP), then unrestricted mmap as final fallback
  • When a relocated instruction overflows its immediate range, expand it to an absolute sequence (LDR X16 + BR X16 for branches, MOVZ/MOVK for address loads, inverted condition + absolute branch for conditionals)
  • Trampoline is now variable-length: Vec instead of fixed offsets
  • Dynamic error messages in alloc_near show actual max_range
  • 7 new unit tests for all expansion types (B, BL, ADR, ADRP, CBZ, TBZ, near-stays-single)

The previous fix used alloc_near with a fixed ±128MB range for ARM64 trampolines. This fails
on some VM layouts where no free memory exists within that range.

- Try ±128MB first (all relocations fit in-place), then ±4GB (covers ADRP), then unrestricted
  mmap as final fallback
- When a relocated instruction overflows its immediate range, expand it to an absolute sequence
  (LDR X16 + BR X16 for branches, MOVZ/MOVK for address loads, inverted condition + absolute
  branch for conditionals)
- Trampoline is now variable-length: Vec<u32> instead of fixed offsets
- Dynamic error messages in alloc_near show actual max_range
- 7 new unit tests for all expansion types (B, BL, ADR, ADRP, CBZ, TBZ, near-stays-single)
@MBurchard MBurchard merged commit f1ce087 into main Mar 27, 2026
1 check passed
@MBurchard MBurchard deleted the fix-arm64-trampoline-expansion branch March 27, 2026 23:20
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