Skip to content

fix(build): preserve Android tagged pointers in OpenTUI FFI (fixes #43, #46) - #48

Open
MohamadOday wants to merge 1 commit into
Hope2333:native-androidfrom
MohamadOday:fix/opentui-tagged-pointers
Open

MohamadOday wants to merge 1 commit into
Hope2333:native-androidfrom
MohamadOday:fix/opentui-tagged-pointers

Conversation

@MohamadOday

Copy link
Copy Markdown

Problem

On Android 11+ ARM64, Bionic Scudo applies a 0xb4 top-byte tag (ARM TBI) to heap allocations.

In OpenTUI 0.6.x (used by OpenCode >= 2.1.4), toSafeNumberPointer() checks pointer > BigInt(Number.MAX_SAFE_INTEGER) and throws Error: Pointer exceeds safe integer range. This prevents the TUI from starting (reported in #43 and #46).

Additionally, toBunPointer2() casts BigInt pointers to Number(pointer), which truncates bits beyond 53 before passing the address to native toArrayBuffer.

Stripping the 0xb4 tag is unsafe because Bionic Scudo validates the tag during free() and aborts with Pointer tag for 0x... was truncated.

Fix

Bun 1.4.0 FFI natively accepts BigInt values for ptr arguments and preserves the 64-bit tagged pointer.

  1. In tools/build-bionic/apply-platform-patch.sh, patch toSafeNumberPointer() to return the pointer directly when pointer > BigInt(Number.MAX_SAFE_INTEGER) instead of throwing POINTER_UNSAFE.
  2. Patch toBunPointer2() to return the pointer directly instead of casting to Number(pointer).
  3. Add tests/unit/apply-platform-patch.bats to verify patch application, idempotency, OpenTUI 0.5.x compatibility, and runtime 64-bit pointer preservation under Node.js.

Verification

  • Ran tests/unit/apply-platform-patch.bats inside Termux: 5/5 tests passed.
  • Ran all existing unit tests (common.bats, make-opencode.bats, plugin-manager.bats, upgrade-matrix.bats): 53/53 tests passed.
  • Verified on a patched 2.1.4 binary on Android 12 (kernel 5.10, ARM64): opencode launches the TUI without crashing, handles sessions, and exits cleanly without Bionic free aborts.

…pe2333#43, Hope2333#46)

On Android 11+ ARM64, Bionic Scudo applies a 0xb4 top-byte tag (ARM TBI)
to heap allocations. In OpenTUI 0.6.x (shipped with OpenCode >= 2.1.4),
toSafeNumberPointer() throws 'Pointer exceeds safe integer range' on any
pointer above Number.MAX_SAFE_INTEGER, while toBunPointer2() casts BigInt
pointers to Number, truncating bits beyond 53. Stripping the tag causes
Bionic to abort on free().

Bun 1.4.0 FFI natively accepts BigInt values for pointer parameters and
preserves the 64-bit tagged pointer.

Update apply-platform-patch.sh to pass through BigInt pointers when
exceeding Number.MAX_SAFE_INTEGER in toSafeNumberPointer, and return
pointers directly in toBunPointer2. Add apply-platform-patch.bats unit
tests to verify patch application, idempotency, and 64-bit preservation.
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