mirror of
https://github.com/mitch030504/Wiicompiled_VR_Frame.git
synced 2026-10-06 14:00:26 +02:00
* Scope Kamek bl-patch LR-continuation detection to genuine skip-return targets Fix crash from Kamek skip-return hooks (Item Rain crash) (#182) added every Kamek BranchLink patch target to lrContinuationCallTargets unconditionally, with no filter analogous to the RetroWfcHookSetsLinkRegister check already used for RetroWFC hooks. Since bl is the ordinary PowerPC call instruction, this made the codegen treat effectively every patched call in the mod as a potential skip-return hook, forcing conservative handling (full register reload, disabled resident-call fast paths, local LR-continuation dispatch tables) onto thousands of calls that just return normally. For Retro Rewind this inflated total translated mod size by +42% (1,414,327 -> 2,005,284 lines), concentrated in ~10 unrelated overlay functions that happened to call a patched target, and was enough to make one aggregate build shard pathologically slow to compile (hangs Linux CI). Instead, only mark a bl target as LR-continuation-aware if a lightweight discovery-only decode of its own body actually finds evidence of skip-return behavior via DiscoverLrRelativeIndirectJumpOffsets. Falls back to the conservative (old) behavior if a target can't be statically analyzed, so no skip-return case is silently missed. Verified against the real Retro Rewind mod: total mod size returns to 1,416,350 lines (+0.14% vs. pre-fix, down from +42%), all 6 genuinely new continuation functions from the original fix are preserved, zero functions lost, and all 609 existing translator tests still pass. * Distinguish exhausted from truncated LR-relative offset search CodeRabbit flagged that TargetExhibitsLrSkipReturn (added in ad2d4e7) treated an empty DiscoverLrRelativeIndirectJumpOffsets result as a verified "this target never skip-returns," but the analysis silently drops any path state once more than MaxStatesPerInstruction (16) distinct states reach one instruction - a bctr/return on a dropped state can never contribute its offset, so an empty result could be an incomplete search rather than a real negative. Treating every capped case as "skip-return possible" outright was rejected as too broad a fallback given how conservative/expensive that path already is. Instead: raise MaxStatesPerInstruction 16 -> 512 (an arbitrary conservative bound to begin with, not something correctness depended on) so genuinely branchy functions have far more headroom to reach an exhaustive answer, and give DiscoverLrRelativeIndirectJumpOffsets an optional onStateCapExceeded callback that fires exactly when a state is dropped. TargetExhibitsLrSkipReturn now only falls back to the conservative "treat as skip-return" answer when the search both found nothing and the cap was actually hit during that run - not whenever the cap merely exists - so a target is trusted as clean once the search genuinely exhausts it. Verified: all 609 translator tests pass, and a full translate-mod run against the real Retro Rewind mod produces byte-for-byte identical output to the prior fix (same 4,065 functions, 1,416,350 total lines) - confirming the 16-state cap was never actually the limiting factor in practice and this change is a pure safety-net closure, not a behavior change for this mod. * Add LR continuation regression tests * Refine LR continuation hook analysis --------- Co-authored-by: patchzyy <64382339+patchzyy@users.noreply.github.com>
79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using Translator.Core.Disassembly;
|
|
using Translator.Core.Mods;
|
|
|
|
namespace Translator.Tests;
|
|
|
|
public sealed class LrRelativeAnalysisCompletenessTests
|
|
{
|
|
[Fact]
|
|
public void EmptyResultReportsWhenPathStatesWereDropped()
|
|
{
|
|
var capped = false;
|
|
var offsets = Analyze(SyntheticLrHookFactory.ManyOrdinaryReturnPaths(), () => capped = true);
|
|
|
|
Assert.Empty(offsets);
|
|
Assert.True(capped, "An empty result from capped exploration must not prove an ordinary return.");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(20)]
|
|
public void ExhaustiveAnalysisDoesNotReportAStateCap(int offset)
|
|
{
|
|
var capped = false;
|
|
var offsets = Analyze(
|
|
[
|
|
0x7D8802A6u, // mflr r12
|
|
0x398C0000u | (ushort)offset, // addi r12,r12,offset
|
|
0x7D8803A6u, // mtlr r12
|
|
0x4E800020u // blr
|
|
], () => capped = true);
|
|
|
|
Assert.False(capped);
|
|
Assert.Equal(offset == 0 ? Array.Empty<int>() : [offset], offsets);
|
|
}
|
|
|
|
[Fact]
|
|
public void CappedArmDoesNotDiscardAnOffsetFoundOnAnotherArm()
|
|
{
|
|
var capped = false;
|
|
var offsets = Analyze(
|
|
[
|
|
0x7D8802A6u, // +00: mflr r12
|
|
0x2C030000u, // +04: cmpwi r3,0
|
|
0x41820010u, // +08: beq +0x18
|
|
0x398C0014u, // +0C: addi r12,r12,20
|
|
0x7D8803A6u, // +10: mtlr r12
|
|
0x4E800020u, // +14: blr
|
|
.. SyntheticLrHookFactory.ManyOrdinaryReturnPaths() // +18
|
|
], () => capped = true);
|
|
|
|
Assert.True(capped);
|
|
Assert.Equal(new[] { 20 }, offsets);
|
|
}
|
|
|
|
private static int[] Analyze(uint[] words, Action onStateCapExceeded)
|
|
{
|
|
var instructions = words.Select((word, index) =>
|
|
PpcDecoder.Decode(0x80010000u + (uint)index * 4, word)).ToArray();
|
|
return ContinuationPlanner.DiscoverLrRelativeIndirectJumpOffsets(instructions, onStateCapExceeded).ToArray();
|
|
}
|
|
}
|
|
|
|
internal static class SyntheticLrHookFactory
|
|
{
|
|
public static uint[] ManyOrdinaryReturnPaths()
|
|
{
|
|
// 1,024 possible states exceed the 512-state cap without an unbounded loop.
|
|
var words = new List<uint> { 0x7FE802A6u }; // mflr r31
|
|
for (uint register = 3; register <= 12; register++)
|
|
{
|
|
words.Add(0x2C000000u | (register << 16)); // cmpwi rN,0
|
|
words.Add(0x41820008u); // beq +8
|
|
words.Add(0x38000004u | (register << 21) | (31u << 16)); // addi rN,r31,4
|
|
}
|
|
words.Add(0x4E800020u); // blr with unchanged LR
|
|
return words.ToArray();
|
|
}
|
|
}
|