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:
authorVictor <vgpastor08@gmail.com>2026-11-22 20:48:35 +0100
committerVictor <vgpastor08@gmail.com>2026-11-22 20:48:35 +0100
commitc9e08f9f1a509d4cabcdf36fa5e225d3eaefead1 (patch)
treef8694852362348432a39ce075061c5784d27f0b5 /runtime/test/loader_test.cpp
parent162821017ea3c286c8bec860f3ff94f037912061 (diff)
fix(loader): leave empty named exports unresolved
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index 194174b00..a0d0eaa36 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -359,6 +359,56 @@ void test_exports() {
359 } 359 }
360} 360}
361 361
362void test_empty_named_export() {
363 const char* what = "an empty named export stays unresolved without hiding valid exports";
364 auto img = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000);
365 auto* ed = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(img.data() + 0x200);
366 ed->NumberOfFunctions = 3;
367 ed->NumberOfNames = 3;
368 nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = 0x1A0;
369 auto* funcs = reinterpret_cast<uint32_t*>(img.data() + 0x300);
370 funcs[1] = 0;
371 funcs[2] = 0x11C0; // a code export outside the export directory
372 auto* names = reinterpret_cast<uint32_t*>(img.data() + 0x320);
373 names[0] = 0x1188;
374 names[1] = 0x1180;
375 names[2] = 0x1190;
376 auto* ords = reinterpret_cast<uint16_t*>(img.data() + 0x340);
377 ords[0] = 1;
378 ords[1] = 0;
379 ords[2] = 2;
380 std::memcpy(img.data() + 0x388, "empty\0", 6);
381 std::memcpy(img.data() + 0x390, "valid\0", 6);
382 auto provider = write_image_as(img, "empty_exports.dll");
383 try {
384 rt::Loader loader(g_dir);
385 rt::Module& m = loader.load(provider);
386 for (const char* sym : {"empty", "#2"}) {
387 bool owns = false;
388 if (m.exports.count(sym) || m.forwarders.count(sym) ||
389 loader.resolve_module_export(m.base, sym, owns) || !owns)
390 return failed(what, std::string(sym) + " did not remain absent from the mapped module");
391 }
392 for (const char* sym : {"valid", "#3", "fwd", "#1"}) {
393 bool owns = false;
394 void* expected = (std::strcmp(sym, "valid") == 0 || std::strcmp(sym, "#3") == 0)
395 ? m.base + 0x11C0 : host_symbol("kernel32.dll", "GetCurrentProcessId");
396 if (!expected || loader.resolve_module_export(m.base, sym, owns) != expected || !owns)
397 return failed(what, std::string(sym) + " no longer resolves to its valid target");
398 }
399 auto importer = write_image_as(make_import_image("empty_exports.dll", "empty"), "empty_import.exe");
400 rt::Module& importing = loader.load(importer);
401 if (importing.imports.size() != 1 || importing.imports[0].binding != rt::Binding::Unimplemented)
402 return failed(what, "an import of the empty slot did not bind as unimplemented");
403 void* bound = *reinterpret_cast<void**>(importing.base + 0x1180);
404 if (!bound || bound == m.base)
405 return failed(what, "the empty import was not given a failure thunk");
406 pass(what);
407 } catch (const std::exception& e) {
408 failed(what, e.what());
409 }
410}
411
362void test_imports() { 412void test_imports() {
363 { 413 {
364 auto img = make_image(); 414 auto img = make_image();
@@ -1098,6 +1148,7 @@ int main() {
1098 test_truncation(); 1148 test_truncation();
1099 test_relocations(); 1149 test_relocations();
1100 test_exports(); 1150 test_exports();
1151 test_empty_named_export();
1101 test_imports(); 1152 test_imports();
1102 test_delay_imports(); 1153 test_delay_imports();
1103 test_initialise(); 1154 test_initialise();