lean4-htt/tests/lean/run/sint_div_overflow.lean
Kim Morrison 67ba4da71f
fix: avoid SIGFPE on x86_64 for signed integer division overflow (#11624)
This PR fixes a SIGFPE crash on x86_64 when evaluating `INT_MIN / -1` or
`INT_MIN % -1` for signed integer types.

On x86_64, the `idiv` instruction traps when the quotient overflows the
destination register. For signed integers, `INT_MIN / -1` produces a
result that overflows (e.g., `-2147483648 / -1 = 2147483648` which
doesn't fit in Int32). ARM64's `sdiv` instruction wraps instead of
trapping.

The fix:
- For Int8/Int16/Int32: widen to the next larger type before
dividing/modding, then truncate back
- For Int64: explicitly check for the overflow case and return the
wrapped result

Fixes #11612

🤖 Prepared with Claude Code
2025-12-13 02:42:33 +00:00

36 lines
1.2 KiB
Text

/-!
# Signed integer division overflow test
On x86_64, INT_MIN / -1 and INT_MIN % -1 cause a SIGFPE (floating point exception)
because the result overflows the destination register. This test verifies the fix.
The expected behavior (per BitVec.sdiv semantics): return minValue (wrapped result) for div,
and 0 for mod.
-/
-- Test that signed division handles overflow correctly
-- INT_MIN / -1 should return INT_MIN (wrapped)
#guard (Int8.minValue / -1 : Int8) == Int8.minValue
#guard (Int16.minValue / -1 : Int16) == Int16.minValue
#guard (Int32.minValue / -1 : Int32) == Int32.minValue
#guard (Int64.minValue / -1 : Int64) == Int64.minValue
-- Test that signed mod handles overflow correctly
-- INT_MIN % -1 should return 0
#guard (Int8.minValue % -1 : Int8) == 0
#guard (Int16.minValue % -1 : Int16) == 0
#guard (Int32.minValue % -1 : Int32) == 0
#guard (Int64.minValue % -1 : Int64) == 0
-- Also test via #eval to exercise the C runtime
#eval (Int8.minValue / -1 : Int8)
#eval (Int16.minValue / -1 : Int16)
#eval (Int32.minValue / -1 : Int32)
#eval (Int64.minValue / -1 : Int64)
#eval (Int8.minValue % -1 : Int8)
#eval (Int16.minValue % -1 : Int16)
#eval (Int32.minValue % -1 : Int32)
#eval (Int64.minValue % -1 : Int64)