Repository navigation
Decode the real delay slot when a function's final branch has its slot at the next function - #415
Open
0xLeathery wants to merge 2 commits into
Open
0xLeathery wants to merge 2 commits into
0xLeathery wants to merge 2 commits into
Conversation
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.
Problem
PS2Recompiler::decodeFunctiondecodes[function.start, function.end). The function emitter uses the next decoded instruction as each branch's delay slot. If that instruction isn't in the list, the emitter substitutes a synthetic NOP (makeSyntheticDelaySlot).When the analyzer starts another function exactly at the delay slot of a function's final branch, the first function's
endis that slot's address. The real instruction there is then silently dropped.In Sly 2 (SCUS-97316) this happens in 4 functions. One of them ends in
jr $raat0x10183cwithaddiu $sp, $sp, 0xA0in its delay slot at0x101840, where another function begins. The recompiled routine returned with$sp0xA0 too low. That corrupted its caller's frame and sent a cleanup loop running through the whole address space.Change
decodeFunction(read, config patch, decode, MMIO annotation) now lives in onedecodeInstructionAthelper. It behaves the same for normal instructions.function.end, and that address is valid, the real instruction there is decoded with the same patch and MMIO handling and appended. The emitter's existing decoded-delay-slot path then uses it.function.end, the function's registered range, labels and entries are unchanged. Every consumer of the decoded list checks< function.endbefore using an address, so the slot gets no label, case or registration in the earlier function. Branch-likely still runs the slot only on the taken path.Testing
New tests in
ps2_recompiler_tests.cpp:jr $rawhoseaddiu $sp, $sp, 0x20slot starts the next function now runs that slot.j,jalandbne.The first three failed before the change.
The tests use
single_file_output. That path has an existing race in multi-threaded combined output: "combined output completion queue is missing index N". It shows up about 2 in 6 runs onmainas well. The test helper pinsoutput_worker_threads = 1, and the race should get its own issue.ps2x_tests: 450/450, in 8 consecutive runs.beqlfinal branch, an MMIO slot, and a production standalone entry wrapper all behave correctly.Known limits, not changed here
Built and tested on macOS arm64 (Apple clang).