diff options
| author | Kam471 <kam471@users.noreply.github.com> | 2026-11-21 22:19:03 +0000 |
|---|---|---|
| committer | Kam471 <kam471@users.noreply.github.com> | 2026-11-21 23:29:24 +0000 |
| commit | 993a2cbf72a9509ca730e509ea38e455d35f5a74 (patch) | |
| tree | a3d6dc8481bfeabb7e5a94840ef4474504add23d /runtime/test/loader_test.cpp | |
| parent | d7cbc3e7cb4d031c790dddd265a83fb6a1a1a7a5 (diff) | |
Runtime: install static TLS for mapped images
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index b756cc1b6..6165352f6 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | // What it pins down: a valid image maps exactly as its headers say, and a truncated or corrupt one is rejected with | 4 | // What it pins down: a valid image maps exactly as its headers say, and a truncated or corrupt one is rejected with |
| 5 | // a message instead of being followed out of bounds. The truncation case covers every length of a valid image. | 5 | // a message instead of being followed out of bounds. The truncation case covers every length of a valid image. |
| 6 | #include <windows.h> | 6 | #include <windows.h> |
| 7 | #include <intrin.h> | ||
| 7 | 8 | ||
| 8 | #include <algorithm> | 9 | #include <algorithm> |
| 9 | #include <cstdio> | 10 | #include <cstdio> |
| @@ -499,12 +500,21 @@ void test_initialise() { | |||
| 499 | failed("initialise calls DllMain for a DLL", "the entry point never wrote its sentinel"); | 500 | failed("initialise calls DllMain for a DLL", "the entry point never wrote its sentinel"); |
| 500 | else | 501 | else |
| 501 | pass("initialise calls DllMain for a DLL"); | 502 | pass("initialise calls DllMain for a DLL"); |
| 502 | uint32_t slot = *reinterpret_cast<const uint32_t*>(m.base + 0x10A0); | 503 | // __declspec(thread) reads TEB->ThreadLocalStoragePointer[_tls_index] at gs:[58h], which is a different index |
| 503 | void* block = slot == TLS_OUT_OF_INDEXES ? nullptr : TlsGetValue(slot); | 504 | // space from the TlsAlloc slots. The loader writes the OS-assigned module index (reserved through |
| 505 | // gta6rt_tls_host.dll) into AddressOfIndex and puts the template copy in that TEB slot, so this reads exactly | ||
| 506 | // what mapped __declspec(thread) code would. | ||
| 507 | const uint32_t index = *reinterpret_cast<const uint32_t*>(m.base + 0x10A0); | ||
| 508 | auto** module_slots = reinterpret_cast<void**>(__readgsqword(0x58)); | ||
| 509 | void* block = index == TLS_OUT_OF_INDEXES || index >= 64 || !module_slots ? nullptr : module_slots[index]; | ||
| 510 | if (index != m.tls_index) | ||
| 511 | failed("the module TLS index is written to AddressOfIndex", "the image and the Module disagree"); | ||
| 512 | else | ||
| 513 | pass("the module TLS index is written to AddressOfIndex"); | ||
| 504 | if (!block || std::memcmp(block, "TLS!", 4) != 0) | 514 | if (!block || std::memcmp(block, "TLS!", 4) != 0) |
| 505 | failed("the TLS template is copied into the allocated slot", "the slot doesn't hold the template"); | 515 | failed("the TLS template is copied into the module TLS slot", "the TEB slot doesn't hold the template"); |
| 506 | else | 516 | else |
| 507 | pass("the TLS template is copied into the allocated slot"); | 517 | pass("the TLS template is copied into the module TLS slot"); |
| 508 | } catch (const std::exception& e) { | 518 | } catch (const std::exception& e) { |
| 509 | failed("initialise runs a TLS callback and DllMain", e.what()); | 519 | failed("initialise runs a TLS callback and DllMain", e.what()); |
| 510 | } | 520 | } |