upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/loader_test.cpp
diff options
context:
space:
mode:
authorNotRexed <therealnothingfn@gmail.com>2026-11-27 17:50:37 -0500
committerNotRexed <therealnothingfn@gmail.com>2026-11-27 17:50:37 -0500
commitb87abdef5735adc68a614b2025ffcf9869a47cc5 (patch)
tree0d8b473e8e753883a01e00153175994d6b7949c2 /runtime/test/loader_test.cpp
parent84e728faffb36cd96a08934e5632ba9b194801f3 (diff)
parent34e33d3ddc4dd95ad14a074db886df3d25588201 (diff)
Merge mandy's main into port/d3d12-nvidia
Brings the D3D12 runtime branch (d3d12/, its launcher scripts and notes, and the pull requests reviewed and applied on it) together with mandy's main. Conflicts (67 files, none under d3d12/): docs/, make_game.py, runtime/, scripts/build_runtime.ps1, tests/ and tools/. Every one takes main's side: main is where that code is maintained, and this branch's copies were older states of the same files or the same pull requests in another form. tests/test_fspx_unpack.py is main's too, as it tests main's tools/fspx_unpack.py. Nothing under d3d12/ is changed by the merge. Checked before pushing: python -m compileall over make_game.py, tools and tests; make_game.py --help; scripts/check_no_game_data.py over the files this adds to main; tests/test_text_write_line_endings.py. The translator suite, the C++ runtime build and CI have not run on the result. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index 3f68604c9..17cb2d304 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -654,6 +654,93 @@ void test_initialise_rejects() {
654 } 654 }
655} 655}
656 656
657// IMAGE_FILE_RELOCS_STRIPPED against a relocation directory, when the preferred base is taken. Measured on the
658// Windows 11 (26200) loader with synthetic DLLs (runtime/README.md): with no relocation directory the flag refuses
659// the move and an image with the flag clear is moved with nothing fixed up; with a relocation table the image is
660// moved and fixed up whatever the flag says. The DllMain stub stores through an absolute address in this process,
661// not in the image, so it runs correctly wherever the image lands.
662void test_moved_without_relocations() {
663 auto img = make_image();
664 auto* nt = nt_of(img);
665 nt->FileHeader.Characteristics |= IMAGE_FILE_DLL;
666 nt->OptionalHeader.ImageBase = leonida_test::free_image_base(0x1C0000000);
667 nt->OptionalHeader.AddressOfEntryPoint = 0x1040;
668 emit_store_stub(img, 0x240, reinterpret_cast<uint64_t>(&g_main_ran), 5, 1);
669 const uint64_t preferred = nt->OptionalHeader.ImageBase;
670 void* held = VirtualAlloc(reinterpret_cast<void*>(preferred), 0x2000, MEM_RESERVE, PAGE_NOACCESS);
671 if (!held) {
672 // Something else holds it already, which is the case under test as well; but make sure it is held.
673 MEMORY_BASIC_INFORMATION info{};
674 if (!VirtualQuery(reinterpret_cast<void*>(preferred), &info, sizeof info) || info.State == MEM_FREE)
675 return failed("an image without relocations moves when its base is taken", "cannot hold its base");
676 }
677 {
678 const char* what = "an image without relocations, flag clear, moves when its base is taken";
679 g_main_ran = 0;
680 auto p = write_image(img);
681 try {
682 rt::Loader loader(g_dir);
683 rt::Module& m = loader.load(p);
684 if (reinterpret_cast<uint64_t>(m.base) == preferred || !m.relocated)
685 failed(what, "it was not moved");
686 else if (std::memcmp(m.base + 0x1000, text_raw(img), 0x200) != 0)
687 failed(what, "the mapped section doesn't match the file (something was fixed up)");
688 else {
689 loader.initialise();
690 if (g_main_ran != 5) failed(what, "its DllMain never ran at the new address");
691 else pass(what);
692 }
693 } catch (const std::exception& e) {
694 failed(what, e.what());
695 }
696 }
697 {
698 auto stripped = img;
699 nt_of(stripped)->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
700 std::string error;
701 const char* what = "an image with IMAGE_FILE_RELOCS_STRIPPED is refused when its base is taken";
702 if (load(write_image(stripped), error) == 0) failed(what, "the image loaded");
703 else if (error.find("stripped") == std::string::npos) failed(what, "refused for another reason: " + error);
704 else pass(what);
705 }
706 if (held) VirtualFree(held, 0, MEM_RELEASE);
707 {
708 // At a free base such an image maps, as it did before. Not Windows parity: the Windows 11 loader refused these
709 // DLLs (error 487) at a free base too, for a reason not yet known; runtime/README.md keeps the question.
710 auto stripped = img;
711 nt_of(stripped)->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
712 expect_loads("an image with IMAGE_FILE_RELOCS_STRIPPED and no relocations maps at its free base", stripped);
713 }
714 {
715 // With a relocation table the flag does not stop the move: the table is applied, as Windows does it.
716 // ImageBase 0 makes the loader move it; the one DIR64 entry points at the qword at RVA 0x1100.
717 const char* what = "an image with IMAGE_FILE_RELOCS_STRIPPED and a relocation table is moved and fixed up";
718 auto stripped = make_image();
719 auto* snt = nt_of(stripped);
720 snt->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
721 snt->OptionalHeader.ImageBase = 0;
722 snt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] = {0x1000, 0x0A};
723 auto* block = reinterpret_cast<IMAGE_BASE_RELOCATION*>(stripped.data() + 0x200);
724 block->VirtualAddress = 0x1000;
725 block->SizeOfBlock = 0x0A;
726 reinterpret_cast<uint16_t*>(block + 1)[0] = (IMAGE_REL_BASED_DIR64 << 12) | 0x100;
727 uint64_t original;
728 std::memcpy(&original, stripped.data() + 0x300, sizeof original);
729 auto p = write_image(stripped);
730 try {
731 rt::Loader loader(g_dir);
732 rt::Module& m = loader.load(p);
733 uint64_t moved;
734 std::memcpy(&moved, m.base + 0x1100, sizeof moved);
735 if (!m.relocated) failed(what, "it was not moved");
736 else if (moved != original + reinterpret_cast<uint64_t>(m.base)) failed(what, "the pointer wasn't fixed up");
737 else pass(what);
738 } catch (const std::exception& e) {
739 failed(what, e.what());
740 }
741 }
742}
743
657// The images below jump to host observers so notifications remain observable after the mappings are freed. 744// The images below jump to host observers so notifications remain observable after the mappings are freed.
658// The FLS destructor itself starts in mapped code: leaving it registered would call unmapped memory at exit. 745// The FLS destructor itself starts in mapped code: leaving it registered would call unmapped memory at exit.
659void emit_jump_stub(std::vector<uint8_t>& img, size_t off, uint64_t target) { 746void emit_jump_stub(std::vector<uint8_t>& img, size_t off, uint64_t target) {
@@ -1408,13 +1495,33 @@ void test_compat_modules() {
1408 std::filesystem::path bad = g_dir / "compat_bad"; 1495 std::filesystem::path bad = g_dir / "compat_bad";
1409 std::filesystem::create_directories(bad); 1496 std::filesystem::create_directories(bad);
1410 write_image_as(std::vector<uint8_t>(64, 0x5A), "compat_bad/testcompat.dll"); 1497 write_image_as(std::vector<uint8_t>(64, 0x5A), "compat_bad/testcompat.dll");
1498 // The loader has to keep Windows from raising its "Bad Image" box for this file (the load would wait
1499 // on a click), and has to leave the thread's error mode as it found it. The mode here is one without
1500 // SEM_FAILCRITICALERRORS, so a loader that suppresses nothing shows the box, and one that does not
1501 // restore the mode is caught below.
1502 const DWORD mode_before = GetThreadErrorMode() & ~static_cast<DWORD>(SEM_FAILCRITICALERRORS);
1503 DWORD mode_at_entry = 0;
1504 SetThreadErrorMode(mode_before, &mode_at_entry);
1505 const ULONGLONG started = GetTickCount64();
1411 try { 1506 try {
1412 bind_with_compat(bad, "testcompat.dll", "Answer", bound); 1507 bind_with_compat(bad, "testcompat.dll", "Answer", bound);
1413 failed(what, "the load succeeded"); 1508 failed(what, "the load succeeded");
1414 } catch (const std::exception& e) { 1509 } catch (const std::exception& e) {
1415 if (std::string(e.what()).find("LEONIDA_COMPAT_DIR") == std::string::npos) failed(what, e.what()); 1510 if (std::string(e.what()).find("LEONIDA_COMPAT_DIR") == std::string::npos) failed(what, e.what());
1511 else if (std::string(e.what()).find("(error 0)") != std::string::npos)
1512 failed(what, "the message lost the Windows error code");
1416 else pass(what); 1513 else pass(what);
1417 } 1514 }
1515 const ULONGLONG waited_ms = GetTickCount64() - started;
1516 const DWORD mode_after = GetThreadErrorMode();
1517 SetThreadErrorMode(mode_at_entry, nullptr);
1518 if (mode_after != mode_before)
1519 failed("a compat file Windows cannot load: the thread error mode is put back", "the mode changed");
1520 else pass("a compat file Windows cannot load: the thread error mode is put back");
1521 // A refused load takes milliseconds; with the box up, the load returns only after someone clicks it.
1522 if (waited_ms > 5000)
1523 failed("a compat file Windows cannot load is refused without an error box", "the load took over 5 s");
1524 else pass("a compat file Windows cannot load is refused without an error box");
1418 } 1525 }
1419 } catch (const std::exception& e) { 1526 } catch (const std::exception& e) {
1420 failed("LEONIDA_COMPAT_DIR", e.what()); 1527 failed("LEONIDA_COMPAT_DIR", e.what());
@@ -1454,6 +1561,7 @@ int main() {
1454 test_section_bounds(); 1561 test_section_bounds();
1455 test_truncation(); 1562 test_truncation();
1456 test_relocations(); 1563 test_relocations();
1564 test_moved_without_relocations();
1457 test_exports(); 1565 test_exports();
1458 test_empty_named_export(); 1566 test_empty_named_export();
1459 test_imports(); 1567 test_imports();