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:
authorDominik <55330408+Dom-in@users.noreply.github.com>2026-11-22 15:22:13 +0000
committerDominik <55330408+Dom-in@users.noreply.github.com>2026-11-22 16:43:32 +0000
commit83d973a424afa3a5c21a52683154b770e4702065 (patch)
tree2c4142b2485b169b46063c5a0562cc705110e830 /runtime/test/loader_test.cpp
parent49475796c226c8511b9470bf97a4daf57b8af323 (diff)
pe_loader: read the TLS directory through directory(), honouring NumberOfRvaAndSizes
Every data directory the loader reads goes through directory(), which returns an empty entry past OptionalHeader.NumberOfRvaAndSizes (PE/COFF: those slots are not directories, whatever bytes they hold) - except the TLS one, which read DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] directly. An image declaring fewer than 10 directories with leftover bytes in slot 9 then failed to load ("the TLS directory runs past the image") or, with plausible bytes, had a TLS directory the Windows loader would ignore. loader_test: with NumberOfRvaAndSizes = 9 and junk in the TLS slot the image loads (it was rejected before); with all 16 the same slot is still checked and refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index 194174b00..3a4ced292 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -941,6 +941,19 @@ std::vector<uint8_t> make_unwind_image(const std::vector<RUNTIME_FUNCTION>& entr
941 return img; 941 return img;
942} 942}
943 943
944// PE/COFF: NumberOfRvaAndSizes counts the data directories; the slots past it are not directories whatever they hold.
945// With 9 directories the TLS slot (index 9) is not one, so bytes left in it must not make the image fail to load.
946void test_tls_directory_count() {
947 auto img = make_image();
948 auto& tls = nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
949 tls.VirtualAddress = 0xFFFF0000; // would run past the image if it were read as a TLS directory
950 tls.Size = sizeof(IMAGE_TLS_DIRECTORY64);
951 nt_of(img)->OptionalHeader.NumberOfRvaAndSizes = IMAGE_DIRECTORY_ENTRY_TLS;
952 expect_loads("a TLS slot past NumberOfRvaAndSizes is not read as a TLS directory", img);
953 nt_of(img)->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
954 expect_rejects("the same TLS slot within NumberOfRvaAndSizes is checked and refused", img);
955}
956
944void test_unwind_registration() { 957void test_unwind_registration() {
945 const std::vector<RUNTIME_FUNCTION> entries = {{0x1000, 0x1010, kUnwindRva}, {0x1010, 0x1020, kUnwindRva}}; 958 const std::vector<RUNTIME_FUNCTION> entries = {{0x1000, 0x1010, kUnwindRva}, {0x1010, 0x1020, kUnwindRva}};
946 uintptr_t pc = 0; 959 uintptr_t pc = 0;
@@ -1109,6 +1122,7 @@ int main() {
1109 test_delay_descriptor_fields(); 1122 test_delay_descriptor_fields();
1110 test_forwarded_exports(); 1123 test_forwarded_exports();
1111 test_export_bounds(); 1124 test_export_bounds();
1125 test_tls_directory_count();
1112 test_unwind_registration(); 1126 test_unwind_registration();
1113 } catch (const std::exception& e) { 1127 } catch (const std::exception& e) {
1114 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. 1128 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process.