Skip to content

Fix GH-25573: FFI constant expression evaluation - #25575

Open
Marti2203 wants to merge 4 commits into
php:PHP-8.4from
Marti2203:fix-gh25573-ffi-constexpr
Open

Marti2203 wants to merge 4 commits into
php:PHP-8.4from
Marti2203:fix-gh25573-ffi-constexpr

Conversation

@Marti2203

@Marti2203 Marti2203 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the crashes from #25573 (the constant value fixes are in #25724 against master).

zend_ffi_expr_div() / zend_ffi_expr_mod(): an integer division or remainder that traps (divisor zero, or INT64_MIN / -1) raises an FFI\ParserException ("Division by zero or overflow in constant expression") instead of SIGFPE. The check follows the operand conversions of zend_ffi_expr_math() (floating operands divide as floating point) and zend_ffi_expr_int_math() (floating operands of % are converted to integers first), so '\377' as divisor and 1 % 0.5 are covered; expressions that do not trap keep their values.

Test: ext/ffi/tests/gh25573.phpt; each erroring expression raises SIGFPE without the change, the controls are unchanged; ext/ffi tests 96/96.

This issue was found by an LLM-assisted tool and verified manually.

- Integer division or modulo by zero, and INT64_MIN / -1, raised SIGFPE;
  raise an FFI\ParserException instead.
- '\a' was never assigned, and the hex digits A-F/a-f in '\xNN' were
  converted to 0-5.
- sizeof of a character constant is sizeof(int), as in C.
- _Alignas(N) used the alignment of the constant's type instead of N.
- An unsigned value compared with a character constant read the
  uninitialized double member of the operand.
A CType obtained from a temporary FFI instance points into the instance's
symbol table, which is freed with it (ASan: heap-use-after-free in
FFI::alignof).
@ndossche

Copy link
Copy Markdown
Member

Thanks!
This looks to go into the right direction but it's not complete:

  • INT64_MIN / -1 hits a very similar FPE problem because that's not representable. It should be possible to test this via a VLA size
  • The modulo/division check seems incomplete ,as I believe 1.0%0 or 1%0.0 would hit the same issue. Please check.

The operands of % must have integer type. A floating operand was
converted to an integer before the remainder, so 1.0 % 0 or 1 % 0.0
raised SIGFPE.
@Marti2203

Copy link
Copy Markdown
Contributor Author

Thanks! INT64_MIN / -1 is already rejected; the test checks ((-9223372036854775807 - 1) / -1) as an array size. You're right about %: a floating operand was converted to an integer first, so 1.0 % 0 and 1 % 0.0 still raised SIGFPE. Since the operands of % must have integer type in C, the new commit rejects floating operands of % with a parser error, and the test now covers 1.0 % 0, 1 % 0.0, (-9223372036854775807.0 - 1) % -1 and 7 % 4.

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks mostly right but I'm concerned about a few backwards incompatible changes going into a near-EOL bugfix branch:

  • Alignas changes, if a user worked around the bugs then their code suddenly breaks in a patch release
  • Modulo operator now throws a parser exception
  • sizeof(character) increases from 1 to 4

There's also still some bugs/inconsistencies:

  • alignof('a') still returns 1
  • This crashes: typedef char A[((-9223372036854775807 - 1) / '\xff') ? 1 : 2];

Keep only the SIGFPE fixes on PHP-8.4: reject an integer division or
remainder by zero, and INT64_MIN / -1, after the operand conversions
that zend_ffi_expr_math() and zend_ffi_expr_int_math() perform, so a
character divisor such as '\377' and floating operands of % are covered.
The changes to constant values (_Alignas, sizeof of a character
constant, escape sequences, character comparisons) move to master.
@Marti2203

Copy link
Copy Markdown
Contributor Author

Thanks, agreed about the patch release. I've reduced this PR to the crashes: a constant expression whose integer division or remainder would trap (divisor zero, or INT64_MIN / -1) is now a parser error, checked after the operand conversions zend_ffi_expr_math() / zend_ffi_expr_int_math() do. Every expression that errors in the test raises SIGFPE on PHP-8.4 without the change, and the non-trapping cases in it give the same values as before.

The '\xff' crash came from the hex-escape fix in this PR (before it, '\xff' was 0x55). On 8.4 the same crash is reachable with '\377', which is covered now, as are % with a floating operand that converts to 0 (1 % 0.5) and (-9223372036854775807.0 - 1) % -1.

The changes to computed values (_Alignas, sizeof and __alignof__ of a character constant, which now both give 4, the '\a' and hex escapes, unsigned-vs-character comparisons, and rejecting floating operands of %) are in a separate PR against master: #25724.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants