mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #1393 from xsvm/fix/relinker-rewriter-reference-sites
fix(relinker): classify rewrite reference sites with the instruction decoder
This commit is contained in:
@@ -215,7 +215,6 @@ inline constexpr std::uint8_t SibBaseDisp32 = 0x05;
|
||||
inline constexpr std::size_t Disp8Size = 1;
|
||||
inline constexpr std::size_t Disp32Size = 4;
|
||||
|
||||
inline constexpr std::size_t Rel32InstructionLength = 5;
|
||||
|
||||
inline constexpr std::size_t Vex2PrefixLength = 2;
|
||||
inline constexpr std::size_t Vex3PrefixLength = 3;
|
||||
|
||||
@@ -14,78 +14,20 @@ X64InstructionRewriter::ReferenceSite X64InstructionRewriter::ClassifyInstructio
|
||||
const std::uint8_t* data,
|
||||
std::size_t length
|
||||
) {
|
||||
std::size_t pos = 0;
|
||||
const auto info = X64InstructionDecoder{}.DecodeInstruction(data, length);
|
||||
|
||||
while (pos < length) {
|
||||
const std::uint8_t b = data[pos];
|
||||
if ((b >= RexMin && b <= RexMax) ||
|
||||
b == PrefixLock || b == PrefixRepne || b == PrefixRep ||
|
||||
b == PrefixSegCs || b == PrefixSegSs || b == PrefixSegDs ||
|
||||
b == PrefixSegEs || b == PrefixSegFs || b == PrefixSegGs ||
|
||||
b == PrefixOperandSize || b == PrefixAddressSize) {
|
||||
pos += 1;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
if (info.HasRipRelativeDisp) {
|
||||
return {ReferenceKind::RipRelativeDisp32, info.RipRelativeDispOffset};
|
||||
}
|
||||
|
||||
if (pos >= length) {
|
||||
if (!info.HasBranchTarget) {
|
||||
return {ReferenceKind::None, 0};
|
||||
}
|
||||
|
||||
std::uint8_t opcode = data[pos];
|
||||
const std::size_t opcodeOffset = pos;
|
||||
pos += 1;
|
||||
bool twoByteOpcode = false;
|
||||
|
||||
if (opcode == TwoByteOpcodeEscape) {
|
||||
if (pos >= length) {
|
||||
return {ReferenceKind::None, 0};
|
||||
}
|
||||
twoByteOpcode = true;
|
||||
opcode = data[pos];
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
if (!twoByteOpcode) {
|
||||
if (opcode == OneByteCallRel32 || opcode == OneByteJmpRel32) {
|
||||
if (length >= opcodeOffset + Rel32InstructionLength) {
|
||||
return {ReferenceKind::Rel32Branch, pos};
|
||||
}
|
||||
return {ReferenceKind::None, 0};
|
||||
}
|
||||
|
||||
if (opcode == OneByteJmpRel8 || (opcode >= OneByteJccRel8Min && opcode <= OneByteJccRel8Max)) {
|
||||
if (length >= pos + Disp8Size) {
|
||||
return {ReferenceKind::Rel8Branch, pos};
|
||||
}
|
||||
return {ReferenceKind::None, 0};
|
||||
}
|
||||
} else {
|
||||
if (opcode >= TwoByteJccRel32Min && opcode <= TwoByteJccRel32Max) {
|
||||
if (length >= pos + Disp32Size) {
|
||||
return {ReferenceKind::Rel32Branch, pos};
|
||||
}
|
||||
return {ReferenceKind::None, 0};
|
||||
}
|
||||
}
|
||||
|
||||
if (pos >= length) {
|
||||
return {ReferenceKind::None, 0};
|
||||
}
|
||||
|
||||
const std::uint8_t modrm = data[pos];
|
||||
const std::uint8_t mod = static_cast<std::uint8_t>((modrm >> ModRmModShift) & ModRmModMask);
|
||||
const std::uint8_t rm = static_cast<std::uint8_t>(modrm & ModRmRmMask);
|
||||
|
||||
if (mod == ModRmModIndirect && rm == ModRmRmRipRelative) {
|
||||
const std::size_t dispOffset = pos + 1;
|
||||
if (length >= dispOffset + Disp32Size) {
|
||||
return {ReferenceKind::RipRelativeDisp32, dispOffset};
|
||||
}
|
||||
}
|
||||
|
||||
return {ReferenceKind::None, 0};
|
||||
const bool rel32 = info.IsTwoByteOpcode || info.Opcode == OneByteCallRel32 || info.Opcode == OneByteJmpRel32;
|
||||
return rel32
|
||||
? ReferenceSite{ReferenceKind::Rel32Branch, info.Length - Disp32Size}
|
||||
: ReferenceSite{ReferenceKind::Rel8Branch, info.Length - Disp8Size};
|
||||
}
|
||||
|
||||
std::int32_t X64InstructionRewriter::ReadInt32(const std::uint8_t* data) {
|
||||
|
||||
@@ -723,6 +723,19 @@ void rewriterStrayRex() {
|
||||
require(rewritten.Bytes == expected, "Branch with a stray REX was not adjusted by a length-changing rewrite");
|
||||
}
|
||||
|
||||
void rewriterReferenceSites() {
|
||||
const std::vector<std::tuple<Bytes, std::uint64_t, Bytes>> cases = {
|
||||
{{0x90, 0x48, 0xB8, 0x05, 0xF0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xC3}, 0,
|
||||
{0x66, 0x90, 0x48, 0xB8, 0x05, 0xF0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xC3}},
|
||||
{{0xC5, 0xF9, 0x6F, 0x05, 0x01, 0x00, 0x00, 0x00, 0x90, 0xC3}, 8,
|
||||
{0xC5, 0xF9, 0x6F, 0x05, 0x02, 0x00, 0x00, 0x00, 0x66, 0x90, 0xC3}},
|
||||
{{0x66, 0x0F, 0x38, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x90, 0xC3}, 9,
|
||||
{0x66, 0x0F, 0x38, 0x00, 0x05, 0x02, 0x00, 0x00, 0x00, 0x66, 0x90, 0xC3}},
|
||||
{{0xE2, 0x01, 0x90, 0xC3}, 2, {0xE2, 0x02, 0x66, 0x90, 0xC3}}};
|
||||
for (const auto& [code, offset, expected] : cases)
|
||||
require(Codegen::X64InstructionRewriter{}.Rewrite(code, {offset, {0x66, 0x90}}).Bytes == expected, "Length-changing rewrite adjusted an immediate or missed a VEX, three-byte or LOOP reference");
|
||||
}
|
||||
|
||||
void converterFailureOffsets() {
|
||||
const auto converter = Codegen::MakeAmd64OnlyConverter();
|
||||
const auto file = segmentFixture();
|
||||
@@ -1323,6 +1336,7 @@ int main() {
|
||||
converterReciprocal();
|
||||
converterStrayRex();
|
||||
rewriterStrayRex();
|
||||
rewriterReferenceSites();
|
||||
converterFailureOffsets();
|
||||
linuxPlacement();
|
||||
linuxRipRelativePlacement();
|
||||
|
||||
@@ -282,7 +282,7 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
- [Per-stage descriptor count](../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - a pixel shader's colour attachments are not counted against `maxPerStageResources`, so its buffers and images can fit the limit while the pipeline exceeds it
|
||||
- [v_rcp, v_rsq, v_sqrt, v_log, v_exp, v_sin and v_cos](../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) (f32 and f16) use the driver's approximation for finite results, not the console's: the low bits can differ. Only NaN, invalid, zero and infinite results and the exact sin/cos zeros are chosen on the bits. v_sin_f32/v_cos_f32 with inputs beyond ±256 cycles were not compared with the console.
|
||||
- `--to-intel` does not lower RDPRU/MCOMMIT; the [matcher](../../core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp) fails the relink instead.
|
||||
- The length-changing path of the [instruction rewriter](../../core/relinker/codegen/src/x86/X64InstructionRewriter.cpp) is not used by the [converter](../../core/relinker/codegen/src/Amd64OnlyConverter.cpp): it does not adjust VEX/0F38/0F3A RIP-relative operands, data-to-code references (relocations, FDEs, jump tables) or segment sizes, so every substitution keeps the instruction length.
|
||||
- The length-changing path of the [instruction rewriter](../../core/relinker/codegen/src/x86/X64InstructionRewriter.cpp) is not used by the [converter](../../core/relinker/codegen/src/Amd64OnlyConverter.cpp): it does not adjust data-to-code references (relocations, FDEs, jump tables) or segment sizes, so every substitution keeps the instruction length.
|
||||
- [DB_SHADER_CONTROL.CONSERVATIVE_Z_EXPORT](../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp) (libSceAgcDriver) becomes `DepthLess`/`DepthGreater`. For a pixel shader that breaks the promise, the host driver decides whether its interpolated Z is depth-tested before shading; what the console does then was not measured.
|
||||
- [DB_SHADER_CONTROL.PRIMITIVE_ORDERED_PIXEL_SHADER](../../core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) (shader recompiler) - the host pixel interlock spans the whole shader, not only up to `s_sendmsg` ORDERED_PS_DONE.
|
||||
- [POPS collision SGPRs](../../core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) (shader recompiler) - the collision wave ID and intrawave collision mask read 0, so a title skips its POPS wait and runs its per-collision loop once.
|
||||
|
||||
Reference in New Issue
Block a user