Repository navigation
Conversation
DORA-B
added a commit
to DORA-B/vex
that referenced
this pull request
Sep 25, 2026
Retain the current RISC-V division operators and explicitly handle zero divisors and signed overflow before evaluating generic division.
RISC-V's M extension defines divide-by-zero and the signed overflow corner as NON-TRAPPING with fixed results: div/0=-1, divu/0=all-ones, rem/0=remu/0= dividend, div(INT_MIN,-1)=INT_MIN, rem(INT_MIN,-1)=0. pyvex's generic ZeroDivisionPostProcessor inserts an Ijk_SigFPE_IntDiv exit before every Div/Mod (correct for x86/ARM, wrong for RISC-V), and the defined values were never produced. Fix in the frontend: divide by a forced-nonzero divisor and select the RISC-V-defined value when the divisor is zero; the Div operand is then provably nonzero so the auto-inserted guard is always-false (dead) and no host divide-by- zero occurs. Overflow corner handled by the wrapping divmod. Verified by rebuilding pyvex: all 8 forms (div/divu/rem/remu + w) return the spec values for zero divisor and the INT_MIN/-1 corner, and remain correct over the nonzero domain (0/1000 mismatch each).
DORA-B
force-pushed
the
fix/riscv64-divzero
branch
from
November 21, 2026 03:19
d45f548 to
7daed63
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RISC-V integer division by zero must return a defined value without trapping: DIV/DIVU return all ones, and REM/REMU return the dividend. Signed overflow (
INT_MIN / -1) must return INT_MIN for DIV and zero for REM. The word forms sign-extend their 32-bit results.The frontend previously passed these operands to generic VEX operations. PyVEX adds a divide-by-zero exit, and VEX leaves unrepresentable signed division undefined. This change supplies a safe divisor for both exceptional cases, then selects the RISC-V result explicitly. Ordinary inputs retain the existing operations.
This PR now incorporates current master, including the remainder-operator fixes imported by #98, and no longer depends on #83.
Reproducer
Before the fix, these executions raise a divide-by-zero exception. Expected results are
divu 0xffffffffffffffffandrem 0x5.Validation
Current PyVEX builds successfully with the repaired frontend. A standalone concrete evaluator passed 288 lifted-IR cases across all eight XLEN/word forms, covering zero divisors, signed overflow, ordinary edge cases, random operands, and word-result sign extension. The generated IR explicitly supplies a nonzero safe divisor for both exceptional inputs. This concrete suite does not claim end-to-end execution on every VEX host backend.