fix(lift): SDIV of the most negative number by -1 gives the dividend, not a trap #1423

Open
Victor wants to merge 2 commits from Victor/Longshot:fix/sdiv-int-min into main
Contributor

Type: fix · Risk: low · Tested on: Linux, RTX 3050 Ti Laptop 4 GB · Needs: none

Why

Lifted code that divides the most negative number by -1 kills the game with no message. AArch64's SDIV gives the dividend back there and goes on; the C the translator writes for it divides as C does, where that division is undefined, and x86's idiv traps on it (SIGFPE on Linux, 0xC0000095 on Windows). Whether the game ever divides like that is not known: if it does, it is a crash nobody can explain from the log. Fixes #361.

How to reproduce

Seen on: Linux (Arch), x86-64, gcc and clang; found by the equivalence run described in #361, which had to leave SDIV out because the lifted C stopped on these inputs
Settings and switches: none: no game is needed
Steps: python -I -B tests/test_sdiv_overflow.py on main (the check is this PR's first commit). It translates sdiv x0, x1, x2 and sdiv w0, w1, w2 as the lift does, compiles the C and runs it with INT64_MIN / -1 and INT32_MIN / -1
Expected: the dividend, as on the console
Actual: the program stops with signal 8 on those inputs, at -O0 and at -O2

What changed

nx2/insn.py, which already writes the C for the instruction forms suyu's translator lacks, now writes SDIV's too: suyu's own text with a divisor of -1 taken as a negation in unsigned arithmetic, which wraps to AArch64's value for every dividend. Every other input gives what it gave, and third_party/a64-kit is not edited (base code plus a patch outside it, as agreed on Discord for this issue).

  • lift: sdiv() in nx2/insn.py, reached from emit() for (w & 0x7FE0FC00) == 0x1AC00C00. A destination of XZR still goes to suyu, which writes no division for it. The text keeps suyu's shape (c->x[d]=(uint64_t)(...), the same register reads), so the lifter's later passes see what they saw.
  • tests: tests/test_sdiv_overflow.py (new): SDIV's X and W forms, a register read as XZR, one register for both operands, and UDIV beside them, each over 20 pairs of values at -O0 and -O2, every run a process of its own so a trap names its case.

What it costs: one compare more at each SDIV. The game's lifted code has 1,926 of them (384 on X registers, 1,542 on W), counted in a build of main @ dbad7d12. It changes the lifted C, so a game folder is translated and compiled again by its next build.

Measurements

Verdict: it passes. The check stops on main at the 8 overflow runs of 280 and passes here, and the game plays as before with it. No speed was measured: one compare at each of 1,926 divisions.

Hardware: Arch Linux (kernel 7.2.11), NVIDIA GeForce RTX 3050 Ti Laptop GPU 4 GB, driver 615.78.08, Intel Core i7-11800H, 31 GB RAM
Scene: none for the check (no game). In the game: normal play from a save, about 8,400 frames presented, closed from the window
Method: the check's output at each commit; the translated C against Unicorn 2.1.4 on the same inputs; in the game, a few minutes of play on a build of this change
How to reproduce: python -I -B tests/test_sdiv_overflow.py [--cc clang] [--cxx g++]

Before: main @ 544229a2 After: this PR @ ed90712c
tests/test_sdiv_overflow.py fails: 8 of 280 runs stop with signal 8 PASS, 280 runs
sdiv x0, x1, x2, 8000000000000000 / ffffffffffffffff the program stops (signal 8) 8000000000000000
sdiv w0, w1, w2, 80000000 / ffffffff the program stops (signal 8) 0000000080000000
In the game not run for this plays as before: about 8,400 frames, no trap, closed normally (exit status 0)

The Before column is the check run at this PR's first commit (277effaa: main's sources with the new check).

main's sources (277effaa):
sdiv x0, x1, x2 at -O0, 8000000000000000 / ffffffffffffffff: the program stopped (signal 8), AArch64 gives 8000000000000000
sdiv w0, w1, w2 at -O0, 0000000080000000 / 00000000ffffffff: the program stopped (signal 8), AArch64 gives 0000000080000000
sdiv x0, x1, x2 at -O2, 8000000000000000 / ffffffffffffffff: the program stopped (signal 8), AArch64 gives 8000000000000000
sdiv w0, w1, w2 at -O2, 0000000080000000 / 00000000ffffffff: the program stopped (signal 8), AArch64 gives 0000000080000000
FAIL: 8 of 280 runs

this PR (ed90712c):
PASS: 280 runs of 7 forms: SDIV and UDIV give what AArch64 gives, the overflows too

Flags and settings

None

Testing

Tested:

  • tests/test_sdiv_overflow.py: fails on main's sources, passes here, with gcc and clang++ and with clang and g++. It fails again with the fix broken three ways: SDIV left to suyu (8 runs), a divisor of -1 giving the dividend back for every dividend (10), and the W result sign-extended (18).

  • The values the check expects are Unicorn's: all 140 of its form and value pairs give the same in Unicorn 2.1.4. Beyond the check, the translated C for the X and W forms gives Unicorn's result on 20,000 pairs of register values, 324 of them edge pairs (a local run, not part of the PR).

  • python -I -B tests/run_all.py on Linux: 665 pass, 40 fail, 17 skipped. The 40 are the same 40 that fail on main @ 544229a2 on this PC (664 pass there), and none of them reads a file this PR changes: the 9 checks that do pass or are skipped (nx2/test_insn.py and nx2/test_leaf.py need the game's modules).

    The 40, failing on main as well

    test_bug_report_defaults.py, test_cpu_report.py, test_crt_options.py, test_damage_debug_menu.py, test_display_startup_failure.py, test_dlss_feature_size.py, test_failed_compile_keeps_objects.py, test_framegen_preset.py, test_fsr_uniform_ownership.py, test_guest_imported.py, test_hud_layout_row.py, test_import_reach.py, test_invalidate_vcore.py, test_linux_preflight.py, test_linux_pressure.py, test_linux_update.py, test_mirror_budget.py, test_missing_nup.py, test_mods_size_table.py, test_nonuniform_image_arrays.py, test_parallel_init_order.py, test_pending_guest_import.py, test_pipeline_play_cache.py, test_present_thread.py, test_profiling_mode.py, test_readback_timeout_source.py, test_replay_write_order.py, test_rumble_menu.py, test_run_all_changed.py, test_slow_frames.py, test_staging_burst.py, test_switch_table.py, test_target_image_pool.py, test_tested_configurations.py, test_upscaler_round_trip.py, test_upscaler_rows.py, test_vcore_escape.py, test_vk_occlusion_queries.py, test_vk_swapchain_compat.py, test_walk_key.py

  • In the game, on a build of main @ dbad7d12 with this change (the lifted code translated and compiled again; all 1,926 signed divisions carry the new text): several minutes of normal play from a save, about 8,400 frames. Everything played as before, nothing stopped, and the game closed normally.

Not tested:

  • Windows: nothing was built or run there. The change is Python in the lifter and the C it writes.
  • A place where the game itself divides like that: nobody knows of one, so the trap was reproduced only by the check.

Must be tested: None

Risks

What could break: every signed division in the game's lifted code goes through the new text. A mistake in it would show as wrong arithmetic anywhere; the check and the comparison with Unicorn are what stand against that.

Turning it off: revert the commit; there is no switch.

Compatibility: the lifted C changes, so the next build of a game folder translates and compiles the game again. Saves, settings, the shader and pipeline caches and the vk ABI are untouched.

Issues

Fixes #361

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what
    can't be run by hand is in Testing's Not testable by hand, with why.
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and
    in Risks.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named).
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • A game-free test added for the fix, failing on main and passing here.
**Type:** fix · **Risk:** low · **Tested on:** Linux, RTX 3050 Ti Laptop 4 GB · **Needs:** none ## Why Lifted code that divides the most negative number by -1 kills the game with no message. AArch64's SDIV gives the dividend back there and goes on; the C the translator writes for it divides as C does, where that division is undefined, and x86's `idiv` traps on it (SIGFPE on Linux, `0xC0000095` on Windows). Whether the game ever divides like that is not known: if it does, it is a crash nobody can explain from the log. Fixes #361. ## How to reproduce **Seen on:** Linux (Arch), x86-64, gcc and clang; found by the equivalence run described in #361, which had to leave SDIV out because the lifted C stopped on these inputs **Settings and switches:** none: no game is needed **Steps:** `python -I -B tests/test_sdiv_overflow.py` on main (the check is this PR's first commit). It translates `sdiv x0, x1, x2` and `sdiv w0, w1, w2` as the lift does, compiles the C and runs it with `INT64_MIN / -1` and `INT32_MIN / -1` **Expected:** the dividend, as on the console **Actual:** the program stops with signal 8 on those inputs, at `-O0` and at `-O2` ## What changed `nx2/insn.py`, which already writes the C for the instruction forms suyu's translator lacks, now writes SDIV's too: suyu's own text with a divisor of -1 taken as a negation in unsigned arithmetic, which wraps to AArch64's value for every dividend. Every other input gives what it gave, and `third_party/a64-kit` is not edited (base code plus a patch outside it, as agreed on Discord for this issue). - lift: `sdiv()` in `nx2/insn.py`, reached from `emit()` for `(w & 0x7FE0FC00) == 0x1AC00C00`. A destination of XZR still goes to suyu, which writes no division for it. The text keeps suyu's shape (`c->x[d]=(uint64_t)(...)`, the same register reads), so the lifter's later passes see what they saw. - tests: `tests/test_sdiv_overflow.py` (new): SDIV's X and W forms, a register read as XZR, one register for both operands, and UDIV beside them, each over 20 pairs of values at `-O0` and `-O2`, every run a process of its own so a trap names its case. What it costs: one compare more at each SDIV. The game's lifted code has 1,926 of them (384 on X registers, 1,542 on W), counted in a build of main @ dbad7d12. It changes the lifted C, so a game folder is translated and compiled again by its next build. ## Measurements **Verdict:** it passes. The check stops on main at the 8 overflow runs of 280 and passes here, and the game plays as before with it. No speed was measured: one compare at each of 1,926 divisions. Hardware: Arch Linux (kernel 7.2.11), NVIDIA GeForce RTX 3050 Ti Laptop GPU 4 GB, driver 615.78.08, Intel Core i7-11800H, 31 GB RAM Scene: none for the check (no game). In the game: normal play from a save, about 8,400 frames presented, closed from the window Method: the check's output at each commit; the translated C against Unicorn 2.1.4 on the same inputs; in the game, a few minutes of play on a build of this change How to reproduce: `python -I -B tests/test_sdiv_overflow.py [--cc clang] [--cxx g++]` | | Before: main @ 544229a2 | After: this PR @ ed90712c | |---|---|---| | `tests/test_sdiv_overflow.py` | fails: 8 of 280 runs stop with signal 8 | PASS, 280 runs | | `sdiv x0, x1, x2`, `8000000000000000 / ffffffffffffffff` | the program stops (signal 8) | `8000000000000000` | | `sdiv w0, w1, w2`, `80000000 / ffffffff` | the program stops (signal 8) | `0000000080000000` | | In the game | not run for this | plays as before: about 8,400 frames, no trap, closed normally (exit status 0) | The Before column is the check run at this PR's first commit (277effaa: main's sources with the new check). ``` main's sources (277effaa): sdiv x0, x1, x2 at -O0, 8000000000000000 / ffffffffffffffff: the program stopped (signal 8), AArch64 gives 8000000000000000 sdiv w0, w1, w2 at -O0, 0000000080000000 / 00000000ffffffff: the program stopped (signal 8), AArch64 gives 0000000080000000 sdiv x0, x1, x2 at -O2, 8000000000000000 / ffffffffffffffff: the program stopped (signal 8), AArch64 gives 8000000000000000 sdiv w0, w1, w2 at -O2, 0000000080000000 / 00000000ffffffff: the program stopped (signal 8), AArch64 gives 0000000080000000 FAIL: 8 of 280 runs this PR (ed90712c): PASS: 280 runs of 7 forms: SDIV and UDIV give what AArch64 gives, the overflows too ``` ### Flags and settings None ## Testing **Tested:** - `tests/test_sdiv_overflow.py`: fails on main's sources, passes here, with gcc and clang++ and with clang and g++. It fails again with the fix broken three ways: SDIV left to suyu (8 runs), a divisor of -1 giving the dividend back for every dividend (10), and the W result sign-extended (18). - The values the check expects are Unicorn's: all 140 of its form and value pairs give the same in Unicorn 2.1.4. Beyond the check, the translated C for the X and W forms gives Unicorn's result on 20,000 pairs of register values, 324 of them edge pairs (a local run, not part of the PR). - `python -I -B tests/run_all.py` on Linux: 665 pass, 40 fail, 17 skipped. The 40 are the same 40 that fail on main @ 544229a2 on this PC (664 pass there), and none of them reads a file this PR changes: the 9 checks that do pass or are skipped (`nx2/test_insn.py` and `nx2/test_leaf.py` need the game's modules). <details><summary>The 40, failing on main as well</summary> `test_bug_report_defaults.py`, `test_cpu_report.py`, `test_crt_options.py`, `test_damage_debug_menu.py`, `test_display_startup_failure.py`, `test_dlss_feature_size.py`, `test_failed_compile_keeps_objects.py`, `test_framegen_preset.py`, `test_fsr_uniform_ownership.py`, `test_guest_imported.py`, `test_hud_layout_row.py`, `test_import_reach.py`, `test_invalidate_vcore.py`, `test_linux_preflight.py`, `test_linux_pressure.py`, `test_linux_update.py`, `test_mirror_budget.py`, `test_missing_nup.py`, `test_mods_size_table.py`, `test_nonuniform_image_arrays.py`, `test_parallel_init_order.py`, `test_pending_guest_import.py`, `test_pipeline_play_cache.py`, `test_present_thread.py`, `test_profiling_mode.py`, `test_readback_timeout_source.py`, `test_replay_write_order.py`, `test_rumble_menu.py`, `test_run_all_changed.py`, `test_slow_frames.py`, `test_staging_burst.py`, `test_switch_table.py`, `test_target_image_pool.py`, `test_tested_configurations.py`, `test_upscaler_round_trip.py`, `test_upscaler_rows.py`, `test_vcore_escape.py`, `test_vk_occlusion_queries.py`, `test_vk_swapchain_compat.py`, `test_walk_key.py` </details> - In the game, on a build of main @ dbad7d12 with this change (the lifted code translated and compiled again; all 1,926 signed divisions carry the new text): several minutes of normal play from a save, about 8,400 frames. Everything played as before, nothing stopped, and the game closed normally. **Not tested:** - Windows: nothing was built or run there. The change is Python in the lifter and the C it writes. - A place where the game itself divides like that: nobody knows of one, so the trap was reproduced only by the check. **Must be tested:** None ## Risks **What could break:** every signed division in the game's lifted code goes through the new text. A mistake in it would show as wrong arithmetic anywhere; the check and the comparison with Unicorn are what stand against that. **Turning it off:** revert the commit; there is no switch. **Compatibility:** the lifted C changes, so the next build of a game folder translates and compiles the game again. Saves, settings, the shader and pipeline caches and the vk ABI are untouched. ## Issues Fixes #361 ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [x] Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what can't be run by hand is in Testing's Not testable by hand, with why. - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and in Risks. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`. - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [x] A game-free test added for the fix, failing on `main` and passing here.
The C suyu's translator writes for SDIV divides as C does, and x86's idiv traps when the quotient does not fit: lifted
code that divides INT64_MIN or INT32_MIN by -1 kills the game with no message (#361). The check runs SDIV's translated
C in processes of its own and fails here on the 8 overflow runs of 280 (signal 8).

Assisted-by: Claude Code
Co-Authored-By: Claude Fable 5.2 <[email protected]>
fix(lift): SDIV of the most negative number by -1 gives the dividend, not a trap
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 9s
ed90712c10
suyu's C for SDIV divides as C does. INT64_MIN / -1 and INT32_MIN / -1 are undefined there, and x86's idiv traps on
them (#DE): lifted code that divides like that kills the game with no message (SIGFPE; 0xC0000095 on Windows), where
the console gives the dividend back and goes on (#361).

nx2/insn.py, which already writes the C for the forms suyu lacks, writes SDIV's too: suyu's text with a divisor of -1
taken as a negation in unsigned arithmetic, which wraps to AArch64's value for every dividend. Every other input
gives what it gave. The vendored kit is not edited.

Assisted-by: Claude Code
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Author
Contributor

Tested in game: Linux (Arch, kernel 7.2.11), NVIDIA GeForce RTX 3050 Ti Laptop 4 GB, driver 615.78.08. A build of main @ dbad7d12 with this change, the lifted code translated and compiled again (all 1,926 signed divisions carry the new text). Several minutes of normal play from a save, about 8,400 frames: everything played as before, nothing stopped, and the game closed normally.

Tested in game: Linux (Arch, kernel 7.2.11), NVIDIA GeForce RTX 3050 Ti Laptop 4 GB, driver 615.78.08. A build of main @ dbad7d12 with this change, the lifted code translated and compiled again (all 1,926 signed divisions carry the new text). Several minutes of normal play from a save, about 8,400 frames: everything played as before, nothing stopped, and the game closed normally.
Victor changed title from WIP: fix(lift): SDIV of the most negative number by -1 gives the dividend, not a trap to fix(lift): SDIV of the most negative number by -1 gives the dividend, not a trap 2026-11-17 14:42:57 +00:00
Owner

Brought up to the ready-for-review standard (#1454): the checklist's Everyone list has two new boxes, added here from what this PR's description and comments show (please check they match what you know).

  • Tested on my own PC: ticked: Testing's Tested and an author comment: an in-game run on Arch Linux with an RTX 3050 Ti Laptop 4 GB.
  • Nothing gets worse that I know of: ticked: Measurements names the one compare per signed division as the only cost and says no speed was measured for it.
Brought up to the [ready-for-review standard](https://git.magicbean.dev/MagicBeanDev/Longshot/src/branch/main/CONTRIBUTING.md#ready-for-review-out-of-wip) (#1454): the checklist's Everyone list has two new boxes, added here from what this PR's description and comments show (please check they match what you know). - *Tested on my own PC*: ticked: Testing's Tested and an author comment: an in-game run on Arch Linux with an RTX 3050 Ti Laptop 4 GB. - *Nothing gets worse that I know of*: ticked: Measurements names the one compare per signed division as the only cost and says no speed was measured for it.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 9s
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/sdiv-int-min:Victor-fix/sdiv-int-min'
git switch 'Victor-fix/sdiv-int-min'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!1423
No description provided.