PHP FFI ioctl Bug on Apple Silicon Silently Corrupts Terminal Window Size
A developer discovered a subtle but critical bug while building pseudo-terminal support for PHP using Foreign Function Interface (FFI) on Apple Silicon Macs. The issue arises because most PHP FFI code snippets declare ioctl with a fixed-arity signature, but ioctl is actually a variadic function in C. Apple's ARM64 ABI differs from the standard in that variadic arguments are passed on the stack rather than in registers, causing the wrong memory to be read when ioctl is called incorrectly. This means the call returns a success code of zero while silently writing garbage data, making the bug extremely difficult to detect — especially if CI pipelines run only on Linux, where the two calling conventions happen to agree. The fix is a one-line change: replacing the typed third parameter in the FFI declaration with an ellipsis (...), prompting libffi to generate the correct call frame for Darwin.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in