From 31e9e67f5407a13006d76d975a229edc030094fc Mon Sep 17 00:00:00 2001 From: "husari.orozco" Date: Mon, 5 Oct 2026 17:50:50 -0600 Subject: [PATCH] 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. --- .../src/windows/WindowsTlsBuilder.cpp | 16 +++- .../tests/test_tls_function_coverage.py | 80 +++++++++++++++++-- 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp index a38b93257..cec6aec7e 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp @@ -163,14 +163,26 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector& 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; diff --git a/core/relinker/relinker/tests/test_tls_function_coverage.py b/core/relinker/relinker/tests/test_tls_function_coverage.py index 4d68200c2..9e0037181 100644 --- a/core/relinker/relinker/tests/test_tls_function_coverage.py +++ b/core/relinker/relinker/tests/test_tls_function_coverage.py @@ -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("> 3), 0xb8 | (target & 7)]) + struct.pack("= 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(" 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("> 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("> 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),