mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(relinker): keep the original rax and rcx in ALU TLS reads
loadPointer overwrites rax and rcx, so a destination of rax or rcx computed the operation against the thread pointer or the TLS index instead of the original register. Restore the destination before the operation for both, and include rcx in the byte-level cases, which previously covered only rax, rbx, r8 and r13. Add self-checking execution cases for rax, rcx and rbx destinations: each verifies the result, the preservation of the other two registers and the ZF and CF bits.
This commit is contained in:
@@ -163,14 +163,26 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector<std::uint8_t>& source, co
|
||||
patchAccess(sections, access, code.GetRva());
|
||||
code.Emit({0x48, 0x8d, 0x64, 0x24, 0x80});
|
||||
if (access.AluOpcode != 0 && access.Register == 0) {
|
||||
code.Emit({0x52});
|
||||
code.Emit({0x51});
|
||||
code.Emit({0x52});
|
||||
code.Emit({0x50});
|
||||
loadPointer();
|
||||
code.Emit({0x48, 0x8b, 0x90});
|
||||
code.U32(access.Displacement);
|
||||
code.Emit({0x58});
|
||||
code.Emit({0x48, access.AluOpcode, 0xc2});
|
||||
code.Emit({0x59});
|
||||
code.Emit({0x5a});
|
||||
code.Emit({0x59});
|
||||
} else if (access.AluOpcode != 0 && access.Register == 1) {
|
||||
code.Emit({0x51});
|
||||
code.Emit({0x50});
|
||||
loadPointer();
|
||||
code.Emit({0x48, 0x8b, 0x80});
|
||||
code.U32(access.Displacement);
|
||||
code.Emit({0x48, 0x8b, 0x4c, 0x24, 0x08});
|
||||
code.Emit({0x48, access.AluOpcode, 0xc8});
|
||||
code.Emit({0x58});
|
||||
code.Emit({0x48, 0x8d, 0x64, 0x24, 0x08});
|
||||
} else {
|
||||
const bool preserveAccumulator = access.StoreImmediate || access.Register != 0;
|
||||
const bool preserveCounter = access.Register != 1;
|
||||
|
||||
@@ -37,6 +37,71 @@ def fs_alu(opcode, register, displacement):
|
||||
return bytes([0x64, 0x48 | (register >> 3) << 2, opcode, 0x04 | (register & 7) << 3, 0x25]) + struct.pack("<i", displacement)
|
||||
|
||||
|
||||
def alu_check_body(opcode, register, base, value, result, flags_masked):
|
||||
code = bytearray()
|
||||
jumps = []
|
||||
|
||||
def emit(data):
|
||||
code.extend(data)
|
||||
|
||||
def move_immediate(target, immediate):
|
||||
emit(bytes([0x48 | (target >> 3), 0xb8 | (target & 7)]) + struct.pack("<Q", immediate & ((1 << 64) - 1)))
|
||||
|
||||
def compare_with_r8(target):
|
||||
emit(bytes([0x48 | 0x44 | (0x01 if target >= 8 else 0), 0x39, 0xc0 | (target & 7)]))
|
||||
|
||||
def fail_unless_equal():
|
||||
emit(bytes.fromhex("0f 85 00 00 00 00"))
|
||||
jumps.append(len(code) - 4)
|
||||
|
||||
emit(bytes.fromhex("64 c7 04 25 28 00 00 00") + struct.pack("<I", value & 0xffffffff))
|
||||
for target in (0, 1, 3):
|
||||
move_immediate(target, base)
|
||||
emit(fs_alu(opcode, register, 0x28))
|
||||
emit(bytes.fromhex("9c 5a 83 e2 41"))
|
||||
move_immediate(8, result)
|
||||
compare_with_r8(register)
|
||||
fail_unless_equal()
|
||||
for target in (0, 1, 3):
|
||||
if target == register:
|
||||
continue
|
||||
move_immediate(8, base)
|
||||
compare_with_r8(target)
|
||||
fail_unless_equal()
|
||||
emit(bytes.fromhex("83 fa") + bytes([flags_masked]))
|
||||
fail_unless_equal()
|
||||
emit(bytes.fromhex("b8 2a 00 00 00 c3 b8 01 00 00 00 c3"))
|
||||
failure = len(code) - 6
|
||||
for position in jumps:
|
||||
struct.pack_into("<i", code, position, failure - position - 4)
|
||||
return bytes(code)
|
||||
|
||||
|
||||
def alu_execution_cases():
|
||||
mask = (1 << 64) - 1
|
||||
cases = (
|
||||
("xor", 0x33, lambda a, b: a ^ b, 0xffffffff00000000, 0xffffffff),
|
||||
("and", 0x23, lambda a, b: a & b, 0xffffffff00000000, 0xffffffff),
|
||||
("or", 0x0b, lambda a, b: a | b, 0xffffffff00000000, 0xffffffff),
|
||||
("add", 0x03, lambda a, b: a + b, 0x0102030405060708, 0x11223344),
|
||||
("sub", 0x2b, lambda a, b: a - b, 0x0000000012345678, 0xffffffff),
|
||||
("cmp", 0x3b, lambda a, b: a, 0x0000000012345678, 0xffffffff),
|
||||
("cmp-equal", 0x3b, lambda a, b: a, 0x00000000ffffffff, 0xffffffff),
|
||||
)
|
||||
for name, opcode, operation, base, value in cases:
|
||||
result = operation(base, value) & mask
|
||||
if opcode in (0x2b, 0x3b):
|
||||
carry = 1 if base < value else 0
|
||||
elif opcode == 0x03:
|
||||
carry = 1 if base + value > mask else 0
|
||||
else:
|
||||
carry = 0
|
||||
flags_masked = ((1 if result == 0 else 0) << 6) | carry
|
||||
for register in (0, 1, 3):
|
||||
body = alu_check_body(opcode, register, base, value, result, flags_masked)
|
||||
yield f"alu-exec-{name}-{register}", make_image("register", "unwind", body=body)
|
||||
|
||||
|
||||
def displacement_load(register, displacement, flags, round_trip=False):
|
||||
saved = (3, 5, 6, 7, 12, 13, 14, 15)
|
||||
code = bytearray()
|
||||
@@ -284,7 +349,7 @@ def main():
|
||||
for name, image, error in displacement_bounds_cases():
|
||||
convert(name, image, error, error_offset=0x1240)
|
||||
for name, opcode in ALU_READ.items():
|
||||
for register in (0, 3, 8, 13):
|
||||
for register in (0, 1, 2, 3, 8, 13):
|
||||
for displacement in (0, 40, -8):
|
||||
body = fs_alu(opcode, register, displacement) + b"\xc3"
|
||||
case = f"alu-{name}-{register}-{displacement}"
|
||||
@@ -301,10 +366,15 @@ def main():
|
||||
assert patched[0] == 0xe9, case
|
||||
stub_address = patched_address + 5 + struct.unpack_from("<i", patched, 1)[0]
|
||||
stub = pe_bytes_at(pe, stub_address, 96)
|
||||
value_load = bytes.fromhex("48 8b 80" if register else "48 8b 90") + struct.pack("<i", displacement)
|
||||
operation = bytes([0x48 | (register >> 3) << 2, opcode, 0xc0 | (register & 7) << 3]) if register else bytes([0x48, opcode, 0xc2])
|
||||
assert value_load in stub, (case, stub.hex())
|
||||
assert operation in stub, (case, stub.hex())
|
||||
if register == 0:
|
||||
expected = bytes.fromhex("48 8b 90") + struct.pack("<i", displacement) + bytes([0x58, 0x48, opcode, 0xc2])
|
||||
elif register == 1:
|
||||
expected = bytes.fromhex("48 8b 80") + struct.pack("<i", displacement) + bytes.fromhex("48 8b 4c 24 08") + bytes([0x48, opcode, 0xc8])
|
||||
else:
|
||||
expected = bytes.fromhex("48 8b 80") + struct.pack("<i", displacement) + bytes([0x48 | ((register >> 3) << 2), opcode, 0xc0 | ((register & 7) << 3)])
|
||||
assert expected in stub, (case, expected.hex(), stub.hex())
|
||||
for name, image in alu_execution_cases():
|
||||
convert(name, image)
|
||||
rejected = {
|
||||
"rsp-displacement": fs_load(4, 40),
|
||||
"rsp-alu": fs_alu(0x33, 4, 40),
|
||||
|
||||
Reference in New Issue
Block a user