upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/startup_trace_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/startup_trace_test.cpp')
-rw-r--r--runtime/test/startup_trace_test.cpp100
1 files changed, 99 insertions, 1 deletions
diff --git a/runtime/test/startup_trace_test.cpp b/runtime/test/startup_trace_test.cpp
index 170ea39d5..2b8671099 100644
--- a/runtime/test/startup_trace_test.cpp
+++ b/runtime/test/startup_trace_test.cpp
@@ -7,7 +7,8 @@
7// 0x1220 reads address 0 (an access violation the test catches) 7// 0x1220 reads address 0 (an access violation the test catches)
8// The loader maps it with the trace open, the test calls the three functions, closes the trace and checks the lines: 8// The loader maps it with the trace open, the test calls the three functions, closes the trace and checks the lines:
9// map, unimplemented_call with the caller as module+RVA, debug_string with the text, exception with the faulting 9// map, unimplemented_call with the caller as module+RVA, debug_string with the text, exception with the faulting
10// RVA and access kind. No game data and no GPU are involved. 10// RVA and access kind. A second image with delay imports checks delay_import_unimplemented (make_delay_image).
11// No game data and no GPU are involved.
11// LEONIDA_TEST_DIR names the folder for the image and the trace, as for loader_test; the default is the temp folder. 12// LEONIDA_TEST_DIR names the folder for the image and the trace, as for loader_test; the default is the temp folder.
12#include <windows.h> 13#include <windows.h>
13 14
@@ -279,6 +280,102 @@ void test_trace(const fs::path& dir) {
279 check(find_line(lines, "exit") == &lines.back(), "exit comes last"); 280 check(find_line(lines, "exit") == &lines.back(), "exit comes last");
280} 281}
281 282
283// A DLL image with no code: one ordinary import that binds to nothing, and two delay-import descriptors, one naming a
284// function nothing serves and one naming kernel32!GetTickCount (bound to the host). Delay imports are bound when the
285// image is loaded, so the trace names the missing one then; the ordinary missing import and the bound delay import
286// give no such event.
287constexpr const char* kDelayImageName = "delay_image.bin";
288
289std::vector<uint8_t> make_delay_image() {
290 std::vector<uint8_t> b(0x600, 0);
291 auto* dos = reinterpret_cast<IMAGE_DOS_HEADER*>(b.data());
292 dos->e_magic = IMAGE_DOS_SIGNATURE;
293 dos->e_lfanew = 0x80;
294 auto* nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(b.data() + dos->e_lfanew);
295 nt->Signature = IMAGE_NT_SIGNATURE;
296 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
297 nt->FileHeader.NumberOfSections = 1;
298 nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER64);
299 nt->FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_LARGE_ADDRESS_AWARE;
300 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC;
301 nt->OptionalHeader.ImageBase = 0x1a0000000;
302 nt->OptionalHeader.SizeOfHeaders = 0x200;
303 nt->OptionalHeader.SizeOfImage = 0x2000;
304 nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
305 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
306 auto* sec = IMAGE_FIRST_SECTION(nt);
307 std::memcpy(sec->Name, ".data", 6);
308 sec->VirtualAddress = kTextRva;
309 sec->Misc.VirtualSize = 0x400;
310 sec->PointerToRawData = 0x200;
311 sec->SizeOfRawData = 0x400;
312 sec->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
313
314 uint8_t* data = b.data() + 0x200;
315 auto put_string = [&](uint32_t off, const char* s) { std::strcpy(reinterpret_cast<char*>(data + off), s); };
316 // One name table entry and its terminator at `lookup_off`, the import's hint/name at `hint_off`.
317 auto put_thunk = [&](uint32_t lookup_off, uint32_t hint_off, const char* name) {
318 *reinterpret_cast<uint64_t*>(data + lookup_off) = kTextRva + hint_off;
319 put_string(hint_off + 2, name);
320 };
321 put_string(0x100, "leonida_trace_probe.dll");
322 put_string(0x120, "kernel32.dll");
323
324 auto* d = reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(data);
325 d->Name = kTextRva + 0x100;
326 d->OriginalFirstThunk = kTextRva + 0x140;
327 d->FirstThunk = kTextRva + 0x160;
328 put_thunk(0x140, 0x180, "NormalProbeMissing");
329
330 const struct {
331 uint32_t dll_off, int_off, iat_off, hint_off;
332 const char* name;
333 } delayed[2] = {{0x100, 0x1A0, 0x1C0, 0x1E0, "DelayProbeMissing"}, {0x120, 0x200, 0x220, 0x240, "GetTickCount"}};
334 for (int i = 0; i < 2; ++i) {
335 auto* dd = reinterpret_cast<IMAGE_DELAYLOAD_DESCRIPTOR*>(data + 0x40) + i;
336 dd->Attributes.AllAttributes = 1; // dlattrRva
337 dd->DllNameRVA = kTextRva + delayed[i].dll_off;
338 dd->ImportNameTableRVA = kTextRva + delayed[i].int_off;
339 dd->ImportAddressTableRVA = kTextRva + delayed[i].iat_off;
340 put_thunk(delayed[i].int_off, delayed[i].hint_off, delayed[i].name);
341 }
342
343 auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
344 dir.VirtualAddress = kTextRva;
345 dir.Size = 2 * sizeof(IMAGE_IMPORT_DESCRIPTOR);
346 auto& ddir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT];
347 ddir.VirtualAddress = kTextRva + 0x40;
348 ddir.Size = 3 * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR);
349 return b;
350}
351
352void test_delay_import_trace(const fs::path& dir) {
353 const fs::path image = write_file(dir / kDelayImageName, make_delay_image());
354 const fs::path trace = dir / "delay_trace.jsonl";
355 check(rt::startup_trace::open(trace.c_str()), "open the delay-import trace file");
356 {
357 rt::Loader loader(dir);
358 rt::Module& m = loader.load(image);
359 int unimplemented = 0, host = 0;
360 for (const auto& r : m.imports) {
361 unimplemented += r.binding == rt::Binding::Unimplemented;
362 host += r.binding == rt::Binding::Host;
363 }
364 check(m.imports.size() == 3 && unimplemented == 2 && host == 1,
365 "the delay image binds 3 imports: 2 unimplemented, 1 host");
366 }
367 rt::startup_trace::close();
368
369 const auto lines = read_lines(trace);
370 int events = 0;
371 for (auto& l : lines) events += l.find("\"kind\":\"delay_import_unimplemented\"") != std::string::npos;
372 const std::string* e = find_line(lines, "delay_import_unimplemented");
373 check(events == 1, "one delay_import_unimplemented event (got " + std::to_string(events) + ")");
374 check(has(e, "\"module\":\"" + std::string(kDelayImageName) + "\"") &&
375 has(e, "\"import\":\"leonida_trace_probe.dll!DelayProbeMissing\""),
376 "delay_import_unimplemented names the module and the delay import");
377}
378
282} // namespace 379} // namespace
283 380
284int wmain() { 381int wmain() {
@@ -287,6 +384,7 @@ int wmain() {
287 test_disabled_is_silent(); 384 test_disabled_is_silent();
288 test_existing_file_is_kept(dir); 385 test_existing_file_is_kept(dir);
289 test_trace(dir); 386 test_trace(dir);
387 test_delay_import_trace(dir);
290 std::error_code ec; 388 std::error_code ec;
291 fs::remove_all(dir, ec); 389 fs::remove_all(dir, ec);
292 } catch (const std::exception& e) { 390 } catch (const std::exception& e) {