diff options
| author | Tauwni <102470988+Tauwni@users.noreply.github.com> | 2026-11-26 12:19:12 +0100 |
|---|---|---|
| committer | Tauwni <102470988+Tauwni@users.noreply.github.com> | 2026-11-26 12:19:12 +0100 |
| commit | beffaa9a476ca1b3dfd9cfa55e39eafbb1f5eec6 (patch) | |
| tree | 32877805bf4eb46d9a89d5753db6b5a2b06dc845 /runtime/test/int3_skip_test.cpp | |
| parent | f29ca7bacae9507f83e8c56abdb604bd4efb4cf1 (diff) | |
| parent | a73a57e1339be072baf5522351c8ec7f1482b56b (diff) | |
Merge main into fix/null-descriptor-generated-dir
Diffstat (limited to 'runtime/test/int3_skip_test.cpp')
| -rw-r--r-- | runtime/test/int3_skip_test.cpp | 210 |
1 files changed, 193 insertions, 17 deletions
diff --git a/runtime/test/int3_skip_test.cpp b/runtime/test/int3_skip_test.cpp index 3c06302eb..9fa05386e 100644 --- a/runtime/test/int3_skip_test.cpp +++ b/runtime/test/int3_skip_test.cpp | |||
| @@ -1,9 +1,21 @@ | |||
| 1 | // LEONIDA_SKIP_INT3 (shims/startup_trace.h): with the variable set and a trace open, an int3 is logged once per | 1 | // LEONIDA_SKIP_INT3 (shims/startup_trace.h): with the variable set and a trace open, an int3 is logged once per |
| 2 | // address and stepped over, so execution continues. A separate executable because the variable is read once. | 2 | // address and stepped over, so execution continues. |
| 3 | // | ||
| 4 | // Each case runs in a child process of its own (`int3_skip_test --case NAME`), so a case that crashes fails alone and | ||
| 5 | // each one opens the trace with its own environment: | ||
| 6 | // real a real int3 at two sites, one of them hit twice: execution continues, one event per address | ||
| 7 | // windows a synthetic breakpoint with Rip on the 0xCC (Windows): Rip ends one past it, one event | ||
| 8 | // wine Rip one byte past the 0xCC (Wine, old upstream 7599): the same result | ||
| 9 | // null null EXCEPTION_POINTERS, ExceptionRecord or ContextRecord: no skip and no crash | ||
| 10 | // init the variable is read when the trace opens: removed after open(), int3s are still skipped | ||
| 11 | // off without the variable, nothing is skipped | ||
| 3 | #include <windows.h> | 12 | #include <windows.h> |
| 4 | 13 | ||
| 5 | #include <cstdio> | 14 | #include <cstdio> |
| 15 | #include <cstring> | ||
| 16 | #include <filesystem> | ||
| 6 | #include <fstream> | 17 | #include <fstream> |
| 18 | #include <memory> | ||
| 7 | #include <sstream> | 19 | #include <sstream> |
| 8 | #include <string> | 20 | #include <string> |
| 9 | 21 | ||
| @@ -35,31 +47,195 @@ __declspec(noinline) void site_b(int& reached) { | |||
| 35 | ++reached; | 47 | ++reached; |
| 36 | } | 48 | } |
| 37 | 49 | ||
| 38 | } // namespace | 50 | // A trace file of this process; the text is read back after close(). |
| 51 | struct Trace { | ||
| 52 | std::wstring path; | ||
| 53 | Trace() { | ||
| 54 | wchar_t temp[MAX_PATH]; | ||
| 55 | GetTempPathW(MAX_PATH, temp); | ||
| 56 | path = std::wstring(temp) + L"int3_skip_test_" + std::to_wstring(GetCurrentProcessId()) + L".jsonl"; | ||
| 57 | DeleteFileW(path.c_str()); | ||
| 58 | check(rt::startup_trace::open(path.c_str()), "open the startup trace file"); | ||
| 59 | } | ||
| 60 | std::string close_and_read() { | ||
| 61 | rt::startup_trace::close(); | ||
| 62 | std::string log; | ||
| 63 | { | ||
| 64 | std::ifstream in{std::filesystem::path(path)}; | ||
| 65 | std::stringstream text; | ||
| 66 | text << in.rdbuf(); | ||
| 67 | log = text.str(); | ||
| 68 | } | ||
| 69 | DeleteFileW(path.c_str()); | ||
| 70 | return log; | ||
| 71 | } | ||
| 72 | }; | ||
| 73 | |||
| 74 | // A synthetic EXCEPTION_BREAKPOINT for skip_int3, as the vectored handler receives it. | ||
| 75 | struct Breakpoint { | ||
| 76 | EXCEPTION_RECORD record{}; | ||
| 77 | std::unique_ptr<CONTEXT> context = std::make_unique<CONTEXT>(); // heap: a 16-byte-aligned member pads the struct | ||
| 78 | EXCEPTION_POINTERS pointers{}; | ||
| 79 | Breakpoint(const unsigned char* address, const unsigned char* rip) { | ||
| 80 | record.ExceptionCode = EXCEPTION_BREAKPOINT; | ||
| 81 | record.ExceptionAddress = const_cast<unsigned char*>(address); | ||
| 82 | record.NumberParameters = 1; | ||
| 83 | context->ContextFlags = CONTEXT_CONTROL; | ||
| 84 | context->Rip = reinterpret_cast<DWORD64>(rip); | ||
| 85 | pointers.ExceptionRecord = &record; | ||
| 86 | pointers.ContextRecord = context.get(); | ||
| 87 | } | ||
| 88 | bool skip() { return rt::startup_trace::detail::skip_int3(&pointers); } | ||
| 89 | bool rip_is(const unsigned char* p) const { return context->Rip == reinterpret_cast<DWORD64>(p); } | ||
| 90 | }; | ||
| 91 | |||
| 92 | // nop, int3, nop: the int3 at code + 1, with a byte on each side that is not one. | ||
| 93 | alignas(16) unsigned char g_code[4] = {0x90, 0xCC, 0x90, 0x90}; | ||
| 39 | 94 | ||
| 40 | int main() { | 95 | std::string event_at(const unsigned char* at) { |
| 41 | SetEnvironmentVariableW(L"LEONIDA_SKIP_INT3", L"1"); // before the first exception: the flag is read once | 96 | const std::string where = rt::startup_trace::where(reinterpret_cast<uint64_t>(at)); |
| 42 | wchar_t temp[MAX_PATH]; | 97 | return "\"kind\":\"int3_skipped\",\"at\":" + rt::startup_trace::quote(where); |
| 43 | GetTempPathW(MAX_PATH, temp); | 98 | } |
| 44 | const std::wstring path = std::wstring(temp) + L"int3_skip_test_" + std::to_wstring(GetCurrentProcessId()) + L".jsonl"; | ||
| 45 | DeleteFileW(path.c_str()); | ||
| 46 | check(rt::startup_trace::open(path.c_str()), "open the startup trace file"); | ||
| 47 | 99 | ||
| 100 | void case_real() { | ||
| 101 | SetEnvironmentVariableW(L"LEONIDA_SKIP_INT3", L"1"); | ||
| 102 | Trace trace; | ||
| 48 | int reached = 0; | 103 | int reached = 0; |
| 49 | for (int i = 0; i < 2; ++i) site_a(reached); // the same site twice: one event | 104 | for (int i = 0; i < 2; ++i) site_a(reached); // the same site twice: one event |
| 50 | site_b(reached); // another site | 105 | site_b(reached); // another site |
| 51 | check(reached == 3, "execution continues past every int3"); | 106 | check(reached == 3, "execution continues past every int3"); |
| 52 | rt::startup_trace::close(); | 107 | const std::string log = trace.close_and_read(); |
| 53 | |||
| 54 | std::ifstream in(path); | ||
| 55 | std::stringstream text; | ||
| 56 | text << in.rdbuf(); | ||
| 57 | const std::string log = text.str(); | ||
| 58 | in.close(); | ||
| 59 | DeleteFileW(path.c_str()); | ||
| 60 | check(count(log, "\"kind\":\"int3_skipped\"") == 2, "exactly two int3_skipped events (one per address)"); | 108 | check(count(log, "\"kind\":\"int3_skipped\"") == 2, "exactly two int3_skipped events (one per address)"); |
| 61 | check(count(log, "\"kind\":\"exception\"") == 0, "a skipped int3 is not also logged as an exception"); | 109 | check(count(log, "\"kind\":\"exception\"") == 0, "a skipped int3 is not also logged as an exception"); |
| 62 | check(log.find("\"at\":\"host:") != std::string::npos, "the event names the address"); | 110 | check(log.find("\"at\":\"host:") != std::string::npos, "the event names the address"); |
| 111 | } | ||
| 112 | |||
| 113 | void case_windows() { | ||
| 114 | SetEnvironmentVariableW(L"LEONIDA_SKIP_INT3", L"1"); | ||
| 115 | Trace trace; | ||
| 116 | const unsigned char* at = g_code + 1; | ||
| 117 | Breakpoint first(at, at); | ||
| 118 | check(first.skip(), "Rip on the 0xCC: skipped"); | ||
| 119 | check(first.rip_is(at + 1), "Rip on the 0xCC: Rip ends one byte past it"); | ||
| 120 | Breakpoint again(at, at); | ||
| 121 | check(again.skip() && again.rip_is(at + 1), "the same site again: skipped the same way"); | ||
| 122 | Breakpoint no_int3(g_code + 3, g_code + 3); | ||
| 123 | check(!no_int3.skip() && no_int3.rip_is(g_code + 3), "no 0xCC at or before Rip: not skipped, Rip unchanged"); | ||
| 124 | Breakpoint elsewhere(g_code, at); | ||
| 125 | check(!elsewhere.skip() && elsewhere.rip_is(at), "ExceptionAddress not at the int3 or past it: not skipped"); | ||
| 126 | const std::string log = trace.close_and_read(); | ||
| 127 | check(count(log, "\"kind\":\"int3_skipped\"") == 1, "one int3_skipped event for the site"); | ||
| 128 | check(log.find(event_at(at)) != std::string::npos, "the event names the 0xCC byte"); | ||
| 129 | } | ||
| 130 | |||
| 131 | void case_wine() { | ||
| 132 | SetEnvironmentVariableW(L"LEONIDA_SKIP_INT3", L"1"); | ||
| 133 | Trace trace; | ||
| 134 | const unsigned char* at = g_code + 1; | ||
| 135 | Breakpoint past(at + 1, at + 1); // ExceptionAddress and Rip one byte past the 0xCC | ||
| 136 | check(past.skip(), "Rip one byte past the 0xCC: skipped"); | ||
| 137 | check(past.rip_is(at + 1), "Rip one byte past the 0xCC: Rip ends one byte past it"); | ||
| 138 | Breakpoint mixed(at, at + 1); // ExceptionAddress on the 0xCC, Rip past it | ||
| 139 | check(mixed.skip() && mixed.rip_is(at + 1), "ExceptionAddress on the 0xCC, Rip past it: skipped"); | ||
| 140 | const std::string log = trace.close_and_read(); | ||
| 141 | check(count(log, "\"kind\":\"int3_skipped\"") == 1, "one int3_skipped event for the site"); | ||
| 142 | check(log.find(event_at(at)) != std::string::npos, "the event names the 0xCC byte, not the byte after it"); | ||
| 143 | } | ||
| 144 | |||
| 145 | // nop, int3, int3, nop: a run of int3 padding. | ||
| 146 | alignas(16) unsigned char g_padding[4] = {0x90, 0xCC, 0xCC, 0x90}; | ||
| 147 | |||
| 148 | void case_padding() { | ||
| 149 | SetEnvironmentVariableW(L"LEONIDA_SKIP_INT3", L"1"); | ||
| 150 | Trace trace; | ||
| 151 | const unsigned char* at = g_padding + 1; | ||
| 152 | Breakpoint mixed(at, at + 1); // ExceptionAddress on the first 0xCC, Rip on the second | ||
| 153 | check(mixed.skip() && mixed.rip_is(at + 1), | ||
| 154 | "ExceptionAddress on an int3 followed by another, Rip past it: the int3 ExceptionAddress names is skipped"); | ||
| 155 | const std::string log = trace.close_and_read(); | ||
| 156 | check(count(log, "\"kind\":\"int3_skipped\"") == 1, "one int3_skipped event"); | ||
| 157 | check(log.find(event_at(at)) != std::string::npos, "the event names the int3 ExceptionAddress names"); | ||
| 158 | } | ||
| 159 | |||
| 160 | void case_null() { | ||
| 161 | SetEnvironmentVariableW(L"LEONIDA_SKIP_INT3", L"1"); | ||
| 162 | Trace trace; | ||
| 163 | check(!rt::startup_trace::detail::skip_int3(nullptr), "null EXCEPTION_POINTERS: not skipped"); | ||
| 164 | Breakpoint no_record(g_code + 1, g_code + 1); | ||
| 165 | no_record.pointers.ExceptionRecord = nullptr; | ||
| 166 | check(!no_record.skip() && no_record.rip_is(g_code + 1), "null ExceptionRecord: not skipped"); | ||
| 167 | Breakpoint no_context(g_code + 1, g_code + 1); | ||
| 168 | no_context.pointers.ContextRecord = nullptr; | ||
| 169 | check(!no_context.skip(), "null ContextRecord: not skipped"); | ||
| 170 | const std::string log = trace.close_and_read(); | ||
| 171 | check(count(log, "\"kind\":\"int3_skipped\"") == 0, "no int3_skipped event"); | ||
| 172 | } | ||
| 173 | |||
| 174 | void case_init() { | ||
| 175 | SetEnvironmentVariableW(L"LEONIDA_SKIP_INT3", L"1"); | ||
| 176 | Trace trace; | ||
| 177 | SetEnvironmentVariableW(L"LEONIDA_SKIP_INT3", nullptr); // read when the trace opened, not at the first int3 | ||
| 178 | check(rt::startup_trace::detail::skip_int3_on.load(), "the flag holds the value read when the trace opened"); | ||
| 179 | Breakpoint bp(g_code + 1, g_code + 1); | ||
| 180 | check(bp.skip() && bp.rip_is(g_code + 2), "an int3 after the variable is removed is still skipped"); | ||
| 181 | trace.close_and_read(); | ||
| 182 | } | ||
| 183 | |||
| 184 | void case_off() { | ||
| 185 | SetEnvironmentVariableW(L"LEONIDA_SKIP_INT3", nullptr); | ||
| 186 | Trace trace; | ||
| 187 | check(!rt::startup_trace::detail::skip_int3_on.load(), "without the variable the flag is off"); | ||
| 188 | Breakpoint bp(g_code + 1, g_code + 1); | ||
| 189 | check(!bp.skip() && bp.rip_is(g_code + 1), "without the variable an int3 is not skipped"); | ||
| 190 | trace.close_and_read(); | ||
| 191 | } | ||
| 192 | |||
| 193 | struct Case { | ||
| 194 | const char* name; | ||
| 195 | void (*run)(); | ||
| 196 | }; | ||
| 197 | constexpr Case kCases[] = {{"real", case_real}, {"windows", case_windows}, {"wine", case_wine}, | ||
| 198 | {"padding", case_padding}, {"null", case_null}, {"init", case_init}, | ||
| 199 | {"off", case_off}}; | ||
| 200 | |||
| 201 | // Runs `this-exe --case NAME` and returns its exit code. | ||
| 202 | DWORD run_child(const char* name) { | ||
| 203 | wchar_t exe[MAX_PATH]; | ||
| 204 | GetModuleFileNameW(nullptr, exe, MAX_PATH); | ||
| 205 | std::wstring command = L"\"" + std::wstring(exe) + L"\" --case "; | ||
| 206 | for (const char* c = name; *c; ++c) command += static_cast<wchar_t>(*c); | ||
| 207 | STARTUPINFOW si{}; | ||
| 208 | si.cb = sizeof si; | ||
| 209 | PROCESS_INFORMATION pi{}; | ||
| 210 | if (!CreateProcessW(exe, command.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi)) return 0xFFFFFFFF; | ||
| 211 | WaitForSingleObject(pi.hProcess, INFINITE); | ||
| 212 | DWORD code = 0xFFFFFFFF; | ||
| 213 | GetExitCodeProcess(pi.hProcess, &code); | ||
| 214 | CloseHandle(pi.hThread); | ||
| 215 | CloseHandle(pi.hProcess); | ||
| 216 | return code; | ||
| 217 | } | ||
| 218 | |||
| 219 | } // namespace | ||
| 220 | |||
| 221 | int main(int argc, char** argv) { | ||
| 222 | if (argc == 3 && std::strcmp(argv[1], "--case") == 0) { | ||
| 223 | for (const Case& c : kCases) | ||
| 224 | if (std::strcmp(argv[2], c.name) == 0) { | ||
| 225 | c.run(); | ||
| 226 | std::fflush(stdout); | ||
| 227 | return g_failed ? 1 : 0; | ||
| 228 | } | ||
| 229 | std::printf("unknown case %s\n", argv[2]); | ||
| 230 | return 2; | ||
| 231 | } | ||
| 232 | for (const Case& c : kCases) { | ||
| 233 | std::printf("-- %s\n", c.name); | ||
| 234 | std::fflush(stdout); | ||
| 235 | const DWORD code = run_child(c.name); | ||
| 236 | if (code != 0) std::printf("FAIL case %s exited with 0x%08lx\n", c.name, static_cast<unsigned long>(code)); | ||
| 237 | if (code != 0) ++g_failed; | ||
| 238 | } | ||
| 63 | std::printf("%s\n", g_failed ? "FAILED" : "all checks passed"); | 239 | std::printf("%s\n", g_failed ? "FAILED" : "all checks passed"); |
| 64 | return g_failed ? 1 : 0; | 240 | return g_failed ? 1 : 0; |
| 65 | } | 241 | } |