mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 13:18:37 +00:00
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.
This commit is contained in:
@ -109,6 +109,7 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
|
||||
shims/winhttp_offline.cpp
|
||||
shims/xmem.cpp
|
||||
shims/xg_xs.cpp
|
||||
shims/acphal.cpp
|
||||
shims/d3d12_xs.cpp
|
||||
shims/d3d12_xs_device.cpp
|
||||
shims/xgameruntime.cpp
|
||||
@ -224,6 +225,17 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
|
||||
target_compile_options(xg_xs_test PRIVATE /W4 /permissive- /utf-8)
|
||||
target_link_options(xg_xs_test PRIVATE /OPT:NOREF)
|
||||
|
||||
# acphal shim ABI test: resolves the registered acphal.dll exports through the registry.
|
||||
add_executable(acphal_test
|
||||
test/acphal_test.cpp
|
||||
shims/acphal.cpp
|
||||
shims/registry.cpp
|
||||
)
|
||||
target_include_directories(acphal_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims)
|
||||
target_compile_definitions(acphal_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
|
||||
target_compile_options(acphal_test PRIVATE /W4 /permissive- /utf-8)
|
||||
target_link_options(acphal_test PRIVATE /OPT:NOREF)
|
||||
|
||||
# The process shim's command line: GetCommandLineW must parse (CommandLineToArgvW) back to its __wargv.
|
||||
add_executable(process_cmdline_test
|
||||
test/process_cmdline_test.cpp
|
||||
@ -1013,6 +1025,9 @@ if(BUILD_TESTING)
|
||||
if(WIN32 AND TARGET xg_xs_test)
|
||||
add_test(NAME xg_xs COMMAND xg_xs_test)
|
||||
endif()
|
||||
if(WIN32 AND TARGET acphal_test)
|
||||
add_test(NAME acphal COMMAND acphal_test)
|
||||
endif()
|
||||
if(WIN32 AND TARGET process_cmdline_test)
|
||||
add_test(NAME process_cmdline COMMAND process_cmdline_test)
|
||||
endif()
|
||||
|
||||
568
runtime/shims/acphal.cpp
Normal file
568
runtime/shims/acphal.cpp
Normal file
@ -0,0 +1,568 @@
|
||||
// acphal.dll: Audio Control Processor HAL. Scalable Hardware Audio Processing Engine
|
||||
// (SHAPE) and APU (Audio Processing Unit) physical memory management for Xbox Series.
|
||||
//
|
||||
// On a PC there is no SHAPE/ACP hardware and APU physical memory does not exist.
|
||||
// This shim creates the APU heap from process memory and manages allocations within it.
|
||||
// The SHAPE context arrays are allocated on the process heap; the ACP interface
|
||||
// accepts and acknowledges commands to allow audio bring-up to proceed cleanly.
|
||||
//
|
||||
// Signatures from the public GDK reference (devdocs.xbox.com):
|
||||
// AcpHal, APU reference, SHAPE_CONTEXTS, APU_ADDRESS.
|
||||
// Measured call-site shapes and findings from the retail executable (1.0.2.1):
|
||||
// - docs/retail_boot_chain.md, docs/retail_boot_frame_loop.md (Stop 28)
|
||||
// - PR #6885 (call sites and parameter analysis), PR #7009 (audio/HAL bring-up).
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "shims.h"
|
||||
#include "startup_trace.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// ---- APU_ADDRESS (from the GDK) ----
|
||||
|
||||
// The console APU uses a 40-bit physical address space.
|
||||
// On PC we translate to a 32-bit APU address offset.
|
||||
struct APU_ADDRESS {
|
||||
uint64_t value; // 40-bit physical address
|
||||
};
|
||||
|
||||
// ---- SHAPE context structures (from the public GDK documentation at devdocs.xbox.com) ----
|
||||
//
|
||||
// These structures are defined here with their documented GDK field layouts so that
|
||||
// sizeof() returns the correct per-context byte count. Without the real layouts the
|
||||
// empty structs would be 1 byte each, and the title's writes (e.g. memset with the
|
||||
// real stride) would overflow the allocation.
|
||||
|
||||
// UINT32 bitfields packed into 32-bit words
|
||||
struct SHAPE_SRC_CONTEXT {
|
||||
uint32_t timestamp : 21;
|
||||
uint32_t blocksToSkip : 2;
|
||||
uint32_t internalSaturate : 1;
|
||||
uint32_t mixBufPeakMag : 4;
|
||||
uint32_t peakMag : 4;
|
||||
uint32_t samplingIncrement : 21;
|
||||
uint32_t reserved0 : 1;
|
||||
uint32_t cmd : 2;
|
||||
uint32_t mixBufPeakMag2 : 4;
|
||||
uint32_t peakMag2 : 4;
|
||||
uint32_t sampleCount;
|
||||
uint32_t samplePointer : 25;
|
||||
uint32_t reserved1 : 7;
|
||||
uint32_t samplingIncrementTarget : 21;
|
||||
uint32_t reserved2 : 11;
|
||||
uint32_t reserved[3];
|
||||
};
|
||||
static_assert(sizeof(SHAPE_SRC_CONTEXT) == 32, "SHAPE_SRC_CONTEXT is 32 bytes per GDK");
|
||||
|
||||
// The EQ/Comp context: 88 UINT32s = 352 bytes per GDK definition.
|
||||
#pragma pack(push, 1)
|
||||
struct alignas(4) SHAPE_EQCOMP_CONTEXT {
|
||||
uint8_t data[352];
|
||||
};
|
||||
#pragma pack(pop)
|
||||
static_assert(sizeof(SHAPE_EQCOMP_CONTEXT) == 352, "SHAPE_EQCOMP_CONTEXT is 352 bytes per GDK");
|
||||
|
||||
struct SHAPE_FILTVOL_CONTEXT {
|
||||
uint32_t timestamp : 21;
|
||||
uint32_t reserved0 : 2;
|
||||
uint32_t internalSaturate : 1;
|
||||
uint32_t mixBufPeakMag : 4;
|
||||
uint32_t peakMag : 4;
|
||||
uint32_t delay1 : 30;
|
||||
uint32_t headroom : 2;
|
||||
uint32_t delay2 : 30;
|
||||
uint32_t svfMode : 2;
|
||||
uint32_t gainTarget : 16;
|
||||
uint32_t gain : 16;
|
||||
uint32_t qRecip : 24;
|
||||
uint32_t reserved1 : 8;
|
||||
uint32_t qRecipTarget : 24;
|
||||
uint32_t reserved2 : 8;
|
||||
uint32_t fc : 24;
|
||||
uint32_t reserved3 : 8;
|
||||
uint32_t fcTarget : 24;
|
||||
uint32_t reserved4 : 8;
|
||||
};
|
||||
static_assert(sizeof(SHAPE_FILTVOL_CONTEXT) == 32, "SHAPE_FILTVOL_CONTEXT is 32 bytes per GDK");
|
||||
|
||||
struct SHAPE_DMA_CONTEXT {
|
||||
uint32_t timestamp : 21;
|
||||
uint32_t reserved0 : 3;
|
||||
uint32_t mixBufPeakMag : 4;
|
||||
uint32_t peakMag : 4;
|
||||
uint32_t readPointer : 5;
|
||||
uint32_t reserved1 : 3;
|
||||
uint32_t writePointer : 5;
|
||||
uint32_t reserved2 : 3;
|
||||
uint32_t full : 1;
|
||||
uint32_t reserved3 : 15;
|
||||
uint32_t reserved4 : 9;
|
||||
uint32_t address : 23;
|
||||
uint32_t numFrames : 5;
|
||||
uint32_t reserved5 : 3;
|
||||
uint32_t floatConvert : 1;
|
||||
uint32_t reserved6 : 7;
|
||||
uint32_t numChannels : 3;
|
||||
uint32_t reserved7 : 5;
|
||||
uint32_t channel : 3;
|
||||
uint32_t reserved8 : 5;
|
||||
};
|
||||
static_assert(sizeof(SHAPE_DMA_CONTEXT) == 16, "SHAPE_DMA_CONTEXT is 16 bytes per GDK");
|
||||
|
||||
struct SHAPE_XMA_CONTEXT {
|
||||
uint32_t sizeRead0 : 12;
|
||||
uint32_t numLoops : 8;
|
||||
uint32_t validBuffer : 2;
|
||||
uint32_t sizeWrite : 5;
|
||||
uint32_t offsetWrite : 5;
|
||||
uint32_t sizeRead1 : 12;
|
||||
uint32_t sequenceNumber : 12;
|
||||
uint32_t stopSampleAddress : 8;
|
||||
uint32_t subframeOffset : 4;
|
||||
uint32_t reserved0 : 1;
|
||||
uint32_t isLooping : 1;
|
||||
uint32_t subframeSkip : 2;
|
||||
uint32_t packetMetadata : 16;
|
||||
uint32_t currentByteOffset : 26;
|
||||
uint32_t reserved1 : 6;
|
||||
uint32_t loopStartOffset : 26;
|
||||
uint32_t loopEndOffset : 6;
|
||||
uint32_t reserved[10];
|
||||
};
|
||||
static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK");
|
||||
|
||||
struct SHAPE_PCM_CONTEXT {
|
||||
uint32_t timestamp : 21;
|
||||
uint32_t reserved0 : 11;
|
||||
uint32_t loopCount : 8;
|
||||
uint32_t reserved1 : 8;
|
||||
uint32_t full : 1;
|
||||
uint32_t reserved2 : 15;
|
||||
uint32_t readPointer : 21;
|
||||
uint32_t reserved3 : 11;
|
||||
uint32_t loopStartWritePointer : 21;
|
||||
uint32_t reserved4 : 11;
|
||||
uint32_t loopEnd : 21;
|
||||
uint32_t reserved5 : 11;
|
||||
uint32_t mode : 1;
|
||||
uint32_t reserved6 : 15;
|
||||
uint32_t format : 2;
|
||||
uint32_t reserved7 : 14;
|
||||
uint32_t reserved[1];
|
||||
};
|
||||
static_assert(sizeof(SHAPE_PCM_CONTEXT) == 32, "SHAPE_PCM_CONTEXT is 32 bytes per GDK");
|
||||
|
||||
// The SHAPE_CONTEXTS structure (from the GDK)
|
||||
struct SHAPE_CONTEXTS {
|
||||
uint32_t numSrcContexts;
|
||||
uint32_t numEqCompContexts;
|
||||
uint32_t numFiltVolContexts;
|
||||
uint32_t numDmaContexts;
|
||||
uint32_t numXmaContexts;
|
||||
uint32_t numPcmContexts;
|
||||
SHAPE_SRC_CONTEXT* srcContextArray;
|
||||
SHAPE_EQCOMP_CONTEXT* eqCompContextArray;
|
||||
SHAPE_FILTVOL_CONTEXT* filtVolContextArray;
|
||||
SHAPE_DMA_CONTEXT* dmaContextArray;
|
||||
SHAPE_XMA_CONTEXT* xmaContextArray;
|
||||
SHAPE_PCM_CONTEXT* pcmContextArray;
|
||||
APU_ADDRESS apuSrcContextArray;
|
||||
APU_ADDRESS apuEqCompContextArray;
|
||||
APU_ADDRESS apuFiltVolContextArray;
|
||||
APU_ADDRESS apuDmaContextArray;
|
||||
APU_ADDRESS apuXmaContextArray;
|
||||
APU_ADDRESS apuPcmContextArray;
|
||||
};
|
||||
|
||||
static_assert(sizeof(SHAPE_CONTEXTS) == 120, "SHAPE_CONTEXTS is 120 bytes per GDK");
|
||||
static_assert(offsetof(SHAPE_CONTEXTS, xmaContextArray) == 0x38,
|
||||
"xmaContextArray is at offset +0x38 per retail binary measurement");
|
||||
|
||||
// ---- Startup trace helper ----
|
||||
|
||||
void trace_call(const char* api, const std::string& detail) {
|
||||
if (!rt::startup_trace::enabled()) return;
|
||||
static std::mutex mutex;
|
||||
static std::set<std::string> seen;
|
||||
{
|
||||
const std::lock_guard lock(mutex);
|
||||
if (!seen.insert(std::string(api) + detail).second) return;
|
||||
}
|
||||
rt::startup_trace::event("acphal",
|
||||
std::string("\"api\":\"") + api + "\"" + detail);
|
||||
}
|
||||
|
||||
// ---- Genuine APU Heap Implementation ----
|
||||
|
||||
constexpr uint32_t kApuBaseAddress = 0x40000000u;
|
||||
|
||||
struct ApuRegion {
|
||||
void* base = nullptr;
|
||||
uint64_t size = 0;
|
||||
std::map<uint32_t, uint32_t> blocks; // offset -> size of active allocations
|
||||
|
||||
bool contains(const void* ptr) const {
|
||||
if (!base || !ptr) return false;
|
||||
const uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
|
||||
const uintptr_t b = reinterpret_cast<uintptr_t>(base);
|
||||
return p >= b && p < b + size;
|
||||
}
|
||||
|
||||
bool is_valid_address(const void* ptr, uint32_t alignment) const {
|
||||
if (!base || !ptr) return false;
|
||||
const uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
|
||||
const uintptr_t b = reinterpret_cast<uintptr_t>(base);
|
||||
if (p < b || p >= b + size) return false;
|
||||
if (alignment == 0) alignment = 1;
|
||||
return ((p - b) & (static_cast<uintptr_t>(alignment) - 1)) == 0;
|
||||
}
|
||||
|
||||
uint32_t to_offset(const void* ptr) const {
|
||||
return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr) -
|
||||
reinterpret_cast<uintptr_t>(base));
|
||||
}
|
||||
|
||||
void* from_offset(uint32_t offset) const {
|
||||
if (offset >= size) return nullptr;
|
||||
return static_cast<uint8_t*>(base) + offset;
|
||||
}
|
||||
|
||||
void* allocate(uint32_t alloc_size, uint32_t alignment, uint32_t* out_offset) {
|
||||
if (alloc_size == 0 || alignment == 0) return nullptr;
|
||||
uint64_t at = 0;
|
||||
for (auto it = blocks.begin();; ++it) {
|
||||
at = (at + alignment - 1) & ~static_cast<uint64_t>(alignment - 1);
|
||||
const uint64_t limit = (it == blocks.end()) ? size : it->first;
|
||||
if (at + alloc_size <= limit) break;
|
||||
if (it == blocks.end()) return nullptr; // out of memory
|
||||
at = static_cast<uint64_t>(it->first) + it->second;
|
||||
}
|
||||
const uint32_t offset = static_cast<uint32_t>(at);
|
||||
blocks.emplace(offset, alloc_size);
|
||||
uint8_t* ptr = static_cast<uint8_t*>(base) + offset;
|
||||
std::memset(ptr, 0, alloc_size);
|
||||
if (out_offset) *out_offset = offset;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
bool free_block(void* ptr) {
|
||||
if (!contains(ptr)) return false;
|
||||
const uint32_t offset = to_offset(ptr);
|
||||
return blocks.erase(offset) > 0;
|
||||
}
|
||||
|
||||
void release() {
|
||||
if (base) {
|
||||
VirtualFree(base, 0, MEM_RELEASE);
|
||||
base = nullptr;
|
||||
}
|
||||
size = 0;
|
||||
blocks.clear();
|
||||
}
|
||||
};
|
||||
|
||||
std::mutex g_apu_mutex;
|
||||
bool g_apu_heap_created = false;
|
||||
ApuRegion g_cached_heap;
|
||||
ApuRegion g_noncached_heap;
|
||||
|
||||
// Tracked SHAPE context arrays
|
||||
std::vector<void*> g_active_shape_arrays;
|
||||
|
||||
// ---- IAcpHal Interface Implementation ----
|
||||
|
||||
struct AcpHalImpl {
|
||||
// Slot 0: Connect (called at boot with (0x1000, 0x1000), result checked)
|
||||
virtual HRESULT __stdcall connect(uint32_t, uint32_t) {
|
||||
trace_call("AcpHal::Connect", "");
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
// Slot 1: Disconnect (called at shutdown)
|
||||
virtual HRESULT __stdcall disconnect() {
|
||||
trace_call("AcpHal::Disconnect", "");
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
// Slot 2: SubmitCommand
|
||||
// Critical: The title calls slot 2 as (2, 0xf0f0f0f0, -1, config, 0) and
|
||||
// a negative result triggers a fatal int3 kernel panic (+0x43ad94e).
|
||||
virtual HRESULT __stdcall submit_command(uint32_t cmd, uint32_t d1, uint32_t d2,
|
||||
const void* config, uint32_t d3) {
|
||||
(void)cmd;
|
||||
(void)d1;
|
||||
(void)d2;
|
||||
(void)config;
|
||||
(void)d3;
|
||||
static std::atomic<bool> warned{false};
|
||||
if (!warned.exchange(true, std::memory_order_relaxed)) {
|
||||
std::fprintf(stderr,
|
||||
"[rt] acphal: command accepted and dropped: no audio DSP hardware on PC\n");
|
||||
}
|
||||
trace_call("AcpHal::SubmitCommand", "");
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
// Slots 3 and 4: PopMessage and GetNumMessages (message polling, not called by title)
|
||||
virtual uint32_t __stdcall pop_message() { return 0; }
|
||||
virtual uint32_t __stdcall get_num_messages() { return 0; }
|
||||
|
||||
// Slot 5: Release
|
||||
virtual HRESULT __stdcall release() {
|
||||
trace_call("AcpHal::Release", "");
|
||||
delete this;
|
||||
return S_OK;
|
||||
}
|
||||
};
|
||||
|
||||
// ---- AcpHalCreate ----
|
||||
|
||||
HRESULT __stdcall AcpHalCreate(void** ppAcpHal) {
|
||||
if (!ppAcpHal) return E_POINTER;
|
||||
*ppAcpHal = new AcpHalImpl();
|
||||
trace_call("AcpHalCreate", "");
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
// ---- AcpHalAllocateShapeContexts ----
|
||||
|
||||
HRESULT __stdcall AcpHalAllocateShapeContexts(SHAPE_CONTEXTS* ctx) {
|
||||
if (!ctx) return E_POINTER;
|
||||
|
||||
std::lock_guard lock(g_apu_mutex);
|
||||
|
||||
auto alloc_array = [](uint32_t count, size_t elem_size) -> void* {
|
||||
if (count == 0) return nullptr;
|
||||
void* ptr = std::calloc(count, elem_size);
|
||||
if (ptr) g_active_shape_arrays.push_back(ptr);
|
||||
return ptr;
|
||||
};
|
||||
|
||||
ctx->srcContextArray = static_cast<SHAPE_SRC_CONTEXT*>(
|
||||
alloc_array(ctx->numSrcContexts, sizeof(SHAPE_SRC_CONTEXT)));
|
||||
ctx->eqCompContextArray = static_cast<SHAPE_EQCOMP_CONTEXT*>(
|
||||
alloc_array(ctx->numEqCompContexts, sizeof(SHAPE_EQCOMP_CONTEXT)));
|
||||
ctx->filtVolContextArray = static_cast<SHAPE_FILTVOL_CONTEXT*>(
|
||||
alloc_array(ctx->numFiltVolContexts, sizeof(SHAPE_FILTVOL_CONTEXT)));
|
||||
ctx->dmaContextArray = static_cast<SHAPE_DMA_CONTEXT*>(
|
||||
alloc_array(ctx->numDmaContexts, sizeof(SHAPE_DMA_CONTEXT)));
|
||||
ctx->xmaContextArray = static_cast<SHAPE_XMA_CONTEXT*>(
|
||||
alloc_array(ctx->numXmaContexts, sizeof(SHAPE_XMA_CONTEXT)));
|
||||
ctx->pcmContextArray = static_cast<SHAPE_PCM_CONTEXT*>(
|
||||
alloc_array(ctx->numPcmContexts, sizeof(SHAPE_PCM_CONTEXT)));
|
||||
|
||||
// On PC there is no physical APU address translation for host context arrays;
|
||||
// assign unique synthetic addresses from the APU base.
|
||||
ctx->apuSrcContextArray = {ctx->srcContextArray ? kApuBaseAddress : 0};
|
||||
ctx->apuEqCompContextArray = {ctx->eqCompContextArray ? kApuBaseAddress + 0x1000 : 0};
|
||||
ctx->apuFiltVolContextArray = {ctx->filtVolContextArray ? kApuBaseAddress + 0x2000 : 0};
|
||||
ctx->apuDmaContextArray = {ctx->dmaContextArray ? kApuBaseAddress + 0x3000 : 0};
|
||||
ctx->apuXmaContextArray = {ctx->xmaContextArray ? kApuBaseAddress + 0x4000 : 0};
|
||||
ctx->apuPcmContextArray = {ctx->pcmContextArray ? kApuBaseAddress + 0x5000 : 0};
|
||||
|
||||
trace_call("AcpHalAllocateShapeContexts",
|
||||
",\"src\":" + std::to_string(ctx->numSrcContexts) +
|
||||
",\"xma\":" + std::to_string(ctx->numXmaContexts) +
|
||||
",\"pcm\":" + std::to_string(ctx->numPcmContexts));
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
// ---- AcpHalReleaseShapeContexts ----
|
||||
|
||||
HRESULT __stdcall AcpHalReleaseShapeContexts(SHAPE_CONTEXTS* ctx = nullptr) {
|
||||
std::lock_guard lock(g_apu_mutex);
|
||||
|
||||
// Free all recorded context allocations
|
||||
for (void* p : g_active_shape_arrays) {
|
||||
if (p) std::free(p);
|
||||
}
|
||||
g_active_shape_arrays.clear();
|
||||
|
||||
if (ctx) {
|
||||
ctx->srcContextArray = nullptr;
|
||||
ctx->eqCompContextArray = nullptr;
|
||||
ctx->filtVolContextArray = nullptr;
|
||||
ctx->dmaContextArray = nullptr;
|
||||
ctx->xmaContextArray = nullptr;
|
||||
ctx->pcmContextArray = nullptr;
|
||||
}
|
||||
|
||||
trace_call("AcpHalReleaseShapeContexts", "");
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
// ---- ApuCreateHeap ----
|
||||
|
||||
HRESULT __stdcall ApuCreateHeap(uint32_t cachedSizeInBytes,
|
||||
uint32_t nonCachedSizeInBytes) {
|
||||
std::lock_guard lock(g_apu_mutex);
|
||||
if (g_apu_heap_created) return E_FAIL;
|
||||
|
||||
if (cachedSizeInBytes > 0) {
|
||||
g_cached_heap.base = VirtualAlloc(
|
||||
nullptr, cachedSizeInBytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
if (!g_cached_heap.base) return E_OUTOFMEMORY;
|
||||
g_cached_heap.size = cachedSizeInBytes;
|
||||
}
|
||||
|
||||
if (nonCachedSizeInBytes > 0) {
|
||||
g_noncached_heap.base = VirtualAlloc(
|
||||
nullptr, nonCachedSizeInBytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
if (!g_noncached_heap.base) {
|
||||
g_cached_heap.release();
|
||||
return E_OUTOFMEMORY;
|
||||
}
|
||||
g_noncached_heap.size = nonCachedSizeInBytes;
|
||||
}
|
||||
|
||||
g_apu_heap_created = true;
|
||||
trace_call("ApuCreateHeap",
|
||||
",\"cachedSize\":" + std::to_string(cachedSizeInBytes) +
|
||||
",\"nonCachedSize\":" + std::to_string(nonCachedSizeInBytes));
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
// ---- ApuAlloc ----
|
||||
|
||||
HRESULT __stdcall ApuAlloc(void** virtualAddress, uint32_t* physicalAddress,
|
||||
uint32_t sizeInBytes, uint32_t alignmentInBytes,
|
||||
uint32_t flags) {
|
||||
if (!virtualAddress) return E_POINTER;
|
||||
*virtualAddress = nullptr;
|
||||
if (physicalAddress) *physicalAddress = 0;
|
||||
|
||||
if (sizeInBytes == 0) return E_INVALIDARG;
|
||||
if (alignmentInBytes == 0) alignmentInBytes = 1;
|
||||
if ((alignmentInBytes & (alignmentInBytes - 1)) != 0) return E_INVALIDARG;
|
||||
|
||||
std::lock_guard lock(g_apu_mutex);
|
||||
|
||||
// Routing: flags=2 routes to non-cached heap; flags=0 or 1 routes to cached heap.
|
||||
// If non-cached heap wasn't created or has no room, fall back to cached heap
|
||||
// because PC memory is unified and cache-coherent.
|
||||
const bool prefer_noncached = (flags == 2);
|
||||
ApuRegion* target = prefer_noncached ? &g_noncached_heap : &g_cached_heap;
|
||||
|
||||
if (!target->base) {
|
||||
if (!prefer_noncached) {
|
||||
// Lazy default cached heap if ApuCreateHeap wasn't called yet
|
||||
constexpr uint32_t kDefaultHeapSize = 64 * 1024 * 1024;
|
||||
target->base = VirtualAlloc(nullptr, kDefaultHeapSize,
|
||||
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
if (!target->base) return E_OUTOFMEMORY;
|
||||
target->size = kDefaultHeapSize;
|
||||
g_apu_heap_created = true;
|
||||
} else {
|
||||
// Fall back to cached heap
|
||||
target = &g_cached_heap;
|
||||
if (!target->base) return E_OUTOFMEMORY;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t offset = 0;
|
||||
void* ptr = target->allocate(sizeInBytes, alignmentInBytes, &offset);
|
||||
if (!ptr && prefer_noncached && g_cached_heap.base) {
|
||||
// Fall back to cached heap if non-cached heap is full
|
||||
target = &g_cached_heap;
|
||||
ptr = target->allocate(sizeInBytes, alignmentInBytes, &offset);
|
||||
}
|
||||
|
||||
if (!ptr) return E_OUTOFMEMORY;
|
||||
|
||||
*virtualAddress = ptr;
|
||||
if (physicalAddress) {
|
||||
// Disjoint APU address ranges:
|
||||
// cached: 0x40000000 .. 0x40000000 + cached_size - 1
|
||||
// non-cached: 0x40000000 + cached_size .. 0x40000000 + cached_size + noncached_size - 1
|
||||
uint32_t apu = kApuBaseAddress + offset;
|
||||
if (target == &g_noncached_heap) {
|
||||
apu += static_cast<uint32_t>(g_cached_heap.size);
|
||||
}
|
||||
*physicalAddress = apu;
|
||||
}
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
// ---- ApuFree ----
|
||||
|
||||
void __stdcall ApuFree(void* virtualAddress) {
|
||||
if (!virtualAddress) return;
|
||||
std::lock_guard lock(g_apu_mutex);
|
||||
if (!g_cached_heap.free_block(virtualAddress)) {
|
||||
g_noncached_heap.free_block(virtualAddress);
|
||||
}
|
||||
trace_call("ApuFree", "");
|
||||
}
|
||||
|
||||
// ---- ApuIsVirtualAddressValid ----
|
||||
|
||||
BOOL __stdcall ApuIsVirtualAddressValid(void* virtualAddress, uint32_t alignment) {
|
||||
if (!virtualAddress) return FALSE;
|
||||
if (alignment == 0) alignment = 1;
|
||||
std::lock_guard lock(g_apu_mutex);
|
||||
if (g_cached_heap.is_valid_address(virtualAddress, alignment)) return TRUE;
|
||||
if (g_noncached_heap.is_valid_address(virtualAddress, alignment)) return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// ---- ApuMapApuAddress ----
|
||||
|
||||
void* __stdcall ApuMapApuAddress(uint32_t apu) {
|
||||
if (apu == 0 || apu < kApuBaseAddress) return nullptr;
|
||||
std::lock_guard lock(g_apu_mutex);
|
||||
const uint32_t offset = apu - kApuBaseAddress;
|
||||
if (g_cached_heap.base && offset < g_cached_heap.size) {
|
||||
return g_cached_heap.from_offset(offset);
|
||||
}
|
||||
if (g_noncached_heap.base) {
|
||||
const uint32_t cached_span = static_cast<uint32_t>(g_cached_heap.size);
|
||||
if (offset >= cached_span && offset - cached_span < g_noncached_heap.size) {
|
||||
return g_noncached_heap.from_offset(offset - cached_span);
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// ---- ApuMapVirtualAddress ----
|
||||
|
||||
uint32_t __stdcall ApuMapVirtualAddress(void* virtualAddress) {
|
||||
if (!virtualAddress) return 0;
|
||||
std::lock_guard lock(g_apu_mutex);
|
||||
if (g_cached_heap.contains(virtualAddress)) {
|
||||
return kApuBaseAddress + g_cached_heap.to_offset(virtualAddress);
|
||||
}
|
||||
if (g_noncached_heap.contains(virtualAddress)) {
|
||||
return kApuBaseAddress + static_cast<uint32_t>(g_cached_heap.size) +
|
||||
g_noncached_heap.to_offset(virtualAddress);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
SHIM_TABLE(acphal,
|
||||
{"acphal.dll", "AcpHalAllocateShapeContexts", reinterpret_cast<void*>(&AcpHalAllocateShapeContexts)},
|
||||
{"acphal.dll", "AcpHalCreate", reinterpret_cast<void*>(&AcpHalCreate)},
|
||||
{"acphal.dll", "AcpHalReleaseShapeContexts", reinterpret_cast<void*>(&AcpHalReleaseShapeContexts)},
|
||||
{"acphal.dll", "ApuAlloc", reinterpret_cast<void*>(&ApuAlloc)},
|
||||
{"acphal.dll", "ApuCreateHeap", reinterpret_cast<void*>(&ApuCreateHeap)},
|
||||
{"acphal.dll", "ApuFree", reinterpret_cast<void*>(&ApuFree)},
|
||||
{"acphal.dll", "ApuIsVirtualAddressValid", reinterpret_cast<void*>(&ApuIsVirtualAddressValid)},
|
||||
{"acphal.dll", "ApuMapApuAddress", reinterpret_cast<void*>(&ApuMapApuAddress)},
|
||||
{"acphal.dll", "ApuMapVirtualAddress", reinterpret_cast<void*>(&ApuMapVirtualAddress)},
|
||||
);
|
||||
@ -113,6 +113,11 @@ ReturnClass classify(const std::string& dll, const std::string& name) {
|
||||
if (name == "GameInputCreate") return ReturnClass::HResult;
|
||||
} else if (dll == "dstorage.dll" || dll == "dstorage_xs.dll") {
|
||||
if (name == "DStorageGetFactory") return ReturnClass::HResult;
|
||||
} else if (dll == "acphal.dll") {
|
||||
if (name == "AcpHalAllocateShapeContexts" || name == "AcpHalCreate" ||
|
||||
name == "AcpHalReleaseShapeContexts" || name == "ApuAlloc" ||
|
||||
name == "ApuCreateHeap")
|
||||
return ReturnClass::HResult;
|
||||
} else if (dll == "xgameruntime.dll") {
|
||||
if (name == "XGameRuntimeInitialize" || name == "XGameEventWrite") return ReturnClass::HResult;
|
||||
for (const char* n : kNotHResult)
|
||||
|
||||
262
runtime/test/acphal_test.cpp
Normal file
262
runtime/test/acphal_test.cpp
Normal file
@ -0,0 +1,262 @@
|
||||
// Checks the registered acphal.dll shim against the measured call shapes
|
||||
// (docs/retail_boot_chain.md, docs/retail_boot_frame_loop.md Stop 28, PR #6885, PR #7009).
|
||||
//
|
||||
// All 9 exports are resolved through rt::shims::find so the test exercises
|
||||
// the exact shim table and calling convention the runtime installs.
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "shims.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using AcpHalCreateFn = HRESULT(__stdcall*)(void**);
|
||||
using AcpHalAllocShapeCtxFn = HRESULT(__stdcall*)(void*);
|
||||
using AcpHalReleaseShapeCtxFn = HRESULT(__stdcall*)(void*);
|
||||
using ApuCreateHeapFn = HRESULT(__stdcall*)(uint32_t, uint32_t);
|
||||
// Measured: ApuAlloc(void** va, uint32_t* apu, uint32_t size, uint32_t align, uint32_t flags)
|
||||
using ApuAllocFn = HRESULT(__stdcall*)(void**, uint32_t*, uint32_t, uint32_t, uint32_t);
|
||||
using ApuFreeFn = void(__stdcall*)(void*);
|
||||
// Measured: ApuIsVirtualAddressValid(void* va, uint32_t align)
|
||||
using ApuIsValidFn = BOOL(__stdcall*)(void*, uint32_t);
|
||||
using ApuMapApuAddrFn = void*(__stdcall*)(uint32_t);
|
||||
using ApuMapVirtualAddrFn = uint32_t(__stdcall*)(void*);
|
||||
|
||||
int g_checks = 0;
|
||||
int g_failures = 0;
|
||||
|
||||
void check(bool condition, const char* what) {
|
||||
++g_checks;
|
||||
std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
|
||||
if (!condition) ++g_failures;
|
||||
}
|
||||
|
||||
// Test mirror of SHAPE_CONTEXTS matching devdocs.xbox.com GDK spec
|
||||
struct APU_ADDRESS {
|
||||
uint64_t value;
|
||||
};
|
||||
|
||||
struct TestShapeContexts {
|
||||
uint32_t numSrcContexts;
|
||||
uint32_t numEqCompContexts;
|
||||
uint32_t numFiltVolContexts;
|
||||
uint32_t numDmaContexts;
|
||||
uint32_t numXmaContexts;
|
||||
uint32_t numPcmContexts;
|
||||
void* srcContextArray;
|
||||
void* eqCompContextArray;
|
||||
void* filtVolContextArray;
|
||||
void* dmaContextArray;
|
||||
void* xmaContextArray;
|
||||
void* pcmContextArray;
|
||||
APU_ADDRESS apuSrcContextArray;
|
||||
APU_ADDRESS apuEqCompContextArray;
|
||||
APU_ADDRESS apuFiltVolContextArray;
|
||||
APU_ADDRESS apuDmaContextArray;
|
||||
APU_ADDRESS apuXmaContextArray;
|
||||
APU_ADDRESS apuPcmContextArray;
|
||||
};
|
||||
|
||||
static_assert(sizeof(TestShapeContexts) == 120, "TestShapeContexts is 120 bytes");
|
||||
static_assert(offsetof(TestShapeContexts, xmaContextArray) == 0x38,
|
||||
"xmaContextArray is at offset +0x38");
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
std::printf("acphal_test: registered acphal.dll shim verification\n");
|
||||
|
||||
const auto create = reinterpret_cast<AcpHalCreateFn>(
|
||||
rt::shims::find("acphal.dll", "AcpHalCreate"));
|
||||
const auto alloc_ctx = reinterpret_cast<AcpHalAllocShapeCtxFn>(
|
||||
rt::shims::find("acphal.dll", "AcpHalAllocateShapeContexts"));
|
||||
const auto release_ctx = reinterpret_cast<AcpHalReleaseShapeCtxFn>(
|
||||
rt::shims::find("acphal.dll", "AcpHalReleaseShapeContexts"));
|
||||
const auto create_heap = reinterpret_cast<ApuCreateHeapFn>(
|
||||
rt::shims::find("acphal.dll", "ApuCreateHeap"));
|
||||
const auto alloc = reinterpret_cast<ApuAllocFn>(
|
||||
rt::shims::find("acphal.dll", "ApuAlloc"));
|
||||
const auto free_mem = reinterpret_cast<ApuFreeFn>(
|
||||
rt::shims::find("acphal.dll", "ApuFree"));
|
||||
const auto is_valid = reinterpret_cast<ApuIsValidFn>(
|
||||
rt::shims::find("acphal.dll", "ApuIsVirtualAddressValid"));
|
||||
const auto map_apu = reinterpret_cast<ApuMapApuAddrFn>(
|
||||
rt::shims::find("acphal.dll", "ApuMapApuAddress"));
|
||||
const auto map_virtual = reinterpret_cast<ApuMapVirtualAddrFn>(
|
||||
rt::shims::find("acphal.dll", "ApuMapVirtualAddress"));
|
||||
|
||||
check(create != nullptr, "AcpHalCreate is registered in registry");
|
||||
check(alloc_ctx != nullptr, "AcpHalAllocateShapeContexts is registered in registry");
|
||||
check(release_ctx != nullptr, "AcpHalReleaseShapeContexts is registered in registry");
|
||||
check(create_heap != nullptr, "ApuCreateHeap is registered in registry");
|
||||
check(alloc != nullptr, "ApuAlloc is registered in registry");
|
||||
check(free_mem != nullptr, "ApuFree is registered in registry");
|
||||
check(is_valid != nullptr, "ApuIsVirtualAddressValid is registered in registry");
|
||||
check(map_apu != nullptr, "ApuMapApuAddress is registered in registry");
|
||||
check(map_virtual != nullptr, "ApuMapVirtualAddress is registered in registry");
|
||||
check(rt::shims::owns_dll("acphal.dll"), "the registry owns acphal.dll");
|
||||
|
||||
if (!create || !alloc_ctx || !release_ctx || !create_heap || !alloc ||
|
||||
!free_mem || !is_valid || !map_apu || !map_virtual) {
|
||||
std::printf("Fatal: Missing export bindings for acphal.dll\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 1. AcpHalCreate
|
||||
void* acp = nullptr;
|
||||
check(create(&acp) == S_OK && acp != nullptr,
|
||||
"AcpHalCreate returns S_OK with a non-null object");
|
||||
check(create(nullptr) == E_POINTER,
|
||||
"AcpHalCreate(nullptr) returns E_POINTER");
|
||||
|
||||
if (acp) {
|
||||
using Slot0Fn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t);
|
||||
using Slot1Fn = HRESULT(__stdcall*)(void*);
|
||||
using Slot2Fn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t, uint32_t, const void*, uint32_t);
|
||||
using Slot3Fn = uint32_t(__stdcall*)(void*);
|
||||
using Slot5Fn = HRESULT(__stdcall*)(void*);
|
||||
|
||||
auto vtable = *static_cast<void* const* const*>(acp);
|
||||
check(vtable[0] != nullptr, "IAcpHal vtable slot 0 (connect) is present");
|
||||
check(vtable[1] != nullptr, "IAcpHal vtable slot 1 (disconnect) is present");
|
||||
check(vtable[2] != nullptr, "IAcpHal vtable slot 2 (submit_command) is present");
|
||||
check(vtable[3] != nullptr, "IAcpHal vtable slot 3 (pop_message) is present");
|
||||
check(vtable[4] != nullptr, "IAcpHal vtable slot 4 (get_num_messages) is present");
|
||||
check(vtable[5] != nullptr, "IAcpHal vtable slot 5 (release) is present");
|
||||
|
||||
// Slot 0: Connect (called at boot with (0x1000, 0x1000))
|
||||
check(reinterpret_cast<Slot0Fn>(vtable[0])(acp, 0x1000, 0x1000) == S_OK,
|
||||
"IAcpHal slot 0 (connect) returns S_OK");
|
||||
|
||||
// Slot 2: SubmitCommand. Critical boot requirement: The title calls
|
||||
// slot 2 as (2, 0xf0f0f0f0, -1, config, 0) and treats failure as fatal int3.
|
||||
check(reinterpret_cast<Slot2Fn>(vtable[2])(acp, 2, 0xf0f0f0f0, 0xffffffffu, nullptr, 0) == S_OK,
|
||||
"IAcpHal slot 2 (submit_command) returns S_OK (vital boot unblocker)");
|
||||
|
||||
// Slots 3 and 4: Message polling returns 0
|
||||
check(reinterpret_cast<Slot3Fn>(vtable[3])(acp) == 0, "IAcpHal slot 3 returns 0");
|
||||
check(reinterpret_cast<Slot3Fn>(vtable[4])(acp) == 0, "IAcpHal slot 4 returns 0");
|
||||
|
||||
// Slot 1: Disconnect returns S_OK
|
||||
check(reinterpret_cast<Slot1Fn>(vtable[1])(acp) == S_OK, "IAcpHal slot 1 returns S_OK");
|
||||
|
||||
// Slot 5: Release returns S_OK
|
||||
check(reinterpret_cast<Slot5Fn>(vtable[5])(acp) == S_OK, "IAcpHal slot 5 returns S_OK");
|
||||
}
|
||||
|
||||
// 2. ApuCreateHeap: title passes (cachedSize, nonCachedSize=0x226e00)
|
||||
check(create_heap(4 * 1024 * 1024, 0x226e00) == S_OK,
|
||||
"ApuCreateHeap(4 MiB, 0x226e00) succeeds");
|
||||
check(create_heap(4 * 1024 * 1024, 0x226e00) != S_OK,
|
||||
"ApuCreateHeap second call fails (heap already created)");
|
||||
|
||||
// 3. ApuAlloc argument validation
|
||||
void* dummy = nullptr;
|
||||
check(alloc(nullptr, nullptr, 1024, 0x800, 0) == E_POINTER,
|
||||
"ApuAlloc(nullptr, ...) returns E_POINTER");
|
||||
check(alloc(&dummy, nullptr, 0, 0x800, 0) == E_INVALIDARG,
|
||||
"ApuAlloc with size=0 returns E_INVALIDARG");
|
||||
check(alloc(&dummy, nullptr, 1024, 3, 0) == E_INVALIDARG,
|
||||
"ApuAlloc with unaligned alignment (3) returns E_INVALIDARG");
|
||||
|
||||
// 4. ApuAlloc real boot sequence:
|
||||
// First call: flags=2, size=0x222e00, align=0x800 (noncached heap)
|
||||
void* va_nc = nullptr;
|
||||
uint32_t apu_nc = 0;
|
||||
check(alloc(&va_nc, &apu_nc, 0x222e00, 0x800, 2) == S_OK && va_nc != nullptr,
|
||||
"ApuAlloc flags=2 at title's non-cached size 0x222e00 succeeds");
|
||||
check(apu_nc != 0, "ApuAlloc provides non-zero physical APU address");
|
||||
|
||||
// Second call: flags=1, size=4096, align=0x800 (cached heap)
|
||||
void* va_c = nullptr;
|
||||
uint32_t apu_c = 0;
|
||||
check(alloc(&va_c, &apu_c, 4096, 0x800, 1) == S_OK && va_c != nullptr,
|
||||
"ApuAlloc flags=1 succeeds on cached heap");
|
||||
check(apu_c != 0, "ApuAlloc flags=1 provides non-zero APU address");
|
||||
|
||||
// Address verification & disjoint ranges
|
||||
check(va_nc != va_c, "Cached and non-cached allocations return different virtual pointers");
|
||||
check(apu_nc != apu_c, "Cached and non-cached APU addresses are distinct");
|
||||
check(is_valid(va_nc, 0x800) == TRUE, "Non-cached allocation is in a valid heap");
|
||||
check(is_valid(va_c, 0x800) == TRUE, "Cached allocation is in a valid heap");
|
||||
check(is_valid(nullptr, 0x800) == FALSE, "ApuIsVirtualAddressValid(nullptr) returns FALSE");
|
||||
check(is_valid(reinterpret_cast<void*>(0x1234), 0x800) == FALSE,
|
||||
"ApuIsVirtualAddressValid(bogus) returns FALSE");
|
||||
|
||||
// Round-trip mapping
|
||||
check(map_virtual(va_nc) == apu_nc, "ApuMapVirtualAddress matches apu_nc");
|
||||
check(map_virtual(va_c) == apu_c, "ApuMapVirtualAddress matches apu_c");
|
||||
check(map_apu(apu_nc) == va_nc, "ApuMapApuAddress round-trips non-cached address");
|
||||
check(map_apu(apu_c) == va_c, "ApuMapApuAddress round-trips cached address");
|
||||
check(map_apu(0) == nullptr, "ApuMapApuAddress(0) returns nullptr");
|
||||
check(map_virtual(nullptr) == 0, "ApuMapVirtualAddress(nullptr) returns 0");
|
||||
|
||||
// 5. Genuine State Tracking: Free & Memory Reuse
|
||||
void* reusable_block1 = nullptr;
|
||||
check(alloc(&reusable_block1, nullptr, 0x1000, 0x800, 1) == S_OK && reusable_block1 != nullptr,
|
||||
"Allocate 4 KiB reusable block");
|
||||
free_mem(reusable_block1);
|
||||
check(true, "ApuFree on valid address succeeds without error");
|
||||
free_mem(nullptr);
|
||||
check(true, "ApuFree(nullptr) is a safe no-op");
|
||||
|
||||
// Reallocate in the freed slot:
|
||||
void* reusable_block2 = nullptr;
|
||||
check(alloc(&reusable_block2, nullptr, 0x1000, 0x800, 1) == S_OK && reusable_block2 != nullptr,
|
||||
"Re-allocate in cached heap succeeds (memory successfully reused)");
|
||||
check(reusable_block2 == reusable_block1,
|
||||
"Free block was genuinely reused at the same offset by the allocator");
|
||||
free_mem(reusable_block2);
|
||||
|
||||
// 6. AcpHalAllocateShapeContexts & AcpHalReleaseShapeContexts
|
||||
check(alloc_ctx(nullptr) == E_POINTER,
|
||||
"AcpHalAllocateShapeContexts(nullptr) returns E_POINTER");
|
||||
|
||||
// Real executable call: 0 SRC, 350 XMA (+0x38), 0 PCM
|
||||
TestShapeContexts ctx{};
|
||||
ctx.numXmaContexts = 350;
|
||||
check(alloc_ctx(&ctx) == S_OK, "AcpHalAllocateShapeContexts succeeds");
|
||||
check(ctx.xmaContextArray != nullptr,
|
||||
"xmaContextArray (+0x38) is non-null after allocation");
|
||||
check(ctx.srcContextArray == nullptr, "srcContextArray is null when count is 0");
|
||||
|
||||
// Verify zeroed memory across all 350 * 64 bytes
|
||||
const uint8_t* xma_bytes = static_cast<const uint8_t*>(ctx.xmaContextArray);
|
||||
bool all_zero = true;
|
||||
for (size_t i = 0; i < 350 * 64; ++i) {
|
||||
if (xma_bytes[i] != 0) {
|
||||
all_zero = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
check(all_zero, "SHAPE context buffer is zero-initialized for title's stride");
|
||||
|
||||
// Test multi-context allocation
|
||||
TestShapeContexts ctx_all{};
|
||||
ctx_all.numSrcContexts = 10;
|
||||
ctx_all.numEqCompContexts = 5;
|
||||
ctx_all.numFiltVolContexts = 8;
|
||||
ctx_all.numDmaContexts = 4;
|
||||
ctx_all.numXmaContexts = 12;
|
||||
ctx_all.numPcmContexts = 6;
|
||||
check(alloc_ctx(&ctx_all) == S_OK, "AcpHalAllocateShapeContexts with all 6 types succeeds");
|
||||
check(ctx_all.srcContextArray != nullptr, "srcContextArray is allocated");
|
||||
check(ctx_all.eqCompContextArray != nullptr, "eqCompContextArray is allocated");
|
||||
check(ctx_all.filtVolContextArray != nullptr, "filtVolContextArray is allocated");
|
||||
check(ctx_all.dmaContextArray != nullptr, "dmaContextArray is allocated");
|
||||
check(ctx_all.xmaContextArray != nullptr, "xmaContextArray is allocated");
|
||||
check(ctx_all.pcmContextArray != nullptr, "pcmContextArray is allocated");
|
||||
|
||||
// Release shape contexts
|
||||
check(release_ctx(&ctx_all) == S_OK, "AcpHalReleaseShapeContexts succeeds");
|
||||
check(ctx_all.xmaContextArray == nullptr, "xmaContextArray is reset to null");
|
||||
check(release_ctx(nullptr) == S_OK, "AcpHalReleaseShapeContexts(nullptr) succeeds");
|
||||
|
||||
std::printf("acphal_test complete: %d checks, %d failures\n", g_checks, g_failures);
|
||||
return g_failures ? 1 : 0;
|
||||
}
|
||||
@ -232,7 +232,9 @@ void test_classification() {
|
||||
check(cls("dstorage_xs.dll", "DStorageGetFactory") == ReturnClass::HResult,
|
||||
"the console dstorage name is hresult");
|
||||
check(cls("xg_xs.dll", "XGCreateTexture2DComputer") == ReturnClass::Other, "unmeasured xg_xs import is other");
|
||||
check(cls("acphal.dll", "AcpHalCreate") == ReturnClass::Other, "unmeasured acphal import is other");
|
||||
check(cls("acphal.dll", "AcpHalCreate") == ReturnClass::HResult, "AcpHalCreate is hresult");
|
||||
check(cls("acphal.dll", "ApuFree") == ReturnClass::Other, "ApuFree returns void");
|
||||
check(cls("acphal.dll", "ApuMapVirtualAddress") == ReturnClass::Other, "ApuMapVirtualAddress returns uint32_t");
|
||||
check(cls("xgameruntime.dll", "XGameRuntimeInitialize") == ReturnClass::HResult, "XGameRuntimeInitialize");
|
||||
check(cls("xgameruntime.dll", "XUserAddAsync") == ReturnClass::HResult, "XUser* is hresult");
|
||||
check(cls("xgameruntime.dll", "XTaskQueueCreate") == ReturnClass::HResult, "XTaskQueueCreate is hresult");
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
# Builds runtime\build\gta6rt.exe with MSVC at BelowNormal priority, on half the cores by default.
|
||||
# Builds runtime\build\gta6rt.exe with MSVC at BelowNormal priority, on half the cores by default.
|
||||
# Uses VS's CMake + Ninja when the "C++ CMake tools" component is installed (or native Windows cmake + ninja on PATH),
|
||||
# otherwise calls cl.exe directly.
|
||||
# The GameInput controller shim (SDL3, docs/gameinput.md) is built only on the CMake path: with an installed SDL3, or
|
||||
@ -81,7 +81,7 @@ $build || exit /b 1
|
||||
Write-Warning "CMake and Ninja weren't found (neither VS C++ CMake tools nor native PATH tools): building with cl.exe directly, without the GameInput shim (SDL3). Install CMake + Ninja or VS's CMake tools component to get it."
|
||||
$xtaskqueueDir = Join-Path $root "third_party\xtaskqueue"
|
||||
$xtaskqueueFiles = "Source\Task\TaskQueue.cpp", "Source\Task\AsyncLib.cpp", "Source\Task\ThreadPool_win32.cpp", "Source\Task\WaitTimer_win32.cpp" | ForEach-Object { "`"$xtaskqueueDir\$_`"" }
|
||||
$files = "src\main.cpp", "src\pe_loader.cpp", "shims\registry.cpp", "shims\file_system.cpp", "shims\xmem.cpp", "shims\d3d12_xs.cpp", "shims\d3d12_xs_device.cpp", "shims\d3d12_xs_rootsig.cpp", "shims\xgameruntime.cpp", "shims\xgameruntime_user.cpp", "shims\xgameruntime_gamesave.cpp", "shims\xgameruntime_store.cpp", "shims\xgameruntime_offline.cpp", "shims\winhttp_offline.cpp", "shims\library_loader.cpp", "shims\process.cpp" | ForEach-Object { "`"$src\$_`"" }
|
||||
$files = "src\main.cpp", "src\pe_loader.cpp", "shims\registry.cpp", "shims\file_system.cpp", "shims\xmem.cpp", "shims\xg_xs.cpp", "shims\acphal.cpp", "shims\d3d12_xs.cpp", "shims\d3d12_xs_device.cpp", "shims\d3d12_xs_rootsig.cpp", "shims\xgameruntime.cpp", "shims\xgameruntime_user.cpp", "shims\xgameruntime_gamesave.cpp", "shims\xgameruntime_store.cpp", "shims\xgameruntime_offline.cpp", "shims\winhttp_offline.cpp", "shims\library_loader.cpp", "shims\process.cpp" | ForEach-Object { "`"$src\$_`"" }
|
||||
$includes = "/I `"$src\src`" /I `"$src\shims`" /I `"$src\shims\xtaskqueue`" /I `"$xtaskqueueDir\Include`" /I `"$xtaskqueueDir\Source\Task`" /I `"$xtaskqueueDir\Source\Common`""
|
||||
$opt = if ($Debug) { "/Od /MDd" } else { "/O2 /MD" }
|
||||
$body = @"
|
||||
|
||||
107
tests/test_acphal_abi.py
Normal file
107
tests/test_acphal_abi.py
Normal file
@ -0,0 +1,107 @@
|
||||
"""Synthetic tests for acphal.dll / APU heap management & SHAPE contexts (no game data)."""
|
||||
import os
|
||||
import re
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
|
||||
class TestAcpHalAbi(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.acphal_cpp = os.path.join(ROOT, "runtime", "shims", "acphal.cpp")
|
||||
self.acphal_test = os.path.join(ROOT, "runtime", "test", "acphal_test.cpp")
|
||||
self.registry_cpp = os.path.join(ROOT, "runtime", "shims", "registry.cpp")
|
||||
self.cmakelists = os.path.join(ROOT, "runtime", "CMakeLists.txt")
|
||||
self.build_ps1 = os.path.join(ROOT, "scripts", "build_runtime.ps1")
|
||||
|
||||
def test_all_nine_acphal_exports_defined(self):
|
||||
"""acphal.dll must export all 9 functions imported by the retail binary (PR #6885, docs/retail_boot_frame_loop.md)."""
|
||||
with open(self.acphal_cpp, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
expected_exports = [
|
||||
"AcpHalAllocateShapeContexts",
|
||||
"AcpHalCreate",
|
||||
"AcpHalReleaseShapeContexts",
|
||||
"ApuAlloc",
|
||||
"ApuCreateHeap",
|
||||
"ApuFree",
|
||||
"ApuIsVirtualAddressValid",
|
||||
"ApuMapApuAddress",
|
||||
"ApuMapVirtualAddress",
|
||||
]
|
||||
for exp in expected_exports:
|
||||
self.assertIn(f'"{exp}"', content, f"acphal.dll SHIM_TABLE must register export {exp}")
|
||||
|
||||
def test_shape_contexts_layout_matches_gdk_and_retail_offset(self):
|
||||
"""SHAPE_CONTEXTS layout: 6 dwords, then 6 pointers, with xmaContextArray at +0x38 (retail measurement)."""
|
||||
# SHAPE_CONTEXTS:
|
||||
# uint32_t numSrcContexts; // offset 0
|
||||
# uint32_t numEqCompContexts; // offset 4
|
||||
# uint32_t numFiltVolContexts; // offset 8
|
||||
# uint32_t numDmaContexts; // offset 12
|
||||
# uint32_t numXmaContexts; // offset 16
|
||||
# uint32_t numPcmContexts; // offset 20
|
||||
# void* srcContextArray; // offset 24 (0x18)
|
||||
# void* eqCompContextArray; // offset 32 (0x20)
|
||||
# void* filtVolContextArray; // offset 40 (0x28)
|
||||
# void* dmaContextArray; // offset 48 (0x30)
|
||||
# void* xmaContextArray; // offset 56 (0x38) - 5th pointer, checked at +0x38!
|
||||
# void* pcmContextArray; // offset 64 (0x40)
|
||||
# APU_ADDRESS apuSrcContextArray; // offset 72 (0x48)
|
||||
# APU_ADDRESS apuEqComp... // offset 80 (0x50)
|
||||
# APU_ADDRESS apuFiltVol... // offset 88 (0x58)
|
||||
# APU_ADDRESS apuDma... // offset 96 (0x60)
|
||||
# APU_ADDRESS apuXma... // offset 104 (0x68)
|
||||
# APU_ADDRESS apuPcm... // offset 112 (0x70)
|
||||
# Total size: 120 bytes (0x78)
|
||||
layout_format = "<6I6Q6Q"
|
||||
self.assertEqual(struct.calcsize(layout_format), 120)
|
||||
|
||||
# Offset of 5th pointer (xmaContextArray): 6 * 4 + 4 * 8 = 24 + 32 = 56 = 0x38
|
||||
xma_ptr_offset = 6 * 4 + 4 * 8
|
||||
self.assertEqual(xma_ptr_offset, 0x38)
|
||||
|
||||
# Verify static_asserts in acphal.cpp
|
||||
with open(self.acphal_cpp, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
self.assertIn("static_assert(sizeof(SHAPE_CONTEXTS) == 120", content)
|
||||
self.assertIn("static_assert(offsetof(SHAPE_CONTEXTS, xmaContextArray) == 0x38", content)
|
||||
self.assertIn("static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64", content)
|
||||
self.assertIn("static_assert(sizeof(SHAPE_SRC_CONTEXT) == 32", content)
|
||||
self.assertIn("static_assert(sizeof(SHAPE_EQCOMP_CONTEXT) == 352", content)
|
||||
self.assertIn("static_assert(sizeof(SHAPE_FILTVOL_CONTEXT) == 32", content)
|
||||
self.assertIn("static_assert(sizeof(SHAPE_DMA_CONTEXT) == 16", content)
|
||||
self.assertIn("static_assert(sizeof(SHAPE_PCM_CONTEXT) == 32", content)
|
||||
|
||||
def test_registry_classifies_acphal_hresult(self):
|
||||
"""registry.cpp must classify acphal.dll HRESULT-returning functions."""
|
||||
with open(self.registry_cpp, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
self.assertIn('dll == "acphal.dll"', content)
|
||||
self.assertIn("AcpHalAllocateShapeContexts", content)
|
||||
self.assertIn("AcpHalCreate", content)
|
||||
self.assertIn("ApuAlloc", content)
|
||||
self.assertIn("ApuCreateHeap", content)
|
||||
|
||||
def test_cmakelists_and_fallback_register_acphal(self):
|
||||
"""CMakeLists.txt and build_runtime.ps1 must both register acphal.cpp in gta6rt."""
|
||||
with open(self.cmakelists, "r", encoding="utf-8") as f:
|
||||
cmake = f.read()
|
||||
self.assertIn("shims/acphal.cpp", cmake)
|
||||
self.assertIn("add_executable(acphal_test", cmake)
|
||||
self.assertIn("add_test(NAME acphal COMMAND acphal_test)", cmake)
|
||||
|
||||
with open(self.build_ps1, "r", encoding="utf-8") as f:
|
||||
ps1 = f.read()
|
||||
self.assertIn("shims\\acphal.cpp", ps1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user