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:
authorAyinaki <ayinakidev@gmail.com>2026-11-21 18:48:36 +0000
committerAyinaki <ayinakidev@gmail.com>2026-11-21 19:00:06 +0000
commit126062ae4a8831a564b9e3042684657c58781bef (patch)
tree1a276cc6d4aeeccd84841d275d150d58f76a4fd4 /runtime/test/loader_test.cpp
parent2a732e0881cd20e7c63ba5ced7489cc5e97bc8e1 (diff)
Runtime: initialise a loaded module's TLS and DllMain
Mapping an image never ran anything. TLS callbacks and DllMain were documented as the caller's, and nothing called them, so a module that needs either could not be brought up at all. Loader::initialise() now allocates the module's TLS slot, copies the template and SizeOfZeroFill into it, writes the slot index into the image, runs the TLS callbacks with DLL_PROCESS_ATTACH, then calls the entry point of a DLL as DllMain. Every address it follows is bounded like the rest of the loader: the directory, the template, the index, the callback table and each callback, with a 1 MiB ceiling on the block. Only DLLs get DllMain; an EXE's entry point has a different signature and stays the caller's to run, and --dry-run still runs nothing. The tests build an image whose TLS callback and DllMain are real machine-code stubs that store a sentinel, so they check the code actually executed rather than merely being parsed.
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 1cd92cad0..b6f6e4912 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -22,6 +22,10 @@ int g_failed = 0;
22unsigned g_files = 0; 22unsigned g_files = 0;
23std::filesystem::path g_dir; 23std::filesystem::path g_dir;
24 24
25// Sentinels that the synthesised TLS callback and DllMain stubs write, so a test can see execution happen.
26uint32_t g_tls_ran = 0;
27uint32_t g_main_ran = 0;
28
25void pass(const char* what) { 29void pass(const char* what) {
26 ++g_checks; 30 ++g_checks;
27 std::printf("ok %s\n", what); 31 std::printf("ok %s\n", what);
@@ -361,6 +365,108 @@ void test_delay_imports() {
361 } 365 }
362} 366}
363 367
368// movabs rax, addr ; mov dword [rax], value ; mov eax, eax_value ; ret
369void emit_store_stub(std::vector<uint8_t>& img, size_t off, uint64_t addr, uint32_t value, uint32_t eax_value) {
370 size_t n = off;
371 img[n++] = 0x48;
372 img[n++] = 0xB8;
373 for (int i = 0; i < 8; ++i) img[n++] = static_cast<uint8_t>(addr >> (8 * i));
374 img[n++] = 0xC7;
375 img[n++] = 0x00;
376 for (int i = 0; i < 4; ++i) img[n++] = static_cast<uint8_t>(value >> (8 * i));
377 img[n++] = 0xB8;
378 for (int i = 0; i < 4; ++i) img[n++] = static_cast<uint8_t>(eax_value >> (8 * i));
379 img[n++] = 0xC3;
380}
381
382constexpr uint64_t kInitBase = 0x140000000;
383
384// make_image() plus a TLS directory and an entry point, both aimed at real machine code: a stub that stores a
385// sentinel in this process, so the test can tell a callback ran rather than only that it was parsed.
386std::vector<uint8_t> make_init_image() {
387 auto img = make_image();
388 auto* nt = nt_of(img);
389 nt->FileHeader.Characteristics |= IMAGE_FILE_DLL;
390 nt->OptionalHeader.AddressOfEntryPoint = 0x1040;
391 auto va = [](uint32_t rva) { return kInitBase + rva; };
392 constexpr size_t text = 0x200; // file offset of RVA 0x1000
393 emit_store_stub(img, text + 0x00, reinterpret_cast<uint64_t>(&g_tls_ran), 1, 0);
394 emit_store_stub(img, text + 0x40, reinterpret_cast<uint64_t>(&g_main_ran), 2, 1);
395 std::memcpy(img.data() + text + 0x80, "TLS!", 4);
396 auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + text + 0x90);
397 callbacks[0] = va(0x1000);
398 callbacks[1] = 0;
399 auto* tls = reinterpret_cast<IMAGE_TLS_DIRECTORY64*>(img.data() + text + 0xB0);
400 tls->StartAddressOfRawData = va(0x1080);
401 tls->EndAddressOfRawData = va(0x1084);
402 tls->AddressOfIndex = va(0x10A0);
403 tls->AddressOfCallBacks = va(0x1090);
404 tls->SizeOfZeroFill = 8;
405 auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
406 dir.VirtualAddress = 0x10B0;
407 dir.Size = sizeof(IMAGE_TLS_DIRECTORY64);
408 return img;
409}
410
411void expect_init_rejects(const char* what, const std::vector<uint8_t>& bytes) {
412 auto p = write_image(bytes);
413 try {
414 rt::Loader loader(g_dir);
415 loader.load(p);
416 loader.initialise();
417 failed(what, "the module initialised");
418 } catch (const std::exception&) {
419 pass(what);
420 }
421}
422
423void test_initialise() {
424 g_tls_ran = 0;
425 g_main_ran = 0;
426 auto p = write_image(make_init_image());
427 try {
428 rt::Loader loader(g_dir);
429 rt::Module& m = loader.load(p);
430 loader.initialise();
431 if (g_tls_ran != 1)
432 failed("initialise runs the TLS callback", "the callback never wrote its sentinel");
433 else
434 pass("initialise runs the TLS callback");
435 if (g_main_ran != 2)
436 failed("initialise calls DllMain for a DLL", "the entry point never wrote its sentinel");
437 else
438 pass("initialise calls DllMain for a DLL");
439 uint32_t slot = *reinterpret_cast<const uint32_t*>(m.base + 0x10A0);
440 void* block = slot == TLS_OUT_OF_INDEXES ? nullptr : TlsGetValue(slot);
441 if (!block || std::memcmp(block, "TLS!", 4) != 0)
442 failed("the TLS template is copied into the allocated slot", "the slot doesn't hold the template");
443 else
444 pass("the TLS template is copied into the allocated slot");
445 } catch (const std::exception& e) {
446 failed("initialise runs a TLS callback and DllMain", e.what());
447 }
448}
449
450void test_initialise_rejects() {
451 {
452 auto img = make_init_image();
453 nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress = 0x1FF0;
454 expect_init_rejects("a TLS directory past the image is rejected", img);
455 }
456 {
457 auto img = make_init_image();
458 auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + 0x290); // file offset of RVA 0x1090
459 callbacks[0] = kInitBase + 0x9000; // well past SizeOfImage
460 expect_init_rejects("a TLS callback outside the image is rejected", img);
461 }
462 {
463 g_main_ran = 0;
464 auto img = make_init_image();
465 emit_store_stub(img, 0x240, reinterpret_cast<uint64_t>(&g_main_ran), 3, 0); // DllMain returns FALSE
466 expect_init_rejects("a DllMain that returns FALSE is rejected", img);
467 }
468}
469
364} // namespace 470} // namespace
365 471
366int main() { 472int main() {
@@ -381,6 +487,8 @@ int main() {
381 test_exports(); 487 test_exports();
382 test_imports(); 488 test_imports();
383 test_delay_imports(); 489 test_delay_imports();
490 test_initialise();
491 test_initialise_rejects();
384 492
385 std::filesystem::remove_all(g_dir, ec); 493 std::filesystem::remove_all(g_dir, ec);
386 std::printf("\n%d checks, %d failed\n", g_checks, g_failed); 494 std::printf("\n%d checks, %d failed\n", g_checks, g_failed);