fix(lift): SDIV of the most negative number by -1 gives the dividend, not a trap #1423
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1423
Loading…
Reference in a new issue
No description provided.
Delete branch "Victor/Longshot:fix/sdiv-int-min"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
idivtraps on it (SIGFPE on Linux,0xC0000095on 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.pyon main (the check is this PR's first commit). It translatessdiv x0, x1, x2andsdiv w0, w1, w2as the lift does, compiles the C and runs it withINT64_MIN / -1andINT32_MIN / -1Expected: the dividend, as on the console
Actual: the program stops with signal 8 on those inputs, at
-O0and at-O2What 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, andthird_party/a64-kitis not edited (base code plus a patch outside it, as agreed on Discord for this issue).sdiv()innx2/insn.py, reached fromemit()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/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-O0and-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++]544229a2ed90712ctests/test_sdiv_overflow.pysdiv x0, x1, x2,8000000000000000 / ffffffffffffffff8000000000000000sdiv w0, w1, w2,80000000 / ffffffff0000000080000000The Before column is the check run at this PR's first commit (
277effaa: main's sources with the new check).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.pyon Linux: 665 pass, 40 fail, 17 skipped. The 40 are the same 40 that fail on main @544229a2on 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.pyandnx2/test_leaf.pyneed 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.pyIn the game, on a build of main @
dbad7d12with 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:
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
type(scope): description) of the status line's type.can't be run by hand is in Testing's Not testable by hand, with why.
in Risks.
main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named).third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here.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 @
dbad7d12with 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.WIP: fix(lift): SDIV of the most negative number by -1 gives the dividend, not a trapto fix(lift): SDIV of the most negative number by -1 gives the dividend, not a trapBrought 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).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.