upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/clzero_patch_test.cpp
blob: dc34de8f8fcedd593b10293cc7eb305c43eaa4d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// clzero_patch.h: the two CLZERO loop shapes the title uses are found, rewritten to jump to stubs, and the stubs
// zero exactly the lines the loops would zero and leave the registers the loops leave. The loops are hand-assembled
// here (no game code), and only the patched copies run, so the test runs on any x86-64 CPU.
#include <windows.h>

#include <cstdint>
#include <cstdio>
#include <cstring>
#include <vector>

#include "clzero_patch.h"

namespace {

int failures = 0;
int checks = 0;

void check(bool ok, const char* what) {
    ++checks;
    if (!ok) ++failures;
    std::printf("%s %s\n", ok ? "PASS" : "FAIL", what);
}

// uint64_t f(void* begin, void* end): shape A over [begin, end), returns rax at the loop's exit.
const uint8_t kShapeA[] = {
    0x48, 0x89, 0xC8,        // mov rax, rcx
    0x49, 0x89, 0xD2,        // mov r10, rdx
    0x0F, 0x01, 0xFC,        // L: clzero
    0x48, 0x83, 0xC0, 0x40,  // add rax, 40h
    0x4C, 0x39, 0xD0,        // cmp rax, r10
    0x72, 0xF4,              // jb L
    0xC3,                    // ret
};

// uint64_t f(void* begin, void* end): shape B over [begin, end), returns the counter at the loop's exit.
const uint8_t kShapeB[] = {
    0x49, 0x89, 0xCA,        // mov r10, rcx
    0x49, 0x89, 0xD3,        // mov r11, rdx
    0x4C, 0x89, 0xD0,        // L: mov rax, r10
    0x49, 0x83, 0xC2, 0x40,  // add r10, 40h
    0x0F, 0x01, 0xFC,        // clzero
    0x4D, 0x39, 0xDA,        // cmp r10, r11
    0x72, 0xF1,              // jb L
    0x4C, 0x89, 0xD0,        // mov rax, r10
    0xC3,                    // ret
};

// Shape A with cmp rax, rcx: rcx is the register the stub saves, so this loop is left alone.
const uint8_t kShapeAWithRcx[] = {
    0x0F, 0x01, 0xFC,        // clzero
    0x48, 0x83, 0xC0, 0x40,  // add rax, 40h
    0x48, 0x39, 0xC8,        // cmp rax, rcx
    0x72, 0xF4,              // jb
    0xC3,
};

using Loop = uint64_t (*)(void*, void*);

// Fills a buffer, runs `loop` over [buf + first, buf + last) and checks which bytes changed.
bool zeroes_exactly(Loop loop, size_t first, size_t last, uint64_t& returned) {
    alignas(64) static uint8_t buf[4096];
    std::memset(buf, 0xAB, sizeof buf);
    returned = loop(buf + first, buf + last);
    for (size_t i = 0; i < sizeof buf; ++i) {
        const bool inside = i >= (first & ~size_t(63)) && i < last;
        if (buf[i] != (inside ? 0 : 0xAB)) return false;
    }
    return returned == reinterpret_cast<uint64_t>(buf + last);
}

}  // namespace

int main() {
    namespace cz = rt::clzero_patch;
    {
        const auto a = cz::find(kShapeA, sizeof kShapeA);
        check(a.size() == 1 && a[0].head == 6 && a[0].length == 12 && a[0].clzero == 0, "shape A is found at its CLZERO");
        const auto b = cz::find(kShapeB, sizeof kShapeB);
        check(b.size() == 1 && b[0].head == 6 && b[0].length == 15 && b[0].clzero == 7,
              "shape B is found at its mov rax, CUR");
        check(cz::find(kShapeAWithRcx, sizeof kShapeAWithRcx).empty(), "a loop that compares against rcx is left alone");
        const uint8_t lone[] = {0x0F, 0x01, 0xFC, 0xC3};
        check(cz::find(lone, sizeof lone).empty(), "a CLZERO outside both loop shapes is left alone");
    }

    auto* code = static_cast<uint8_t*>(VirtualAlloc(nullptr, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE));
    check(code != nullptr, "code page");
    if (!code) return 1;
    std::memcpy(code, kShapeA, sizeof kShapeA);
    std::memcpy(code + 0x100, kShapeB, sizeof kShapeB);
    const auto r = cz::patch({{code, 0x1000}}, uint64_t(code), uint64_t(code) + 0x1000);
    check(r.sites == 2 && r.stubs != nullptr, "both loops are patched");
    check(code[6] == 0xE9 && code[0x106] == 0xE9, "each loop starts with a jmp rel32 to its stub");
    const int64_t distance = int64_t(r.stubs) - int64_t(code);
    check(distance > -(int64_t(1) << 31) && distance < (int64_t(1) << 31), "the stubs are within 2 GiB of the code");
    FlushInstructionCache(GetCurrentProcess(), code, 0x1000);

    const auto shape_a = reinterpret_cast<Loop>(code);
    const auto shape_b = reinterpret_cast<Loop>(code + 0x100);
    uint64_t ret = 0;
    check(zeroes_exactly(shape_a, 64, 64 + 1024, ret), "shape A zeroes its 16 lines and returns rax = end");
    check(zeroes_exactly(shape_a, 128, 192, ret), "shape A runs once for a one-line range");
    check(zeroes_exactly(shape_b, 256, 256 + 0x380, ret), "shape B zeroes its 14 lines and leaves CUR = end");
    {
        // An unaligned start: CLZERO zeroes the whole line rAX points into, and the loop steps by 64 from rAX.
        alignas(64) static uint8_t buf[512];
        std::memset(buf, 0xAB, sizeof buf);
        shape_a(buf + 64 + 8, buf + 64 + 8 + 128);
        bool ok = true;
        for (size_t i = 0; i < sizeof buf; ++i) ok &= buf[i] == ((i >= 64 && i < 64 + 128) ? 0 : 0xAB);
        check(ok, "an unaligned start zeroes the lines rAX points into");
    }

    std::printf("clzero_patch_test: %s (%d checks)\n", failures ? "FAILED" : "OK", checks);
    return failures ? 1 : 0;
}