Skip to content

AOT: integer subtraction overflow wraps instead of promoting to float — leftover of #31964 (Zend/zend_operators.h fast_long_sub_function) #32422

Description

@PurHur

Category

Language · AOT silent wrong arithmetic · leftover of #31964 (+ / *)

Problem

#31964 promoted native-long + / * overflow to float (JitLongArithOverflow). Native-long subtraction still uses LLVM sub nsw and wraps. Zend sub_function / fast_long_sub_function promotes to double (__builtin_ssubl_overflow / sign-bit check in Zend/zend_operators.h).

Repro Zend 8.2+ / VM AOT (master)
var_dump(PHP_INT_MIN - 1) float(-9.223372036854776E+18) int(9223372036854775807) (wrap)
$a = PHP_INT_MIN; var_dump($a - 1) same float wrap
var_dump(0 - PHP_INT_MIN) float(9.223372036854776E+18) wrap

Silent wrong arithmetic — AGENTS.md §3.

php-src reference

PHP implementation target

  • lib/JIT/JitLongArithOverflow.php — extend #31964 +/* to TYPE_MINUS (no new runtime/*.c)
  • lib/JIT/Helper.php — native-long ⊙ native-long minus uses overflow helper (same shape as plus)

Repro

./script/docker-exec.sh -- bash -lc 'php bin/vm.php test/repro/issue_int_sub_overflow.php'
./script/docker-exec.sh -- bash -lc 'PHP_COMPILER_HELPER_RUNTIME_O=0 php bin/compile.php -o /tmp/iso.bin test/repro/issue_int_sub_overflow.php && /tmp/iso.bin'

Done when

  • AOT PHP_INT_MIN - 1 / 0 - PHP_INT_MIN match Zend/VM floats
  • Runtime (non-folded) $a = PHP_INT_MIN; $a - 1 matches
  • ./script/aot-smoke.sh stays 8/8
  • php-src-strict; no new runtime/*.c

Refs #31964, #31968

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITbugSomething isn't workingimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions