upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/acphal_test.cpp
diff options
context:
space:
mode:
authorTortoche <corentinjanin31@gmail.com>2026-11-23 22:19:47 +0100
committerTortoche <corentinjanin31@gmail.com>2026-11-23 22:19:47 +0100
commitccd36246f7f50dc6a5f1f30ff76418d570f12fa2 (patch)
treee848016ed4bf9f6de308faa16f84a50f6a681e6e /runtime/test/acphal_test.cpp
parent5d74c272363f2703265d44e6aaab1a1df8ddfaf3 (diff)
runtime: acphal.dll shim with APU heap management and SHAPE context allocation (RT-5, ASSET-587)
- Resolve Stop 28 boot blocker preventing GTA 6 from launching: acphal.dll!ApuCreateHeap and AcpHalAllocateShapeContexts. - Implement genuine APU physical memory management (ApuCreateHeap, ApuAlloc, ApuFree, ApuIsVirtualAddressValid, ApuMapApuAddress, ApuMapVirtualAddress): - Support cached and non-cached heap allocations with flags routing (flags=2 to non-cached, flags=1/0 to cached). - Genuine block tracking allowing proper memory reclamation and reuse upon ApuFree. - Disjoint APU 32-bit physical address space mapping with round-trip address conversions. - Implement IAcpHal interface in AcpHalCreate: - Slot 0 (connect): S_OK. - Slot 1 (disconnect): S_OK. - Slot 2 (submit_command): S_OK (unblocks fatal int3 at +0x43ad94e at boot). - Slots 3 and 4 (pop_message, get_num_messages): return 0. - Slot 5 (release): releases object. - Implement GDK SHAPE_CONTEXTS and exact layout: - Six context counts and arrays: SRC, EQCOMP, FILTVOL, DMA, XMA, PCM. - Structure alignment with xmaContextArray at offset +0x38, matching retail executable measurement (350 contexts x 64 bytes). - AcpHalReleaseShapeContexts releases allocated context arrays. - Classify acphal.dll HRESULT imports in registry.cpp and update unimplemented_policy_test.cpp. - Add acphal_test in runtime/CMakeLists.txt and python verification test tests/test_acphal_abi.py. - Update fallback file list in scripts/build_runtime.ps1. No game data. AI-assisted.
Diffstat (limited to 'runtime/test/acphal_test.cpp')
-rw-r--r--runtime/test/acphal_test.cpp262
1 files changed, 262 insertions, 0 deletions
diff --git a/runtime/test/acphal_test.cpp b/runtime/test/acphal_test.cpp
new file mode 100644
index 000000000..bd93f7040
--- /dev/null
+++ b/runtime/test/acphal_test.cpp
@@ -0,0 +1,262 @@
1// Checks the registered acphal.dll shim against the measured call shapes
2// (docs/retail_boot_chain.md, docs/retail_boot_frame_loop.md Stop 28, PR #6885, PR #7009).
3//
4// All 9 exports are resolved through rt::shims::find so the test exercises
5// the exact shim table and calling convention the runtime installs.
6
7#include <windows.h>
8
9#include <cstddef>
10#include <cstdint>
11#include <cstdio>
12#include <cstring>
13
14#include "shims.h"
15
16namespace {
17
18using AcpHalCreateFn = HRESULT(__stdcall*)(void**);
19using AcpHalAllocShapeCtxFn = HRESULT(__stdcall*)(void*);
20using AcpHalReleaseShapeCtxFn = HRESULT(__stdcall*)(void*);
21using ApuCreateHeapFn = HRESULT(__stdcall*)(uint32_t, uint32_t);
22// Measured: ApuAlloc(void** va, uint32_t* apu, uint32_t size, uint32_t align, uint32_t flags)
23using ApuAllocFn = HRESULT(__stdcall*)(void**, uint32_t*, uint32_t, uint32_t, uint32_t);
24using ApuFreeFn = void(__stdcall*)(void*);
25// Measured: ApuIsVirtualAddressValid(void* va, uint32_t align)
26using ApuIsValidFn = BOOL(__stdcall*)(void*, uint32_t);
27using ApuMapApuAddrFn = void*(__stdcall*)(uint32_t);
28using ApuMapVirtualAddrFn = uint32_t(__stdcall*)(void*);
29
30int g_checks = 0;
31int g_failures = 0;
32
33void check(bool condition, const char* what) {
34 ++g_checks;
35 std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
36 if (!condition) ++g_failures;
37}
38
39// Test mirror of SHAPE_CONTEXTS matching devdocs.xbox.com GDK spec
40struct APU_ADDRESS {
41 uint64_t value;
42};
43
44struct TestShapeContexts {
45 uint32_t numSrcContexts;
46 uint32_t numEqCompContexts;
47 uint32_t numFiltVolContexts;
48 uint32_t numDmaContexts;
49 uint32_t numXmaContexts;
50 uint32_t numPcmContexts;
51 void* srcContextArray;
52 void* eqCompContextArray;
53 void* filtVolContextArray;
54 void* dmaContextArray;
55 void* xmaContextArray;
56 void* pcmContextArray;
57 APU_ADDRESS apuSrcContextArray;
58 APU_ADDRESS apuEqCompContextArray;
59 APU_ADDRESS apuFiltVolContextArray;
60 APU_ADDRESS apuDmaContextArray;
61 APU_ADDRESS apuXmaContextArray;
62 APU_ADDRESS apuPcmContextArray;
63};
64
65static_assert(sizeof(TestShapeContexts) == 120, "TestShapeContexts is 120 bytes");
66static_assert(offsetof(TestShapeContexts, xmaContextArray) == 0x38,
67 "xmaContextArray is at offset +0x38");
68
69} // namespace
70
71int main() {
72 std::printf("acphal_test: registered acphal.dll shim verification\n");
73
74 const auto create = reinterpret_cast<AcpHalCreateFn>(
75 rt::shims::find("acphal.dll", "AcpHalCreate"));
76 const auto alloc_ctx = reinterpret_cast<AcpHalAllocShapeCtxFn>(
77 rt::shims::find("acphal.dll", "AcpHalAllocateShapeContexts"));
78 const auto release_ctx = reinterpret_cast<AcpHalReleaseShapeCtxFn>(
79 rt::shims::find("acphal.dll", "AcpHalReleaseShapeContexts"));
80 const auto create_heap = reinterpret_cast<ApuCreateHeapFn>(
81 rt::shims::find("acphal.dll", "ApuCreateHeap"));
82 const auto alloc = reinterpret_cast<ApuAllocFn>(
83 rt::shims::find("acphal.dll", "ApuAlloc"));
84 const auto free_mem = reinterpret_cast<ApuFreeFn>(
85 rt::shims::find("acphal.dll", "ApuFree"));
86 const auto is_valid = reinterpret_cast<ApuIsValidFn>(
87 rt::shims::find("acphal.dll", "ApuIsVirtualAddressValid"));
88 const auto map_apu = reinterpret_cast<ApuMapApuAddrFn>(
89 rt::shims::find("acphal.dll", "ApuMapApuAddress"));
90 const auto map_virtual = reinterpret_cast<ApuMapVirtualAddrFn>(
91 rt::shims::find("acphal.dll", "ApuMapVirtualAddress"));
92
93 check(create != nullptr, "AcpHalCreate is registered in registry");
94 check(alloc_ctx != nullptr, "AcpHalAllocateShapeContexts is registered in registry");
95 check(release_ctx != nullptr, "AcpHalReleaseShapeContexts is registered in registry");
96 check(create_heap != nullptr, "ApuCreateHeap is registered in registry");
97 check(alloc != nullptr, "ApuAlloc is registered in registry");
98 check(free_mem != nullptr, "ApuFree is registered in registry");
99 check(is_valid != nullptr, "ApuIsVirtualAddressValid is registered in registry");
100 check(map_apu != nullptr, "ApuMapApuAddress is registered in registry");
101 check(map_virtual != nullptr, "ApuMapVirtualAddress is registered in registry");
102 check(rt::shims::owns_dll("acphal.dll"), "the registry owns acphal.dll");
103
104 if (!create || !alloc_ctx || !release_ctx || !create_heap || !alloc ||
105 !free_mem || !is_valid || !map_apu || !map_virtual) {
106 std::printf("Fatal: Missing export bindings for acphal.dll\n");
107 return 1;
108 }
109
110 // 1. AcpHalCreate
111 void* acp = nullptr;
112 check(create(&acp) == S_OK && acp != nullptr,
113 "AcpHalCreate returns S_OK with a non-null object");
114 check(create(nullptr) == E_POINTER,
115 "AcpHalCreate(nullptr) returns E_POINTER");
116
117 if (acp) {
118 using Slot0Fn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t);
119 using Slot1Fn = HRESULT(__stdcall*)(void*);
120 using Slot2Fn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t, uint32_t, const void*, uint32_t);
121 using Slot3Fn = uint32_t(__stdcall*)(void*);
122 using Slot5Fn = HRESULT(__stdcall*)(void*);
123
124 auto vtable = *static_cast<void* const* const*>(acp);
125 check(vtable[0] != nullptr, "IAcpHal vtable slot 0 (connect) is present");
126 check(vtable[1] != nullptr, "IAcpHal vtable slot 1 (disconnect) is present");
127 check(vtable[2] != nullptr, "IAcpHal vtable slot 2 (submit_command) is present");
128 check(vtable[3] != nullptr, "IAcpHal vtable slot 3 (pop_message) is present");
129 check(vtable[4] != nullptr, "IAcpHal vtable slot 4 (get_num_messages) is present");
130 check(vtable[5] != nullptr, "IAcpHal vtable slot 5 (release) is present");
131
132 // Slot 0: Connect (called at boot with (0x1000, 0x1000))
133 check(reinterpret_cast<Slot0Fn>(vtable[0])(acp, 0x1000, 0x1000) == S_OK,
134 "IAcpHal slot 0 (connect) returns S_OK");
135
136 // Slot 2: SubmitCommand. Critical boot requirement: The title calls
137 // slot 2 as (2, 0xf0f0f0f0, -1, config, 0) and treats failure as fatal int3.
138 check(reinterpret_cast<Slot2Fn>(vtable[2])(acp, 2, 0xf0f0f0f0, 0xffffffffu, nullptr, 0) == S_OK,
139 "IAcpHal slot 2 (submit_command) returns S_OK (vital boot unblocker)");
140
141 // Slots 3 and 4: Message polling returns 0
142 check(reinterpret_cast<Slot3Fn>(vtable[3])(acp) == 0, "IAcpHal slot 3 returns 0");
143 check(reinterpret_cast<Slot3Fn>(vtable[4])(acp) == 0, "IAcpHal slot 4 returns 0");
144
145 // Slot 1: Disconnect returns S_OK
146 check(reinterpret_cast<Slot1Fn>(vtable[1])(acp) == S_OK, "IAcpHal slot 1 returns S_OK");
147
148 // Slot 5: Release returns S_OK
149 check(reinterpret_cast<Slot5Fn>(vtable[5])(acp) == S_OK, "IAcpHal slot 5 returns S_OK");
150 }
151
152 // 2. ApuCreateHeap: title passes (cachedSize, nonCachedSize=0x226e00)
153 check(create_heap(4 * 1024 * 1024, 0x226e00) == S_OK,
154 "ApuCreateHeap(4 MiB, 0x226e00) succeeds");
155 check(create_heap(4 * 1024 * 1024, 0x226e00) != S_OK,
156 "ApuCreateHeap second call fails (heap already created)");
157
158 // 3. ApuAlloc argument validation
159 void* dummy = nullptr;
160 check(alloc(nullptr, nullptr, 1024, 0x800, 0) == E_POINTER,
161 "ApuAlloc(nullptr, ...) returns E_POINTER");
162 check(alloc(&dummy, nullptr, 0, 0x800, 0) == E_INVALIDARG,
163 "ApuAlloc with size=0 returns E_INVALIDARG");
164 check(alloc(&dummy, nullptr, 1024, 3, 0) == E_INVALIDARG,
165 "ApuAlloc with unaligned alignment (3) returns E_INVALIDARG");
166
167 // 4. ApuAlloc real boot sequence:
168 // First call: flags=2, size=0x222e00, align=0x800 (noncached heap)
169 void* va_nc = nullptr;
170 uint32_t apu_nc = 0;
171 check(alloc(&va_nc, &apu_nc, 0x222e00, 0x800, 2) == S_OK && va_nc != nullptr,
172 "ApuAlloc flags=2 at title's non-cached size 0x222e00 succeeds");
173 check(apu_nc != 0, "ApuAlloc provides non-zero physical APU address");
174
175 // Second call: flags=1, size=4096, align=0x800 (cached heap)
176 void* va_c = nullptr;
177 uint32_t apu_c = 0;
178 check(alloc(&va_c, &apu_c, 4096, 0x800, 1) == S_OK && va_c != nullptr,
179 "ApuAlloc flags=1 succeeds on cached heap");
180 check(apu_c != 0, "ApuAlloc flags=1 provides non-zero APU address");
181
182 // Address verification & disjoint ranges
183 check(va_nc != va_c, "Cached and non-cached allocations return different virtual pointers");
184 check(apu_nc != apu_c, "Cached and non-cached APU addresses are distinct");
185 check(is_valid(va_nc, 0x800) == TRUE, "Non-cached allocation is in a valid heap");
186 check(is_valid(va_c, 0x800) == TRUE, "Cached allocation is in a valid heap");
187 check(is_valid(nullptr, 0x800) == FALSE, "ApuIsVirtualAddressValid(nullptr) returns FALSE");
188 check(is_valid(reinterpret_cast<void*>(0x1234), 0x800) == FALSE,
189 "ApuIsVirtualAddressValid(bogus) returns FALSE");
190
191 // Round-trip mapping
192 check(map_virtual(va_nc) == apu_nc, "ApuMapVirtualAddress matches apu_nc");
193 check(map_virtual(va_c) == apu_c, "ApuMapVirtualAddress matches apu_c");
194 check(map_apu(apu_nc) == va_nc, "ApuMapApuAddress round-trips non-cached address");
195 check(map_apu(apu_c) == va_c, "ApuMapApuAddress round-trips cached address");
196 check(map_apu(0) == nullptr, "ApuMapApuAddress(0) returns nullptr");
197 check(map_virtual(nullptr) == 0, "ApuMapVirtualAddress(nullptr) returns 0");
198
199 // 5. Genuine State Tracking: Free & Memory Reuse
200 void* reusable_block1 = nullptr;
201 check(alloc(&reusable_block1, nullptr, 0x1000, 0x800, 1) == S_OK && reusable_block1 != nullptr,
202 "Allocate 4 KiB reusable block");
203 free_mem(reusable_block1);
204 check(true, "ApuFree on valid address succeeds without error");
205 free_mem(nullptr);
206 check(true, "ApuFree(nullptr) is a safe no-op");
207
208 // Reallocate in the freed slot:
209 void* reusable_block2 = nullptr;
210 check(alloc(&reusable_block2, nullptr, 0x1000, 0x800, 1) == S_OK && reusable_block2 != nullptr,
211 "Re-allocate in cached heap succeeds (memory successfully reused)");
212 check(reusable_block2 == reusable_block1,
213 "Free block was genuinely reused at the same offset by the allocator");
214 free_mem(reusable_block2);
215
216 // 6. AcpHalAllocateShapeContexts & AcpHalReleaseShapeContexts
217 check(alloc_ctx(nullptr) == E_POINTER,
218 "AcpHalAllocateShapeContexts(nullptr) returns E_POINTER");
219
220 // Real executable call: 0 SRC, 350 XMA (+0x38), 0 PCM
221 TestShapeContexts ctx{};
222 ctx.numXmaContexts = 350;
223 check(alloc_ctx(&ctx) == S_OK, "AcpHalAllocateShapeContexts succeeds");
224 check(ctx.xmaContextArray != nullptr,
225 "xmaContextArray (+0x38) is non-null after allocation");
226 check(ctx.srcContextArray == nullptr, "srcContextArray is null when count is 0");
227
228 // Verify zeroed memory across all 350 * 64 bytes
229 const uint8_t* xma_bytes = static_cast<const uint8_t*>(ctx.xmaContextArray);
230 bool all_zero = true;
231 for (size_t i = 0; i < 350 * 64; ++i) {
232 if (xma_bytes[i] != 0) {
233 all_zero = false;
234 break;
235 }
236 }
237 check(all_zero, "SHAPE context buffer is zero-initialized for title's stride");
238
239 // Test multi-context allocation
240 TestShapeContexts ctx_all{};
241 ctx_all.numSrcContexts = 10;
242 ctx_all.numEqCompContexts = 5;
243 ctx_all.numFiltVolContexts = 8;
244 ctx_all.numDmaContexts = 4;
245 ctx_all.numXmaContexts = 12;
246 ctx_all.numPcmContexts = 6;
247 check(alloc_ctx(&ctx_all) == S_OK, "AcpHalAllocateShapeContexts with all 6 types succeeds");
248 check(ctx_all.srcContextArray != nullptr, "srcContextArray is allocated");
249 check(ctx_all.eqCompContextArray != nullptr, "eqCompContextArray is allocated");
250 check(ctx_all.filtVolContextArray != nullptr, "filtVolContextArray is allocated");
251 check(ctx_all.dmaContextArray != nullptr, "dmaContextArray is allocated");
252 check(ctx_all.xmaContextArray != nullptr, "xmaContextArray is allocated");
253 check(ctx_all.pcmContextArray != nullptr, "pcmContextArray is allocated");
254
255 // Release shape contexts
256 check(release_ctx(&ctx_all) == S_OK, "AcpHalReleaseShapeContexts succeeds");
257 check(ctx_all.xmaContextArray == nullptr, "xmaContextArray is reset to null");
258 check(release_ctx(nullptr) == S_OK, "AcpHalReleaseShapeContexts(nullptr) succeeds");
259
260 std::printf("acphal_test complete: %d checks, %d failures\n", g_checks, g_failures);
261 return g_failures ? 1 : 0;
262}