8000 arm, arm64: return the architectural zero result for integer division by zero by DORA-B · Pull Request #104 · angr/vex · GitHub
[go: up one dir, main page]

Skip to content
8000

arm, arm64: return the architectural zero result for integer division by zero - #104

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

DORA-B wants to merge 1 commit into
angr:masterfrom
DORA-B:fix/arm-a-profile-divzero

Conversation

@DORA-B
@DORA-B DORA-B commented Sep 25, 2026
Copy link
Copy Markdown

Summary

Arm integer division by zero has a defined zero result in the A-profile
execution environments covered here. The A32, Thumb-2, and AArch64 frontends
currently pass a zero divisor directly to generic VEX division operations.
Consequently, an IR consumer can raise a division exception instead of
returning the required result.

This change handles the zero divisor in the frontend: divide by a safe
nonzero operand internally, then select zero as the architectural result.
It covers SDIV and UDIV in A32 and Thumb-2, and both W/X forms in AArch64.

Architectural definition

Direct instruction references are available in the following third-party
HTML mirrors of Arm's instruction descriptions (not Arm-hosted pages):

  • A64: SDIV
    and UDIV
    (Arm release 2026-03).
  • A32/T32: SDIV
    and UDIV
    (Arm pseudocode release 2023-06; each page includes A32 and T32 encodings).

All four pages' Operation sections explicitly select zero when the divisor is
zero. These links were checked against the instruction contents, rather than
only the destination URL.

For a zero divisor, the required quotient is zero. This is an instruction-level
rule, not the undefined behavior of division by zero in C or C++.

Arm's explanation of profile-specific division behavior
also explicitly distinguishes A-profile's zero result from the configurable
trapping behavior in R/M profiles. This patch does not add support for
SCTLR.DZ or CCR.DIV_0_TRP, and must not be read as implementing those
system-level trapping configurations. Integer division does not update the
condition flags; existing conditional destination writes are preserved.

Root cause

The frontends lower these instructions directly to Iop_DivS32, Iop_DivU32,
Iop_DivS64, or Iop_DivU64. Those generic operations do not encode Arm's
special zero-divisor result. PyVEX's ZeroDivisionPostProcessor can insert an
Ijk_SigFPE_IntDiv exit for that operand, and angr then reports
SimZeroDivisionException.

Selecting zero only after an unsafe division is insufficient: the division
itself must also receive a nonzero divisor.

Reproduction

import angr

for name, word in (("sdiv", 0x9AC20C20), ("udiv", 0x9AC20820)):
    # sdiv/udiv x0, x1, x2
    project = angr.load_shellcode(
        word.to_bytes(4, "little"), arch="aarch64", load_address=0x1000
    )
    state = project.factory.blank_state(addr=0x1000)
    state.regs.x1 = 5
    state.regs.x2 = 0
    try:
        out = state.step(num_inst=1).flat_success
8000
ors[0]
        print(name, out.solver.eval(out.regs.x0))
    except angr.errors.SimZeroDivisionException:
        print(name, "unexpected divide-by-zero exception")

Expected: sdiv 0 and udiv 0. Without the fix, both raise a divide-by-zero
exception. The patch must be used through a PyVEX build containing the changed
VEX frontends.

Fix

For each affected instruction, construct the following operations at its
operand width:

is_zero     = divisor == 0
safe_divisor = is_zero ? 1 : divisor
quotient    = signed_or_unsigned_divide(dividend, safe_divisor)
result      = is_zero ? 0 : quotient

For a zero divisor, the internal division receives one and the selected result
is zero. For a nonzero divisor, the original operands and division operation
are retained. The generic divide-by-zero guard remains intact, but its operand
is provably nonzero. Existing destination-width handling and conditional
register writes are unchanged.

Relation to the RISC-V fix

My related RISC-V division fix, PR #84,
addresses the same mismatch between generic division behavior and a guest
architecture's defined exceptional results. It is a related implementation,
not a dependency or a substitute for the Arm specification.

The results intentionally differ:

Instruction Required result for a zero divisor
Arm SDIV / UDIV in the scope above Zero quotient
RISC-V DIV / DIVU All-ones quotient
RISC-V REM / REMU Original dividend

The RISC-V values are specified in the M-extension division definition.
Both changes use a safe internal divisor and then select the result required
by their respective architecture. Neither removes divide exceptions globally
or changes the rules for architectures that require trapping.

The RISC-V PR additionally guards signed overflow (INT_MIN / -1). This Arm
patch is specifically a divide-by-zero repair: it leaves nonzero-divisor
lowering unchanged and does not claim to resolve generic VEX signed-overflow
semantics. That case requires a separate explicit guard to establish the same
backend-independent guarantee.

@DORA-B
DORA-B marked this pull request as ready for review September 25, 2026 03:58
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

0