Skip to content

riscv64: do not trap on division by zero (RISC-V defines it) - #84

Open
DORA-B wants to merge 1 commit into
angr:masterfrom
DORA-B:fix/riscv64-divzero
Open

DORA-B wants to merge 1 commit into
angr:masterfrom
DORA-B:fix/riscv64-divzero

Conversation

@DORA-B

@DORA-B DORA-B commented Jul 26, 2026 •

Copy link
Copy Markdown

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

import angr
import archinfo

for name, encoding in [("divu", "b3527302"), ("rem", "b3627302")]:
    project = angr.load_shellcode(bytes.fromhex(encoding), arch=archinfo.ArchRISCV64())
    state = project.factory.blank_state(addr=0)
    state.regs.x6 = 5
    state.regs.x7 = 0
    out = state.step(num_inst=1).successors[0]
    print(name, hex(out.solver.eval(out.regs.x5)))

Before the fix, these executions raise a divide-by-zero exception. Expected results are divu 0xffffffffffffffff and rem 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.

@DORA-B DORA-B changed the title Fix/riscv64 divzero riscv64: do not trap on division by zero (RISC-V defines it) Jul 26, 2026
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
DORA-B force-pushed the fix/riscv64-divzero branch from d45f548 to 7daed63 Compare November 21, 2026 03:19
@DORA-B

DORA-B commented Nov 21, 2026

Copy link
Copy Markdown
Author

Hi! I rebased this PR and #85, #87, #88, and #104 onto current master. The targeted tests passed locally on Linux. Could someone approve the Build workflows when they have a chance? Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant