From 79340b2c5127b73aa7504bba27a258e27feba14b Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:21:26 +0100 Subject: [PATCH 01/17] fix runtime and translator correctness --- aurora-main | 2 +- runtime/CMakeLists.txt | 3 +- runtime/cmake/PlatformTests.cmake | 136 +++ runtime/cmake/RuntimeLibrary.cmake | 2 +- runtime/include/audio_backend.h | 1 + runtime/include/fiber_manager.h | 12 +- runtime/include/guest_abi/memory_access.h | 19 +- runtime/include/guest_callback_state.h | 6 + runtime/include/host_file.h | 187 ++++ runtime/include/isa/ppc_isa_cr.h | 2 +- runtime/include/runtime_config.h | 54 +- runtime/include/u8_archive.h | 70 ++ runtime/src/audio_backend.cpp | 20 +- runtime/src/fiber_manager.cpp | 207 ++++- runtime/src/guest_flat_memory.cpp | 11 +- runtime/src/hle/gx/gx_fifo.cpp | 8 +- runtime/src/hle/input/pad.cpp | 6 +- runtime/src/hle/net/network_core.cpp | 10 +- runtime/src/hle/net/network_deferred.cpp | 149 ++- runtime/src/hle/net/network_internal.h | 13 +- runtime/src/hle/net/network_socket.cpp | 95 +- runtime/src/hle/net/network_ssl.cpp | 2 +- runtime/src/hle/os/os_interrupt.cpp | 6 + runtime/src/hle/os/os_scheduler.cpp | 10 +- runtime/src/hle/storage/nand_async.cpp | 98 +- runtime/src/hle/storage/nand_callbacks.cpp | 90 ++ runtime/src/hle/storage/nand_fs.cpp | 80 +- runtime/src/hle/storage/nand_internal.h | 1 + runtime/src/hle/storage/nand_isfs.cpp | 18 +- runtime/src/main.cpp | 18 +- runtime/src/memory.cpp | 25 +- .../tests/audio_backend_ownership_tests.cpp | 108 +++ runtime/tests/fiber_manager_tests.cpp | 850 ++++++++++++++++++ runtime/tests/fiber_support/abi_bridge.h | 22 + runtime/tests/fiber_support/hle_stubs.h | 12 + runtime/tests/gx_fifo_invalidation_tests.cpp | 198 ++++ runtime/tests/host_file_tests.cpp | 193 ++++ runtime/tests/host_io/SDL3/SDL_audio.h | 26 + runtime/tests/host_io/SDL3/SDL_init.h | 7 + runtime/tests/memory_access_tests.cpp | 335 +++++++ runtime/tests/memory_slow_test_support.h | 17 + runtime/tests/network_receive_tests.cpp | 491 ++++++++++ runtime/tests/pad_trigger_support/hle_stubs.h | 4 + runtime/tests/pad_trigger_support/memory.h | 15 + runtime/tests/pad_trigger_tests.cpp | 129 +++ runtime/tests/ppc_cr_logical_tests.cpp | 73 ++ runtime/tests/psq_gqr_state_free_tests.cpp | 96 ++ runtime/tests/runtime_config_io_tests.cpp | 118 +++ runtime/tests/u8_archive_tests.cpp | 78 ++ runtime/tests/windows_seh_order_tests.cmake | 28 + .../CxxLinearCodeGenerator.EmitInstruction.cs | 4 +- .../Disassembly/PpcDecoder.Primary31.cs | 18 +- .../src/Translator.Core/Lifting/PpcLifter.cs | 3 +- .../Translation/GqrConstantPropagation.cs | 5 +- .../Translation/GqrInterproceduralAnalysis.cs | 23 +- .../FusedFloatLoadCodeGenTests.cs | 69 ++ .../Translator.Tests/GqrLoopDataflowTests.cs | 135 +++ .../PpcDecoderPrimary31Tests.cs | 2 +- .../PpcLogicalCorrectnessTests.cs | 95 ++ .../StateFreePsqGqrCodeGenTests.cs | 80 ++ 60 files changed, 4231 insertions(+), 364 deletions(-) create mode 100644 runtime/include/guest_callback_state.h create mode 100644 runtime/include/host_file.h create mode 100644 runtime/include/u8_archive.h create mode 100644 runtime/src/hle/storage/nand_callbacks.cpp create mode 100644 runtime/tests/audio_backend_ownership_tests.cpp create mode 100644 runtime/tests/fiber_manager_tests.cpp create mode 100644 runtime/tests/fiber_support/abi_bridge.h create mode 100644 runtime/tests/fiber_support/hle_stubs.h create mode 100644 runtime/tests/gx_fifo_invalidation_tests.cpp create mode 100644 runtime/tests/host_file_tests.cpp create mode 100644 runtime/tests/host_io/SDL3/SDL_audio.h create mode 100644 runtime/tests/host_io/SDL3/SDL_init.h create mode 100644 runtime/tests/memory_access_tests.cpp create mode 100644 runtime/tests/memory_slow_test_support.h create mode 100644 runtime/tests/network_receive_tests.cpp create mode 100644 runtime/tests/pad_trigger_support/hle_stubs.h create mode 100644 runtime/tests/pad_trigger_support/memory.h create mode 100644 runtime/tests/pad_trigger_tests.cpp create mode 100644 runtime/tests/ppc_cr_logical_tests.cpp create mode 100644 runtime/tests/psq_gqr_state_free_tests.cpp create mode 100644 runtime/tests/runtime_config_io_tests.cpp create mode 100644 runtime/tests/u8_archive_tests.cpp create mode 100644 runtime/tests/windows_seh_order_tests.cmake create mode 100644 translator/tests/Translator.Tests/FusedFloatLoadCodeGenTests.cs create mode 100644 translator/tests/Translator.Tests/GqrLoopDataflowTests.cs create mode 100644 translator/tests/Translator.Tests/PpcLogicalCorrectnessTests.cs create mode 100644 translator/tests/Translator.Tests/StateFreePsqGqrCodeGenTests.cs diff --git a/aurora-main b/aurora-main index 33067e34a..a222e54a4 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit 33067e34a17ca680eb961d9e0f3d48ddb22a7d7f +Subproject commit a222e54a4e53d55415de1de1e4fe9dece2705288 diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f0d4fd4d5..41e9f1390 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -54,6 +54,7 @@ endif() option(MKW_BUILD_PRODUCTS "Build translated WiiCompiled product targets" ON) option(MKW_BUILD_PSQ_TESTS "Build focused PSQ ISA tests" OFF) +option(MKW_BUILD_AURORA_TESTS "Build integrated Aurora regression tests" OFF) set(MKW_X86_CPU_PROFILE "v3" CACHE STRING "Minimum x86-64 CPU profile for translated products (v2 or v3)") set_property(CACHE MKW_X86_CPU_PROFILE PROPERTY STRINGS v2 v3) @@ -277,7 +278,7 @@ else() # Aurora components the runtime does not use. They would otherwise fetch # extra dependencies at configure time, which offline builds cannot do. - set(AURORA_ENABLE_TESTS OFF CACHE BOOL "" FORCE) + set(AURORA_ENABLE_TESTS ${MKW_BUILD_AURORA_TESTS} CACHE BOOL "" FORCE) set(AURORA_ENABLE_EXAMPLES OFF CACHE BOOL "" FORCE) set(AURORA_ENABLE_DVD OFF CACHE BOOL "" FORCE) set(AURORA_ENABLE_THP OFF CACHE BOOL "" FORCE) diff --git a/runtime/cmake/PlatformTests.cmake b/runtime/cmake/PlatformTests.cmake index 3eddcb61b..53349d8b1 100644 --- a/runtime/cmake/PlatformTests.cmake +++ b/runtime/cmake/PlatformTests.cmake @@ -23,7 +23,36 @@ if(MKW_BUILD_PSQ_TESTS) add_test(NAME mkw_psq_reserved_tests COMMAND "${CMAKE_COMMAND}" "-DPSQ_TEST_EXECUTABLE=$" -P "${MKW_TEST_RUNTIME_DIR}/tests/psq_reserved_tests.cmake") + + add_executable(mkw_psq_gqr_state_free_tests + "${MKW_TEST_RUNTIME_DIR}/tests/psq_gqr_state_free_tests.cpp" + "${MKW_TEST_RUNTIME_DIR}/src/ppc_quantized.cpp") + target_include_directories(mkw_psq_gqr_state_free_tests PRIVATE + "${MKW_TEST_RUNTIME_DIR}/tests/psq_memory" + "${MKW_TEST_RUNTIME_DIR}/include/isa" + "${MKW_TEST_RUNTIME_DIR}/include") + target_compile_features(mkw_psq_gqr_state_free_tests PRIVATE cxx_std_17) + target_compile_options(mkw_psq_gqr_state_free_tests PRIVATE + -O2 -fno-fast-math -ffp-contract=off -fno-slp-vectorize) + if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|amd64|x86_64|X86_64)$") + target_compile_options(mkw_psq_gqr_state_free_tests PRIVATE + -march=x86-64-${MKW_X86_CPU_PROFILE}) + endif() + set_target_properties(mkw_psq_gqr_state_free_tests PROPERTIES UNITY_BUILD OFF) + add_test(NAME mkw_psq_gqr_state_free_tests COMMAND mkw_psq_gqr_state_free_tests) +endif() + +add_executable(mkw_ppc_cr_logical_tests + "${MKW_TEST_RUNTIME_DIR}/tests/ppc_cr_logical_tests.cpp") +target_include_directories(mkw_ppc_cr_logical_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/include") +target_compile_features(mkw_ppc_cr_logical_tests PRIVATE cxx_std_17) +target_compile_options(mkw_ppc_cr_logical_tests PRIVATE + -fno-fast-math -ffp-contract=off) +if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|amd64|x86_64|X86_64)$") + target_compile_options(mkw_ppc_cr_logical_tests PRIVATE -march=x86-64-${MKW_X86_CPU_PROFILE}) endif() +set_target_properties(mkw_ppc_cr_logical_tests PROPERTIES UNITY_BUILD OFF) +add_test(NAME mkw_ppc_cr_logical_tests COMMAND mkw_ppc_cr_logical_tests) add_executable(mkw_platform_paths_tests "${MKW_TEST_RUNTIME_DIR}/tests/platform_paths_tests.cpp") target_link_libraries(mkw_platform_paths_tests PRIVATE mkw_platform) @@ -37,6 +66,24 @@ target_include_directories(mkw_runtime_config_tests PRIVATE target_compile_features(mkw_runtime_config_tests PRIVATE cxx_std_20) add_test(NAME mkw_runtime_config_tests COMMAND mkw_runtime_config_tests) +if(MKW_PLATFORM_MACOS) + set(MKW_MEMORY_TEST_MAPPING_SOURCE "${MKW_TEST_RUNTIME_DIR}/src/guest_flat_memory_macos.cpp") +else() + set(MKW_MEMORY_TEST_MAPPING_SOURCE "${MKW_TEST_RUNTIME_DIR}/src/guest_flat_memory.cpp") +endif() +add_executable(mkw_memory_access_tests + "${MKW_TEST_RUNTIME_DIR}/tests/memory_access_tests.cpp" + "${MKW_TEST_RUNTIME_DIR}/src/memory.cpp" + "${MKW_MEMORY_TEST_MAPPING_SOURCE}") +target_include_directories(mkw_memory_access_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/include") +target_compile_features(mkw_memory_access_tests PRIVATE cxx_std_20) +target_compile_options(mkw_memory_access_tests PRIVATE -fno-fast-math -ffp-contract=off) +if(MKW_PLATFORM_WINDOWS) + target_link_libraries(mkw_memory_access_tests PRIVATE dbghelp) +endif() +add_test(NAME mkw_memory_access_tests COMMAND mkw_memory_access_tests) +set_tests_properties(mkw_memory_access_tests PROPERTIES TIMEOUT 60) + add_executable(mkw_nand_save_tests "${MKW_TEST_RUNTIME_DIR}/tests/nand_save_tests.cpp") target_include_directories(mkw_nand_save_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/include") target_compile_features(mkw_nand_save_tests PRIVATE cxx_std_17) @@ -49,6 +96,95 @@ add_test(NAME mkw_nand_guest_path_tests COMMAND mkw_nand_guest_path_tests) add_executable(mkw_nand_settings_tests "${MKW_TEST_RUNTIME_DIR}/tests/nand_settings_tests.cpp") find_package(Threads REQUIRED) +target_link_libraries(mkw_memory_access_tests PRIVATE Threads::Threads) +target_link_libraries(mkw_runtime_config_tests PRIVATE Threads::Threads) + +add_executable(mkw_host_file_tests "${MKW_TEST_RUNTIME_DIR}/tests/host_file_tests.cpp") +target_include_directories(mkw_host_file_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/include") +target_compile_features(mkw_host_file_tests PRIVATE cxx_std_17) +target_link_libraries(mkw_host_file_tests PRIVATE Threads::Threads) +add_test(NAME mkw_host_file_tests COMMAND mkw_host_file_tests) + +add_executable(mkw_u8_archive_tests "${MKW_TEST_RUNTIME_DIR}/tests/u8_archive_tests.cpp") +target_include_directories(mkw_u8_archive_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/include") +target_compile_features(mkw_u8_archive_tests PRIVATE cxx_std_17) +add_test(NAME mkw_u8_archive_tests COMMAND mkw_u8_archive_tests) + +add_executable(mkw_runtime_config_io_tests "${MKW_TEST_RUNTIME_DIR}/tests/runtime_config_io_tests.cpp") +target_include_directories(mkw_runtime_config_io_tests PRIVATE + "${MKW_TEST_RUNTIME_DIR}/include" "${MKW_TEST_RUNTIME_DIR}/third_party/toml11") +target_compile_features(mkw_runtime_config_io_tests PRIVATE cxx_std_20) +target_link_libraries(mkw_runtime_config_io_tests PRIVATE Threads::Threads) +add_test(NAME mkw_runtime_config_io_tests COMMAND mkw_runtime_config_io_tests) +if(MKW_PLATFORM_WINDOWS) + target_link_libraries(mkw_runtime_config_tests PRIVATE advapi32) + target_link_libraries(mkw_host_file_tests PRIVATE advapi32) + target_link_libraries(mkw_runtime_config_io_tests PRIVATE advapi32) +endif() + +if(TARGET SDL3::SDL3) + add_executable(mkw_pad_trigger_tests "${MKW_TEST_RUNTIME_DIR}/tests/pad_trigger_tests.cpp") + target_include_directories(mkw_pad_trigger_tests PRIVATE + "${MKW_TEST_RUNTIME_DIR}/tests/pad_trigger_support" + "${MKW_TEST_RUNTIME_DIR}/include" "${MKW_AURORA_DIR}/include") + target_compile_definitions(mkw_pad_trigger_tests PRIVATE TARGET_PC=1 AURORA) + target_compile_features(mkw_pad_trigger_tests PRIVATE cxx_std_20) + target_link_libraries(mkw_pad_trigger_tests PRIVATE SDL3::SDL3) + set_target_properties(mkw_pad_trigger_tests PROPERTIES UNITY_BUILD OFF) + add_test(NAME mkw_pad_trigger_tests COMMAND mkw_pad_trigger_tests) +endif() + +if(TARGET gx_fifo_tests) + target_sources(gx_fifo_tests PRIVATE + "${MKW_TEST_RUNTIME_DIR}/tests/gx_fifo_invalidation_tests.cpp" + "${MKW_TEST_RUNTIME_DIR}/src/hle/gx/gx_fifo.cpp") + target_include_directories(gx_fifo_tests PRIVATE + "${MKW_AURORA_DIR}/tests" "${MKW_TEST_RUNTIME_DIR}/include" + "${MKW_TEST_RUNTIME_DIR}/third_party/toml11") + target_compile_features(gx_fifo_tests PRIVATE cxx_std_20) + set_target_properties(gx_fifo_tests PROPERTIES UNITY_BUILD OFF) + if(MKW_PLATFORM_WINDOWS) + target_link_libraries(gx_fifo_tests PRIVATE advapi32) + endif() +endif() + +add_executable(mkw_fiber_manager_tests + "${MKW_TEST_RUNTIME_DIR}/tests/fiber_manager_tests.cpp" + "${MKW_TEST_RUNTIME_DIR}/src/fiber_manager.cpp" + "${MKW_TEST_RUNTIME_DIR}/src/hle/os/os_scheduler.cpp" + "${MKW_TEST_RUNTIME_DIR}/src/hle/storage/nand_callbacks.cpp" + "${MKW_TEST_RUNTIME_DIR}/src/hle/os/os_thread.cpp") +target_include_directories(mkw_fiber_manager_tests PRIVATE + "${MKW_TEST_RUNTIME_DIR}/tests/fiber_support" + "${MKW_TEST_RUNTIME_DIR}/include" "${MKW_TEST_RUNTIME_DIR}/src" + "${MKW_TEST_RUNTIME_DIR}/third_party" + "${MKW_TEST_RUNTIME_DIR}/third_party/toml11") +target_compile_features(mkw_fiber_manager_tests PRIVATE cxx_std_20) +target_link_libraries(mkw_fiber_manager_tests PRIVATE Threads::Threads) +if(MKW_PLATFORM_MACOS_ARM64) + enable_language(ASM) + target_sources(mkw_fiber_manager_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/src/platform/macos/co_switch.S") +elseif(TARGET mkw::libco) + target_link_libraries(mkw_fiber_manager_tests PRIVATE mkw::libco) +endif() +set_target_properties(mkw_fiber_manager_tests PROPERTIES UNITY_BUILD OFF) +add_test(NAME mkw_fiber_manager_tests COMMAND mkw_fiber_manager_tests) +add_executable(mkw_audio_backend_ownership_tests "${MKW_TEST_RUNTIME_DIR}/tests/audio_backend_ownership_tests.cpp" "${MKW_TEST_RUNTIME_DIR}/src/audio_backend.cpp") +target_include_directories(mkw_audio_backend_ownership_tests BEFORE PRIVATE "${MKW_TEST_RUNTIME_DIR}/tests/host_io" "${MKW_TEST_RUNTIME_DIR}/include" "${MKW_TEST_RUNTIME_DIR}/src") +target_compile_features(mkw_audio_backend_ownership_tests PRIVATE cxx_std_20) +add_test(NAME mkw_audio_backend_ownership_tests COMMAND mkw_audio_backend_ownership_tests) +add_executable(mkw_network_receive_tests "${MKW_TEST_RUNTIME_DIR}/tests/network_receive_tests.cpp") +target_include_directories(mkw_network_receive_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/include" "${MKW_TEST_RUNTIME_DIR}/src") +target_compile_features(mkw_network_receive_tests PRIVATE cxx_std_20) +target_link_libraries(mkw_network_receive_tests PRIVATE Threads::Threads) +if(MKW_PLATFORM_WINDOWS) + target_link_libraries(mkw_network_receive_tests PRIVATE ws2_32) +endif() +set_target_properties(mkw_audio_backend_ownership_tests mkw_network_receive_tests PROPERTIES UNITY_BUILD OFF) +add_test(NAME mkw_network_receive_tests COMMAND mkw_network_receive_tests) +add_test(NAME mkw_windows_seh_order_tests COMMAND "${CMAKE_COMMAND}" "-DSEH_SOURCE=${MKW_TEST_RUNTIME_DIR}/src/main.cpp" -P "${MKW_TEST_RUNTIME_DIR}/tests/windows_seh_order_tests.cmake") +set_tests_properties(mkw_fiber_manager_tests mkw_network_receive_tests PROPERTIES TIMEOUT 60) + target_link_libraries(mkw_nand_settings_tests PRIVATE Threads::Threads) target_include_directories(mkw_nand_settings_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/include") target_compile_features(mkw_nand_settings_tests PRIVATE cxx_std_17) diff --git a/runtime/cmake/RuntimeLibrary.cmake b/runtime/cmake/RuntimeLibrary.cmake index 8d6160af0..baad898b7 100644 --- a/runtime/cmake/RuntimeLibrary.cmake +++ b/runtime/cmake/RuntimeLibrary.cmake @@ -66,7 +66,7 @@ function(mkw_runtime_link_dependencies target scope) aurora::gx aurora::pad aurora::si aurora::vi aurora::mtx) target_link_libraries(${target} ${scope} mkw_platform mkw::pugixml mkw::toml11 mkw::cryptopp mkw::mbedtls) if(MKW_PLATFORM_WINDOWS) - target_link_libraries(${target} ${scope} shell32 windowsapp bthprops) + target_link_libraries(${target} ${scope} shell32 windowsapp bthprops advapi32) elseif(MKW_PLATFORM_LINUX OR MKW_PLATFORM_MACOS_X86_64) # ${CMAKE_DL_LIBS} for music_attenuation.cpp's dlopen of libdbus-1 (MPRIS # media monitoring). Empty on platforms where dl* is already in libc/libSystem. diff --git a/runtime/include/audio_backend.h b/runtime/include/audio_backend.h index 6ff324839..1ca5ad436 100644 --- a/runtime/include/audio_backend.h +++ b/runtime/include/audio_backend.h @@ -43,6 +43,7 @@ class AudioBackend { uint32_t m_sampleRate = 0; uint32_t m_channels = 0; bool m_initialized = false; + bool m_audioSubsystemReady = false; float m_masterVolume = 1.0f; bool m_muted = false; bool m_silenced = false; diff --git a/runtime/include/fiber_manager.h b/runtime/include/fiber_manager.h index cad48c498..bf4420385 100644 --- a/runtime/include/fiber_manager.h +++ b/runtime/include/fiber_manager.h @@ -96,6 +96,8 @@ class GuestFiberManager { // Check if a fiber exists for a guest thread static bool HasFiber(uint32_t guestThreadAddr); static bool IsTerminated(uint32_t guestThreadAddr); + // May be captured before the default thread is registered as a fiber. + static uint64_t GetThreadGeneration(uint32_t guestThreadAddr); private: // The fiber entry point wrapper @@ -104,24 +106,20 @@ class GuestFiberManager { #else static void FiberProc(void* param); #endif - // Switch from whichever fiber is currently active straight to the scheduler fiber, without - // the SwitchToThread bookkeeping (CPU context save/restore, s_currentGuestThread). Used for - // in-fiber yields that aren't a real guest thread switch: waiting out the EGG::Thread::start - // deferral loop, and returning control on natural thread exit. + // Yield to the scheduler while preserving this host frame's registers and ambient state. static void SwitchToScheduler(); // Internal state static std::mutex s_mutex; static std::unordered_map s_fibers; + static std::unordered_map s_threadGenerations; + static uint64_t s_nextThreadGeneration; static std::vector s_fibersPendingDelete; // The scheduler's own host context. Its opaque handle is supplied by the // active HostContext backend, so one field serves every supported host. static void* s_schedulerFiber; static uint32_t s_currentGuestThread; static bool s_initialized; - // Stored CPU context pointer for fiber switches - static thread_local CpuContext* s_cpuContext; - static void PurgePendingFibers(); }; diff --git a/runtime/include/guest_abi/memory_access.h b/runtime/include/guest_abi/memory_access.h index 289f928dd..481971f32 100644 --- a/runtime/include/guest_abi/memory_access.h +++ b/runtime/include/guest_abi/memory_access.h @@ -3,6 +3,7 @@ #include "guest_abi/exec_guard.h" #include "guest_abi/flat_memory.h" #include "guest_abi/memory.h" +#include "isa/ppc_isa_float.h" #include #include @@ -83,7 +84,7 @@ MKW_MEMORY_NO_INLINE MKW_MEMORY_COLD void WriteFloat32Slow(uint32_t addr, double MKW_MEMORY_NO_INLINE MKW_MEMORY_COLD void WriteFloat64Slow(uint32_t addr, double val); template MKW_MEMORY_NO_INLINE MKW_MEMORY_COLD T ReadResolvedFallback(uint32_t addr); -MKW_MEMORY_NO_INLINE MKW_MEMORY_COLD float ReadResolvedFallbackFloat32(uint32_t addr); +MKW_MEMORY_NO_INLINE MKW_MEMORY_COLD double ReadResolvedFallbackFloat32(uint32_t addr); MKW_MEMORY_NO_INLINE MKW_MEMORY_COLD double ReadResolvedFallbackFloat64(uint32_t addr); template MKW_MEMORY_NO_INLINE MKW_MEMORY_COLD void WriteResolvedFallback(uint32_t addr, T value); @@ -150,7 +151,7 @@ MKW_MEMORY_FORCE_INLINE bool TryGetPointerFast(uint32_t address, size_t length, } const uint32_t offset = address & kPageMask; const auto& entry = g_pageTable[page]; - if (!entry.base || offset + length > entry.limit) { + if (!entry.base || offset > entry.limit || length > entry.limit - offset) { pointer = nullptr; return false; } @@ -197,7 +198,7 @@ MKW_MEMORY_FORCE_INLINE bool TryGetWritablePointerFast( // the same executable-write policy as a sparse-table hit. const uint32_t offset = address & kPageMask; const auto& entry = g_pageTable[coarsePage]; - if (!entry.base || offset + length > entry.limit) + if (!entry.base || offset > entry.limit || length > entry.limit - offset) return false; pointer = entry.base + offset; } @@ -489,10 +490,11 @@ MKW_MEMORY_FORCE_INLINE uint32_t ReadResolved32(uint8_t* r, uint32_t o, uint32_t // Live via isa/ppc_isa_quantized.h (the psq resolved tier packs two lanes into // one 64-bit access); generated code never names it directly. MKW_MEMORY_FORCE_INLINE uint64_t ReadResolved64(uint8_t* r, uint32_t o, uint32_t a) { return ReadResolved(r, o, a, ReadResolvedFallback); } -MKW_MEMORY_FORCE_INLINE float ReadResolvedFloat32(uint8_t* r, uint32_t o, uint32_t a) { +// lfs widens the stored bits without applying the host's arithmetic flush mode. +MKW_MEMORY_FORCE_INLINE double ReadResolvedFloat32(uint8_t* r, uint32_t o, uint32_t a) { uint32_t bits = 0; if (!ReadResolvedScalar(r, o, bits)) [[unlikely]] return ReadResolvedFallbackFloat32(a); - float value; std::memcpy(&value, &bits, sizeof(value)); return value; + return PpcBitCastToDoubleInline(PpcConvertToDoubleBitsInline(bits)); } MKW_MEMORY_FORCE_INLINE double ReadResolvedFloat64(uint8_t* r, uint32_t o, uint32_t a) { uint64_t bits = 0; @@ -563,11 +565,8 @@ MKW_MEMORY_FORCE_INLINE uint32_t FlatRead32(uint32_t address) { return FlatLoad(address); } -MKW_MEMORY_FORCE_INLINE float FlatReadFloat32(uint32_t address) { - const uint32_t bits = FlatRead32(address); - float value = 0.0f; - std::memcpy(&value, &bits, sizeof(value)); - return value; +MKW_MEMORY_FORCE_INLINE double FlatReadFloat32(uint32_t address) { + return PpcBitCastToDoubleInline(PpcConvertToDoubleBitsInline(FlatRead32(address))); } MKW_MEMORY_FORCE_INLINE double FlatReadFloat64(uint32_t address) { diff --git a/runtime/include/guest_callback_state.h b/runtime/include/guest_callback_state.h new file mode 100644 index 000000000..40936cb0e --- /dev/null +++ b/runtime/include/guest_callback_state.h @@ -0,0 +1,6 @@ +#pragma once + +namespace GuestCallbacks { +// Saved and restored with the executing host fiber. +inline thread_local bool nandDraining = false; +} diff --git a/runtime/include/host_file.h b/runtime/include/host_file.h new file mode 100644 index 000000000..924b23c54 --- /dev/null +++ b/runtime/include/host_file.h @@ -0,0 +1,187 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#include +#else +#include +#include +#include +#endif + +namespace RuntimeHostFile { + +inline FILE* Open(const std::filesystem::path& path, const char* mode) { +#ifdef _WIN32 + const std::string modeText(mode); + const std::wstring wideMode(modeText.begin(), modeText.end()); + return _wfopen(path.c_str(), wideMode.c_str()); +#else + return std::fopen(path.c_str(), mode); +#endif +} + +struct FileOperations { + static size_t Write(FILE* file, const char* data, size_t size) { + return std::fwrite(data, 1, size, file); + } + + static bool Flush(FILE* file) { + if (std::fflush(file) != 0) return false; +#ifdef _WIN32 + return _commit(_fileno(file)) == 0; +#else + return ::fsync(fileno(file)) == 0; +#endif + } + + static bool Close(FILE* file) { return std::fclose(file) == 0; } + + static bool Publish(const std::filesystem::path& temporary, + const std::filesystem::path& target, bool replace) { +#ifdef _WIN32 + if (replace) { + // ReplaceFile preserves the destination's ACL and other metadata. + if (ReplaceFileW(target.c_str(), temporary.c_str(), nullptr, 0, nullptr, nullptr)) return true; + if (GetLastError() != ERROR_FILE_NOT_FOUND) return false; + } + return MoveFileExW(temporary.c_str(), target.c_str(), MOVEFILE_WRITE_THROUGH) != 0; +#else + if (replace) return ::rename(temporary.c_str(), target.c_str()) == 0; + return ::link(temporary.c_str(), target.c_str()) == 0; +#endif + } +}; + +template +inline int CreateExclusive(const std::filesystem::path& path) { + FILE* file = Open(path, "wbx"); + if (!file) return errno == EEXIST ? EEXIST : EIO; + return Operations::Close(file) ? 0 : EIO; +} + +inline std::filesystem::path WriteTarget(const std::filesystem::path& path, std::error_code& ec) { + const auto status = std::filesystem::symlink_status(path, ec); + if (ec && ec != std::errc::no_such_file_or_directory) return {}; + ec.clear(); + if (std::filesystem::is_symlink(status)) return std::filesystem::canonical(path, ec); + return std::filesystem::weakly_canonical(path, ec); +} + +template +inline bool WriteAtomically(const std::filesystem::path& path, std::string_view contents, + bool replace = true) { + std::error_code ec; + const auto target = WriteTarget(path, ec); + if (ec || target.empty()) return false; + std::filesystem::create_directories(target.parent_path(), ec); + if (ec) return false; + +#ifdef _WIN32 + DWORD securitySize = 0; + std::vector security; + if (!GetFileSecurityW(target.c_str(), DACL_SECURITY_INFORMATION, nullptr, 0, &securitySize)) { + const auto error = GetLastError(); + if (error != ERROR_INSUFFICIENT_BUFFER && error != ERROR_FILE_NOT_FOUND) return false; + } + if (securitySize != 0) { + security.resize(securitySize); + if (!GetFileSecurityW(target.c_str(), DACL_SECURITY_INFORMATION, security.data(), securitySize, &securitySize)) return false; + } +#else + struct stat existing {}; + const bool existed = ::stat(target.c_str(), &existing) == 0; + if (!existed && errno != ENOENT) return false; + if (existed && !S_ISREG(existing.st_mode)) return false; +#endif + + struct TemporaryFile { + FILE* file = nullptr; + std::filesystem::path path; + ~TemporaryFile() { + if (file) std::fclose(file); + if (!path.empty()) { + std::error_code ignored; + std::filesystem::remove(path, ignored); + } + } + } temporary; + + static std::atomic sequence{0}; +#ifdef _WIN32 + const auto process = GetCurrentProcessId(); +#else + const auto process = getpid(); +#endif + const auto token = std::to_string(process) + "-" + std::to_string( + std::chrono::steady_clock::now().time_since_epoch().count()); + for (unsigned attempt = 0; attempt < 128; ++attempt) { + auto candidate = target; + candidate += ".tmp-" + token + "-" + std::to_string(sequence.fetch_add(1, std::memory_order_relaxed)); + temporary.file = Open(candidate, "wbx"); + if (temporary.file) { + temporary.path = std::move(candidate); + break; + } + if (errno != EEXIST) return false; + } + if (!temporary.file) return false; + +#ifdef _WIN32 + if (!security.empty()) { + SECURITY_DESCRIPTOR_CONTROL control = 0; + DWORD revision = 0; + if (!GetSecurityDescriptorControl(security.data(), &control, &revision)) return false; + const DWORD protection = (control & SE_DACL_PROTECTED) ? PROTECTED_DACL_SECURITY_INFORMATION : UNPROTECTED_DACL_SECURITY_INFORMATION; + if (!SetFileSecurityW(temporary.path.c_str(), DACL_SECURITY_INFORMATION | protection, security.data())) return false; + } +#else + if (existed && ::fchmod(fileno(temporary.file), existing.st_mode & 07777) != 0) return false; +#endif + size_t offset = 0; + while (offset < contents.size()) { + const size_t written = Operations::Write(temporary.file, contents.data() + offset, contents.size() - offset); + if (written == 0 || written > contents.size() - offset || std::ferror(temporary.file)) return false; + offset += written; + } +#ifndef _WIN32 + if (existed && ::fchmod(fileno(temporary.file), existing.st_mode & 07777) != 0) return false; +#endif + if (!Operations::Flush(temporary.file)) return false; + FILE* file = temporary.file; + temporary.file = nullptr; + if (!Operations::Close(file) || !Operations::Publish(temporary.path, target, replace)) return false; + +#ifndef _WIN32 + if (!replace && ::unlink(temporary.path.c_str()) != 0) return false; + temporary.path.clear(); + // Sync the directory after publishing the complete file. + const int directory = ::open(target.parent_path().c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (directory < 0) return false; + const bool synced = ::fsync(directory) == 0; + const bool closed = ::close(directory) == 0; + if (!synced || !closed) return false; +#else + temporary.path.clear(); +#endif + return true; +} + +} // namespace RuntimeHostFile diff --git a/runtime/include/isa/ppc_isa_cr.h b/runtime/include/isa/ppc_isa_cr.h index b82c59a71..d7f183075 100644 --- a/runtime/include/isa/ppc_isa_cr.h +++ b/runtime/include/isa/ppc_isa_cr.h @@ -46,7 +46,7 @@ inline uint32_t PpcCrLogicalResident( case 3: result = ~(a & b) & 1u; break; case 4: result = a & b; break; case 5: result = ~(a ^ b) & 1u; break; - case 6: result = (~a & 1u) | b; break; + case 6: result = a | (~b & 1u); break; case 7: result = a | b; break; } return PpcCrSetBitResident(cr, bt, result); diff --git a/runtime/include/runtime_config.h b/runtime/include/runtime_config.h index 939c4c40d..d88ac372e 100644 --- a/runtime/include/runtime_config.h +++ b/runtime/include/runtime_config.h @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include #include @@ -20,6 +22,7 @@ #include #include #include "platform/host_platform.h" +#include "host_file.h" #ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN @@ -267,7 +270,13 @@ inline std::filesystem::path ResolveConfigPath() { return ApplicationDataDirectory() / kConfigFileName; } +inline std::mutex& ConfigWriteMutex() { + static std::mutex mutex; + return mutex; +} + inline void EnsureConfigFile() { + std::lock_guard lock(ConfigWriteMutex()); const std::filesystem::path path = ResolveConfigPath(); std::error_code ec; std::filesystem::create_directories(path.parent_path(), ec); @@ -275,10 +284,7 @@ inline void EnsureConfigFile() { return; } - std::ofstream output(path); - if (!output) { - return; - } + std::ostringstream output; output << "# WiiCompiled user configuration\n" "# Set paths.dvd_root to an extracted Mario Kart Wii DATA directory.\n\n" "[video]\n" @@ -330,6 +336,7 @@ inline void EnsureConfigFile() { "# nand_root = \"D:\\\\WiiNand\"\n" "# retro_rewind_root = \"D:\\\\RetroRewind\\\\RetroRewind6\"\n" "# overlay_roots = [\"D:\\\\RetroRewind\"]\n"; + RuntimeHostFile::WriteAtomically(path, output.str(), false); } template @@ -608,29 +615,40 @@ inline std::optional UpdateSettingText(const std::string& text, std // Update one TOML value without discarding unrelated settings or user-specific paths. This is // used by the in-game F10 settings bar. An unparseable file is left untouched. -inline bool WriteSetting(std::string_view section, std::string_view key, std::string_view value) { - const auto path = ResolveConfigPath(); - std::ostringstream existing; - if (std::ifstream input(path, std::ios::binary); input) { - existing << input.rdbuf(); +template +inline bool WriteSettingFile(const std::filesystem::path& path, std::string_view section, + std::string_view key, std::string_view value) { + std::lock_guard lock(ConfigWriteMutex()); + std::error_code ec; + const auto target = RuntimeHostFile::WriteTarget(path, ec); + if (ec || target.empty()) return false; + std::string existing; + std::unique_ptr input(RuntimeHostFile::Open(target, "rb"), &std::fclose); + if (input) { + char buffer[4096]; + size_t count; + while ((count = std::fread(buffer, 1, sizeof(buffer), input.get())) != 0) existing.append(buffer, count); + const bool read = std::feof(input.get()) && !std::ferror(input.get()); + const bool closed = std::fclose(input.release()) == 0; + if (!read || !closed) return false; + } else if (errno != ENOENT) { + return false; } - const auto updated = UpdateSettingText(existing.str(), section, key, value); + const auto updated = UpdateSettingText(existing, section, key, value); if (!updated) { std::cerr << "[runtime-config] Not updating invalid " << PathToUtf8(path) << std::endl; return false; } - std::error_code ec; - if (path.has_parent_path()) { - std::filesystem::create_directories(path.parent_path(), ec); - } - std::ofstream output(path, std::ios::trunc); - if (!output) { + if (!RuntimeHostFile::WriteAtomically(target, *updated)) { std::cerr << "[runtime-config] Unable to write " << PathToUtf8(path) << std::endl; return false; } - output << *updated; - return static_cast(output); + return true; +} + +inline bool WriteSetting(std::string_view section, std::string_view key, std::string_view value) { + return WriteSettingFile(ResolveConfigPath(), section, key, value); } inline std::string FormatString(std::string_view value) { diff --git a/runtime/include/u8_archive.h b/runtime/include/u8_archive.h new file mode 100644 index 000000000..36cd2fdbe --- /dev/null +++ b/runtime/include/u8_archive.h @@ -0,0 +1,70 @@ +#pragma once + +#include "isa/big_endian.h" + +#include +#include +#include +#include +#include +#include + +namespace RuntimeU8 { + +inline bool Extract(const std::vector& data, std::string_view targetName, + std::vector& outData) { + constexpr uint64_t nodeSize = 12; + if (data.size() < 0x20 || BigEndian::Read32(data.data()) != 0x55AA382Du) return false; + const uint64_t rootOffset = BigEndian::Read32(data.data() + 4); + const uint64_t headerSize = BigEndian::Read32(data.data() + 8); + if (rootOffset > data.size() || nodeSize > data.size() - rootOffset || + headerSize > data.size() - rootOffset) return false; + const uint8_t* root = data.data() + rootOffset; + const uint32_t nodeCount = BigEndian::Read32(root + 8); + const uint64_t nodeTableSize = static_cast(nodeCount) * nodeSize; + const uint64_t metadataEnd = rootOffset + headerSize; + if ((BigEndian::Read32(root) >> 24) != 1 || BigEndian::Read32(root + 4) != 0 || + nodeCount == 0 || nodeTableSize >= headerSize) return false; + const uint64_t stringTableOffset = rootOffset + nodeTableSize; + + struct DirFrame { + std::string path; + uint32_t index; + uint32_t endIndex; + }; + std::vector stack; + struct Payload { uint64_t offset; uint64_t size; }; + std::optional selected; + + for (uint32_t index = 0; index < nodeCount; ++index) { + while (!stack.empty() && index >= stack.back().endIndex) stack.pop_back(); + if (index != 0 && stack.empty()) return false; + const uint8_t* node = root + static_cast(index) * nodeSize; + const uint32_t typeAndName = BigEndian::Read32(node); + const uint32_t type = typeAndName >> 24; + const uint64_t nameOffset = typeAndName & 0x00FFFFFFu; + if (type > 1 || nameOffset >= metadataEnd - stringTableOffset) return false; + const char* name = reinterpret_cast(data.data() + stringTableOffset + nameOffset); + const auto* end = static_cast(std::memchr(name, '\0', + static_cast(metadataEnd - stringTableOffset - nameOffset))); + if (!end) return false; + const std::string_view nameText(name, static_cast(end - name)); + const std::string fullPath = index == 0 ? std::string{} : stack.back().path + std::string(nameText); + const uint32_t offset = BigEndian::Read32(node + 4); + const uint32_t size = BigEndian::Read32(node + 8); + if (type == 1) { + if (size <= index || (index != 0 && + (size > stack.back().endIndex || offset != stack.back().index))) return false; + stack.push_back({index == 0 ? std::string{} : fullPath + "/", index, size}); + } else { + const uint64_t fileOffset = offset; + if (fileOffset > data.size() || size > data.size() - fileOffset) return false; + if (!selected && (fullPath == targetName || nameText == targetName)) selected = Payload{fileOffset, size}; + } + } + if (!selected) return false; + outData.assign(data.begin() + selected->offset, data.begin() + selected->offset + selected->size); + return true; +} + +} // namespace RuntimeU8 diff --git a/runtime/src/audio_backend.cpp b/runtime/src/audio_backend.cpp index fcb4fde3b..f6ae84d72 100644 --- a/runtime/src/audio_backend.cpp +++ b/runtime/src/audio_backend.cpp @@ -43,7 +43,7 @@ void AudioBackend::SetSilenced(bool silenced) { } bool AudioBackend::EnsureInitializedLocked(uint32_t sampleRate, uint32_t channels) { - if (m_initialized && m_sampleRate == sampleRate && m_channels == channels) { + if (m_stream && m_initialized && m_sampleRate == sampleRate && m_channels == channels) { return true; } @@ -52,9 +52,17 @@ bool AudioBackend::EnsureInitializedLocked(uint32_t sampleRate, uint32_t channel m_stream = nullptr; } - if (!SDL_InitSubSystem(SDL_INIT_AUDIO)) { - RT_LOG(RT_TAG_AUDIO) << "SDL_InitSubSystem(SDL_INIT_AUDIO) failed: " << SDL_GetError() << std::endl; - return false; + m_initialized = false; + m_sampleRate = 0; + m_channels = 0; + m_spec = {}; + + if (!m_audioSubsystemReady) { + if (!SDL_InitSubSystem(SDL_INIT_AUDIO)) { + RT_LOG(RT_TAG_AUDIO) << "SDL_InitSubSystem(SDL_INIT_AUDIO) failed: " << SDL_GetError() << std::endl; + return false; + } + m_audioSubsystemReady = true; } SDL_AudioSpec spec{}; @@ -99,12 +107,14 @@ void AudioBackend::Shutdown() { SDL_DestroyAudioStream(m_stream); m_stream = nullptr; } - if (m_initialized) { + if (m_audioSubsystemReady) { SDL_QuitSubSystem(SDL_INIT_AUDIO); + m_audioSubsystemReady = false; } m_initialized = false; m_sampleRate = 0; m_channels = 0; + m_spec = {}; m_reportedDroppedBlock = false; m_convertBuffer.clear(); } diff --git a/runtime/src/fiber_manager.cpp b/runtime/src/fiber_manager.cpp index 87fb7c5e2..a91c76078 100644 --- a/runtime/src/fiber_manager.cpp +++ b/runtime/src/fiber_manager.cpp @@ -3,9 +3,12 @@ #include "abi_bridge.h" #include "hle_stubs.h" #include "host_context.h" +#include "guest_callback_state.h" #include "runtime_log.h" -// Defined in hle/os/os_sleep.cpp; the sleep-timer table is file-local there. +extern "C" void OSExitThread_HLE_801aa0f0(CpuContext* ctx); +extern "C" int32_t OS__RestoreInterrupts_801a65d4(int32_t level); +void OS_HLE_RestoreInterruptMirror(bool enabled) noexcept; #include #include @@ -18,11 +21,28 @@ namespace Fiber { std::mutex GuestFiberManager::s_mutex; std::unordered_map GuestFiberManager::s_fibers; +std::unordered_map GuestFiberManager::s_threadGenerations; +uint64_t GuestFiberManager::s_nextThreadGeneration = 1; std::vector GuestFiberManager::s_fibersPendingDelete; void* GuestFiberManager::s_schedulerFiber = nullptr; uint32_t GuestFiberManager::s_currentGuestThread = 0; bool GuestFiberManager::s_initialized = false; -thread_local CpuContext* GuestFiberManager::s_cpuContext = nullptr; + +namespace { +void SwitchKeepingAmbientContext(void* target) { + CpuContext* const ambient = TryGetCpuContext(); + const uint32_t fpControl = MkwGetHostFpControl(); + const bool nandDraining = GuestCallbacks::nandDraining; + HostContext::Switch(target); + GuestCallbacks::nandDraining = nandDraining; + g_currentCpuContext = ambient; + if (ambient) { + MkwApplyHostNiMode(ambient->fpscr); + } else { + MkwRestoreHostMxcsr(fpControl); + } +} +} // namespace void GuestFiberManager::PurgePendingFibers() { std::vector toDelete; @@ -30,11 +50,18 @@ void GuestFiberManager::PurgePendingFibers() { std::lock_guard lock(s_mutex); toDelete.swap(s_fibersPendingDelete); } + std::vector stillRunning; for (void* f : toDelete) { - if (f && !HostContext::IsCurrent(f)) { + if (f && HostContext::IsCurrent(f)) { + stillRunning.push_back(f); + } else if (f) { HostContext::Destroy(f); } } + if (!stillRunning.empty()) { + std::lock_guard lock(s_mutex); + s_fibersPendingDelete.insert(s_fibersPendingDelete.end(), stillRunning.begin(), stillRunning.end()); + } } // Global VI retrace counter @@ -199,6 +226,7 @@ void GuestFiberManager::Initialize() { } void GuestFiberManager::Shutdown() { + PurgePendingFibers(); std::lock_guard lock(s_mutex); for (auto& [addr, fiber] : s_fibers) { @@ -208,6 +236,7 @@ void GuestFiberManager::Shutdown() { } } s_fibers.clear(); + s_threadGenerations.clear(); if (s_schedulerFiber) { HostContext::ShutdownScheduler(s_schedulerFiber); @@ -231,11 +260,17 @@ bool GuestFiberManager::CreateGuestFiber(uint32_t guestThreadAddr, uint32_t entr std::lock_guard lock(s_mutex); // Check if fiber already exists for this thread - if so, reset it + s_threadGenerations.erase(guestThreadAddr); auto existingIt = s_fibers.find(guestThreadAddr); if (existingIt != s_fibers.end()) { // Delete the old fiber if it exists and is not the scheduler fiber if (existingIt->second.fiber && !existingIt->second.isSchedulerFiber) { - HostContext::Destroy(existingIt->second.fiber); + if (HostContext::IsCurrent(existingIt->second.fiber)) { + s_fibersPendingDelete.push_back(existingIt->second.fiber); + s_currentGuestThread = 0; + } else { + HostContext::Destroy(existingIt->second.fiber); + } } s_fibers.erase(existingIt); } @@ -310,6 +345,7 @@ void GuestFiberManager::SuspendGuestThread(uint32_t guestThreadAddr) { void GuestFiberManager::ExitGuestThread(uint32_t guestThreadAddr, ThreadState finalState) { std::lock_guard lock(s_mutex); + s_threadGenerations.erase(guestThreadAddr); auto it = s_fibers.find(guestThreadAddr); if (it == s_fibers.end()) { @@ -338,6 +374,22 @@ void GuestFiberManager::SwitchToThread(uint32_t guestThreadAddr, CpuContext* cpu RT_LOG(RT_TAG_OS) << "SwitchToThread called before initialization!" << std::endl; return; } + + if (!cpu) { + cpu = TryGetCpuContext(); + if (!cpu) { + cpu = &GetPersistentCpuContext(); + } + } + CpuContext* const threadFile = &GetPersistentCpuContext(); + // Callback scratch registers do not replace the interrupted register file. + const bool fromScratch = cpu != threadFile; + CpuContext interruptedFile{}; + if (fromScratch) { + interruptedFile = *threadFile; + } + const bool interruptsEnabledAtSwitch = OS_HLE_InterruptsEnabled(); + const uint32_t hostFpControl = MkwGetHostFpControl(); void* fiberHandle = nullptr; uint32_t previousThread = 0; @@ -389,9 +441,6 @@ void GuestFiberManager::SwitchToThread(uint32_t guestThreadAddr, CpuContext* cpu } } - // Store CPU context pointer for the target fiber to use - s_cpuContext = cpu; - // Check if we're already on the target fiber (e.g., switching to main thread // when we're already on the scheduler fiber) if (HostContext::IsCurrent(fiberHandle)) { @@ -403,19 +452,24 @@ void GuestFiberManager::SwitchToThread(uint32_t guestThreadAddr, CpuContext* cpu } if (cpu && haveTargetContext) { - *cpu = targetContext; + *threadFile = targetContext; // FPSCR travels with the guest-thread context, and its NI bit is // modeled through the per-host-thread MXCSR; every context restore // must re-mirror it. - MkwApplyHostNiMode(cpu->fpscr); + MkwApplyHostNiMode(targetContext.fpscr); } // Switch to the target fiber (the target fiber will load its own context) - HostContext::Switch(fiberHandle); + SwitchKeepingAmbientContext(fiberHandle); + + if (fromScratch) { + *threadFile = interruptedFile; + } // When we return here, the fiber that issued SwitchToThread has resumed. // That does not automatically mean the previous guest thread became runnable // again; a different thread may simply have yielded back to the scheduler. + bool resumedPreviousThread = false; { std::lock_guard lock(s_mutex); uint32_t runningContext = 0; @@ -428,7 +482,7 @@ void GuestFiberManager::SwitchToThread(uint32_t guestThreadAddr, CpuContext* cpu currentContext = 0; } - const bool resumedPreviousThread = + resumedPreviousThread = previousThread != 0 && runningContext == previousThread && currentContext == previousThread; @@ -451,6 +505,37 @@ void GuestFiberManager::SwitchToThread(uint32_t guestThreadAddr, CpuContext* cpu s_currentGuestThread = 0; } } + if (resumedPreviousThread) { + OS__RestoreInterrupts_801a65d4(interruptsEnabledAtSwitch ? 1 : 0); + } else { + OS_HLE_RestoreInterruptMirror(interruptsEnabledAtSwitch); + } + if (CpuContext* ambient = TryGetCpuContext()) { + MkwApplyHostNiMode(ambient->fpscr); + } else { + MkwRestoreHostMxcsr(hostFpControl); + } + PurgePendingFibers(); +} + +void GuestFiberManager::SwitchToScheduler() { + CpuContext* const cpu = TryGetCpuContext(); + CpuContext* const threadFile = &GetPersistentCpuContext(); + const CpuContext savedThreadFile = *threadFile; + const CpuContext savedContext = cpu ? *cpu : savedThreadFile; + const uint32_t previousThread = s_currentGuestThread; + const bool interruptsEnabledAtSwitch = OS_HLE_InterruptsEnabled(); + + SwitchKeepingAmbientContext(s_schedulerFiber); + + *threadFile = savedThreadFile; + if (cpu) { + *cpu = savedContext; + MkwApplyHostNiMode(cpu->fpscr); + } + s_currentGuestThread = previousThread; + OS__RestoreInterrupts_801a65d4(interruptsEnabledAtSwitch ? 1 : 0); + PurgePendingFibers(); } uint32_t GuestFiberManager::GetCurrentGuestThread() { @@ -474,6 +559,16 @@ bool GuestFiberManager::IsTerminated(uint32_t guestThreadAddr) { return it != s_fibers.end() && it->second.terminated; } +uint64_t GuestFiberManager::GetThreadGeneration(uint32_t guestThreadAddr) { + std::lock_guard lock(s_mutex); + auto [it, inserted] = s_threadGenerations.try_emplace(guestThreadAddr, 0); + if (inserted) { + if (s_nextThreadGeneration == 0) std::abort(); + it->second = s_nextThreadGeneration++; + } + return it->second; +} + bool GuestFiberManager::RegisterMainThreadAsFiber(uint32_t guestThreadAddr, CpuContext* cpu) { if (!s_initialized) { RT_LOG(RT_TAG_OS) << "RegisterMainThreadAsFiber called before initialization!" << std::endl; @@ -535,7 +630,7 @@ void GuestFiberManager::FiberProc(void* param) // Get our fiber info - GuestFiber* fiber = nullptr; + void* fiberHandle = nullptr; uint32_t entryPoint = 0; uint32_t entryArg = 0; @@ -544,19 +639,19 @@ void GuestFiberManager::FiberProc(void* param) auto it = s_fibers.find(guestThreadAddr); if (it == s_fibers.end()) { RT_LOG(RT_TAG_OS) << "FiberProc: fiber not found!" << std::endl; - HostContext::Switch(s_schedulerFiber); - return; + } else { + fiberHandle = it->second.fiber; + entryPoint = it->second.entryPoint; + entryArg = it->second.entryArg; } - fiber = &it->second; - entryPoint = fiber->entryPoint; - entryArg = fiber->entryArg; + } + if (!fiberHandle) { + SwitchToScheduler(); + std::abort(); } // Get the CPU context - CpuContext* cpu = s_cpuContext; - if (!cpu) { - cpu = &GetPersistentCpuContext(); - } + CpuContext* cpu = &GetPersistentCpuContext(); // Guest OSContext: r2 (TOC/SDA2) at 0x08, r13 (SDA) at 0x34. Both must load correctly or // translated code loses access to global/static data. @@ -569,6 +664,7 @@ void GuestFiberManager::FiberProc(void* param) cpu->gpr[1] = Memory::Read32(guestThreadAddr + 0x04u); // Load saved LR cpu->lr = Memory::Read32(guestThreadAddr + 0x84u); + cpu->srr1 = Memory::Read32(guestThreadAddr + kCtxSrr1Offset); } catch (const Memory::AccessViolation& e) { RT_LOG(RT_TAG_OS) << "Failed to load guest context from 0x" << std::hex << guestThreadAddr @@ -581,7 +677,12 @@ void GuestFiberManager::FiberProc(void* param) cpu->srr0 = entryPoint; // Create a CpuContextScope for this fiber + // A new host stack has no enclosing CPU scope. + g_currentCpuContext = nullptr; + GuestCallbacks::nandDraining = false; CpuContextScope scope(cpu); + OS__RestoreInterrupts_801a65d4((cpu->srr1 & 0x8000u) != 0 ? 1 : 0); + PurgePendingFibers(); int startDeferAttempts = 0; while (entryPoint == MKW_GADDR(8024373c)) { // EGG::Thread::start @@ -607,11 +708,9 @@ void GuestFiberManager::FiberProc(void* param) << ", fn=0x" << startFn << ") after retries; continuing anyway." << std::dec << std::endl; break; } - HostContext::Switch(s_schedulerFiber); + SwitchToScheduler(); } - // The deferral loop above yields to the scheduler and therefore can resume - // with registers from a different guest fiber in the shared CpuContext. cpu->gpr[3] = entryArg; cpu->pc = entryPoint; cpu->srr0 = entryPoint; @@ -626,20 +725,45 @@ void GuestFiberManager::FiberProc(void* param) << " not found in registry!" << std::dec << std::endl; } - // Thread entry functions normally return into OSExitThread on hardware. - // Our host fiber call boundary observes the return directly, so complete the - // guest OSThread lifecycle here before handing control back to the scheduler. + bool ownsThread = false; + bool alreadyTerminated = false; + { + std::lock_guard lock(s_mutex); + const auto it = s_fibers.find(guestThreadAddr); + ownsThread = it != s_fibers.end() && it->second.fiber == fiberHandle; + alreadyTerminated = it != s_fibers.end() && it->second.terminated; + } + // A reused OSThread belongs to its replacement, even while this stack retires. + if (!ownsThread && !alreadyTerminated) { + SwitchToScheduler(); + std::abort(); + } + + const uint32_t returnValue = cpu->gpr[3]; + bool isRunningThread = false; + if (!alreadyTerminated) { + try { + isRunningThread = Memory::Read32(kOSRunningContextAddr) == guestThreadAddr; + } catch (const Memory::AccessViolation&) { + } + } + if (isRunningThread) { + OSExitThread_HLE_801aa0f0(cpu); + } try { - RemoveGuestThreadFromQueue(guestThreadAddr); - const uint16_t attributes = Memory::Read16(guestThreadAddr + kThreadAttrOffset); - const bool detached = (attributes & 1u) != 0; - const uint16_t finalState = detached ? 0u : static_cast(ThreadState::MORIBUND); - if (!detached) { - Memory::Write32(guestThreadAddr + kThreadExitValueOffset, 0); + if (!isRunningThread && !alreadyTerminated) { + RemoveGuestThreadFromQueue(guestThreadAddr); + const uint16_t attributes = Memory::Read16(guestThreadAddr + kThreadAttrOffset); + const bool detached = (attributes & 1u) != 0; + const uint16_t finalState = detached ? 0u : static_cast(ThreadState::MORIBUND); + if (!detached) { + Memory::Write32(guestThreadAddr + kThreadExitValueOffset, returnValue); + } + Memory::Write16(guestThreadAddr + kThreadStateOffset, finalState); + WakeGuestThreadsOnQueueNoSwitch(guestThreadAddr + kThreadJoinQueueOffset); + ExitGuestThread(guestThreadAddr, detached ? ThreadState::WAITING : ThreadState::MORIBUND); } - Memory::Write16(guestThreadAddr + kThreadStateOffset, finalState); - WakeGuestThreadsOnQueueNoSwitch(guestThreadAddr + kThreadJoinQueueOffset); if (Memory::Read32(kOSRunningContextAddr) == guestThreadAddr) { Memory::Write32(kOSRunningContextAddr, 0); } @@ -653,18 +777,9 @@ void GuestFiberManager::FiberProc(void* param) << " (" << e.reason() << ")" << std::endl; } - { - std::lock_guard lock(s_mutex); - auto it = s_fibers.find(guestThreadAddr); - if (it != s_fibers.end()) { - it->second.terminated = true; - it->second.state = ThreadState::MORIBUND; - } - s_currentGuestThread = 0; - } - // Return to scheduler - HostContext::Switch(s_schedulerFiber); + SwitchToScheduler(); + std::abort(); } } // namespace Fiber diff --git a/runtime/src/guest_flat_memory.cpp b/runtime/src/guest_flat_memory.cpp index 06d1898e9..119b35da3 100644 --- a/runtime/src/guest_flat_memory.cpp +++ b/runtime/src/guest_flat_memory.cpp @@ -142,7 +142,7 @@ std::vector& MappedRegions() { struct GuardedRange { uint32_t start = 0; - uint32_t end = 0; + uint64_t end = 0; }; std::vector& ExecutableRanges() { @@ -633,8 +633,8 @@ uint8_t* HostPointer(uint32_t guestAddress) { void ProtectDeferredRange(uint32_t address, size_t length) { if (RequiresCheckedAccess() || !g_initialized || length == 0) return; + if (length > kGuestSpaceSize - address) return; const uint64_t end = static_cast(address) + length; - if (end > kGuestSpaceSize) return; std::lock_guard lock(StateMutex()); const uint64_t first = static_cast(address) & ~(kHostPageSize - 1u); const uint64_t last = RoundUp(end, kHostPageSize); @@ -643,16 +643,17 @@ void ProtectDeferredRange(uint32_t address, size_t length) { // clears the readable bias, so nothing silently reads stale bytes. return; } - DeferredRanges().push_back(GuardedRange{address, static_cast(end)}); + DeferredRanges().push_back(GuardedRange{address, end}); } void UnprotectDeferredRange(uint32_t address, size_t length) { if (RequiresCheckedAccess() || !g_initialized || length == 0) return; + if (length > kGuestSpaceSize - address) return; std::lock_guard lock(StateMutex()); auto& ranges = DeferredRanges(); const uint64_t end = static_cast(address) + length; const auto it = std::find_if(ranges.begin(), ranges.end(), [&](const GuardedRange& range) { - return range.start == address && range.end == static_cast(end); + return range.start == address && range.end == end; }); if (it == ranges.end()) return; ranges.erase(it); @@ -721,7 +722,7 @@ bool HandleAccessViolation(void* faultAddress, bool isWrite) noexcept { { bool covered = false; uint32_t rangeStart = 0; - uint32_t rangeEnd = 0; + uint64_t rangeEnd = 0; uint64_t spanFirst = 0; uint64_t spanLast = 0; { diff --git a/runtime/src/hle/gx/gx_fifo.cpp b/runtime/src/hle/gx/gx_fifo.cpp index f122bd292..139b86dba 100644 --- a/runtime/src/hle/gx/gx_fifo.cpp +++ b/runtime/src/hle/gx/gx_fifo.cpp @@ -461,8 +461,14 @@ static void HleFifoWriteGeneral(u32 val, uint32_t sizeBytes) { const uint8_t opcode = cmd & GX_OPCODE_MASK_CMD; u32 sink = 0; - if (cmd == GX_NOP_CMD || opcode == GX_CMD_INVL_VC_CMD) { + if (cmd == GX_NOP_CMD) { + if (!consumeBytes(1, sink)) break; + continue; + } + + if (opcode == GX_CMD_INVL_VC_CMD) { if (!consumeBytes(1, sink)) break; + GXInvalidateVtxCache(); continue; } diff --git a/runtime/src/hle/input/pad.cpp b/runtime/src/hle/input/pad.cpp index c0c6b672d..980f1d672 100644 --- a/runtime/src/hle/input/pad.cpp +++ b/runtime/src/hle/input/pad.cpp @@ -64,9 +64,11 @@ void FillTriggersHeldByButtons(PADStatus* statuses) { } }; u32 count = 0; - scan(PADGetButtonMappings(port, &count), count); + PADButtonMapping* mappings = PADGetButtonMappings(port, &count); + scan(mappings, count); count = 0; - scan(PADGetAltButtonMappings(port, &count), count); + mappings = PADGetAltButtonMappings(port, &count); + scan(mappings, count); } } diff --git a/runtime/src/hle/net/network_core.cpp b/runtime/src/hle/net/network_core.cpp index 7c68222ec..f17d59788 100644 --- a/runtime/src/hle/net/network_core.cpp +++ b/runtime/src/hle/net/network_core.cpp @@ -379,10 +379,10 @@ void CloseNativeSocket(NativeSocket socket) { #endif } -void SetNonBlocking(NativeSocket socket, bool enabled) { +bool SetNonBlocking(NativeSocket socket, bool enabled) { #ifdef _WIN32 u_long mode = enabled ? 1 : 0; - ioctlsocket(socket, FIONBIO, &mode); + return ioctlsocket(socket, FIONBIO, &mode) == 0; #else int flags = fcntl(socket, F_GETFL, 0); if (flags >= 0) { @@ -391,8 +391,9 @@ void SetNonBlocking(NativeSocket socket, bool enabled) { } else { flags &= ~O_NONBLOCK; } - fcntl(socket, F_SETFL, flags); + return fcntl(socket, F_SETFL, flags) == 0; } + return false; #endif } @@ -446,11 +447,12 @@ int32_t ReconnectWiiSocket(WiiSocket& socket, uint16_t port) { CloseNativeSocket(socket.native); socket.native = native; + socket.nativeNonblocking = true; socket.generation = NextSocketGeneration(); socket.peerAddr = addr; socket.peerPort = port; socket.nonblocking = false; - SetNonBlocking(socket.native, false); + SetSocketNonBlocking(socket, false); return 0; } diff --git a/runtime/src/hle/net/network_deferred.cpp b/runtime/src/hle/net/network_deferred.cpp index c1627fb9f..5e784fd7e 100644 --- a/runtime/src/hle/net/network_deferred.cpp +++ b/runtime/src/hle/net/network_deferred.cpp @@ -39,6 +39,7 @@ struct DeferredNetworkRoute { uint64_t token = 0; uint32_t waitQueue = 0; uint32_t expectedThread = 0; + uint64_t threadGeneration = 0; uint32_t callback = 0; uint32_t callbackArg = 0; }; @@ -123,12 +124,27 @@ struct DeferredConnectWork { std::optional initialResult; }; +struct DeferredReceiveWork { + DeferredNetworkRoute route{}; + NetworkPollContract::Timeout timeout{}; + uint32_t deviceFd = 0; + uint32_t wiiFd = 0; + NativeSocket nativeFd = kInvalidSocket; + uint64_t socketGeneration = 0; + uint32_t flags = 0; + uint32_t dataAddress = 0; + uint32_t dataSize = 0; + std::optional initialResult; +}; + using DeferredDnsPreparation = NetworkDeferredContract::Preparation; using DeferredPollPreparation = NetworkDeferredContract::Preparation; using DeferredConnectPreparation = NetworkDeferredContract::Preparation; +using DeferredReceivePreparation = + NetworkDeferredContract::Preparation; struct DeferredNetworkStore { std::mutex completedMutex; @@ -137,6 +153,7 @@ struct DeferredNetworkStore { // thread, so unlike DNS worker completions they need no cross-thread lock. std::vector pendingPolls; std::vector pendingConnects; + std::vector pendingReceives; std::mutex resultMutex; std::unordered_map syncResults; std::mutex schedulerThreadMutex; @@ -165,6 +182,14 @@ void AbortPendingDeferredConnects(uint32_t wiiFd, int32_t result) { } } +void AbortPendingDeferredReceives(uint32_t wiiFd, int32_t result) { + for (DeferredReceiveWork& work : GetDeferredNetworkStore().pendingReceives) { + if (work.wiiFd == wiiFd && !work.initialResult) { + work.initialResult = result; + } + } +} + static void NoteDeferredNetworkSchedulerThread(const char* operation) { DeferredNetworkStore& store = GetDeferredNetworkStore(); const std::thread::id current = std::this_thread::get_id(); @@ -458,6 +483,52 @@ static DeferredConnectPreparation PrepareDeferredConnect( return DeferredConnectPreparation::Ready(std::move(work), -SO_EINVAL); } +static DeferredReceivePreparation PrepareDeferredReceive( + uint32_t fd, uint32_t numIn, uint32_t numOut, uint32_t vectorPtr) { + const uint64_t count = static_cast(numIn) + numOut; + if (!numIn || !numOut || count > kMaxIoVectors || !vectorPtr || + !Memory::Contains(vectorPtr, static_cast(count) * 8u)) { + return DeferredReceivePreparation::Error(-SO_EINVAL); + } + const auto vectors = ReadVectors(vectorPtr, static_cast(count)); + const IoVector& control = vectors[0]; + const IoVector& data = vectors[numIn]; + if (control.size < 8u || !control.address || !Memory::Contains(control.address, 8u) || + data.size > static_cast(INT32_MAX) || + (data.size && (!data.address || !Memory::Contains(data.address, data.size)))) { + return DeferredReceivePreparation::Error(-SO_EINVAL); + } + const uint32_t wiiFd = Memory::Read32(control.address); + WiiSocket* socket = GetWiiSocket(wiiFd); + if (!socket) { + return DeferredReceivePreparation::Error(-SO_EBADF); + } + const uint32_t flags = Memory::Read32(control.address + 4u); + const bool hasFrom = numOut > 1 && vectors[numIn + 1u].address && + vectors[numIn + 1u].size >= 8u; + if (!data.size || socket->type != SOCK_STREAM || socket->nonblocking || (flags & 0x04u) || hasFrom) { + return DeferredReceivePreparation::NotApplicable(); + } + + // Try the nonblocking read itself; readiness can be lost to another reader. + const int32_t result = ReceiveFromNow(wiiFd, flags, data.address, data.size); + if (result != -SO_EAGAIN) { + return DeferredReceivePreparation::Error(result); + } + DeferredReceiveWork work{}; + work.timeout = NetworkPollContract::Timeout::FromMilliseconds(1000); + work.deviceFd = fd; + work.wiiFd = wiiFd; + work.nativeFd = socket->native; + work.socketGeneration = socket->generation; + work.flags = flags & 0x03u; + // IOS async callers retain output storage until their callback. The work + // owns copied addresses, never pointers into the caller's vector array. + work.dataAddress = data.address; + work.dataSize = data.size; + return DeferredReceivePreparation::Ready(std::move(work), -SO_ENOMEM); +} + static DeferredDnsPreparation PrepareDeferredGetAddrInfo( uint32_t fd, uint32_t cmd, uint32_t numIn, uint32_t numOut, uint32_t vectorPtr) { if (!IsIpTopCommand(fd, cmd, IOCTLV_SO_GETADDRINFO)) { @@ -641,6 +712,7 @@ static bool InitializeDeferredSyncRoute(DeferredNetworkRoute& route, uint32_t wa route.token = nextToken; route.waitQueue = waitQueue; route.expectedThread = expectedThread; + route.threadGeneration = Fiber::GuestFiberManager::GetThreadGeneration(expectedThread); // ABA cookie: thread/stack addresses are reused after cancellation. Memory::Write64(waitQueue + 8u, nextToken); return true; @@ -690,6 +762,11 @@ static bool InstallDeferredPoll(DeferredPollWork work) { return true; } +static bool InstallDeferredReceive(DeferredReceiveWork work) { + GetDeferredNetworkStore().pendingReceives.push_back(std::move(work)); + return true; +} + // A blocking connect issues the host connect() at submission time, because the // SDK's own retry loop depends on seeing the immediate outcome; only a genuinely // in-flight connect is parked for the completion pump to probe. @@ -800,6 +877,12 @@ NetworkDeferredContract::StartOutcome Network_HLE_StartIoctlSync( NetworkDeferredContract::StartOutcome Network_HLE_StartIoctlvSync( uint32_t fd, uint32_t cmd, uint32_t numIn, uint32_t numOut, uint32_t vectorPtr, uint32_t waitQueue) { + if (IsIpTopCommand(fd, cmd, IOCTLV_SO_RECVFROM)) { + return RunDeferredEntry(-SO_EINVAL, -SO_EINVAL, [&] { + return StartDeferred(PrepareDeferredReceive(fd, numIn, numOut, vectorPtr), + SyncRoute(waitQueue), InstallDeferredReceive); + }); + } if (!IsIpTopCommand(fd, cmd, IOCTLV_SO_GETADDRINFO)) { return DeferredStartOutcome::NotApplicable(); } @@ -825,6 +908,12 @@ NetworkDeferredContract::StartOutcome Network_HLE_StartIoctlAsync( NetworkDeferredContract::StartOutcome Network_HLE_StartIoctlvAsync( uint32_t fd, uint32_t cmd, uint32_t numIn, uint32_t numOut, uint32_t vectorPtr, uint32_t callback, uint32_t callbackArg) { + if (IsIpTopCommand(fd, cmd, IOCTLV_SO_RECVFROM)) { + return RunDeferredEntry(-SO_EINVAL, -SO_EINVAL, [&] { + return StartDeferred(PrepareDeferredReceive(fd, numIn, numOut, vectorPtr), + AsyncRoute(callback, callbackArg), InstallDeferredReceive); + }); + } if (!IsIpTopCommand(fd, cmd, IOCTLV_SO_GETADDRINFO)) { return DeferredStartOutcome::NotApplicable(); } @@ -854,7 +943,8 @@ static bool DeferredNetworkWaiterIsValid(const DeferredNetworkRoute& route) { if (!route.waitQueue || !route.expectedThread || !Memory::Contains(route.waitQueue, 16u) || !Memory::Contains(route.expectedThread + 0x2C8u, 0x20u) || - Fiber::GuestFiberManager::IsTerminated(route.expectedThread)) { + Fiber::GuestFiberManager::IsTerminated(route.expectedThread) || + Fiber::GuestFiberManager::GetThreadGeneration(route.expectedThread) != route.threadGeneration) { return false; } @@ -952,6 +1042,40 @@ static std::optional ProbeDeferredConnect( return ClassifySettledConnect(work.nativeFd); } +static bool ReceiveIsReady(const DeferredReceiveWork& work) { + NetworkPollContract::NativePollFd descriptor{}; + descriptor.fd = work.nativeFd; +#ifdef _WIN32 + descriptor.events = (work.flags & 0x01u) ? POLLRDBAND : POLLRDNORM; + const int result = WSAPoll(&descriptor, 1, 0); +#else + descriptor.events = (work.flags & 0x01u) ? POLLPRI : POLLIN; + const int result = poll(&descriptor, 1, 0); +#endif + return result < 0 || (result > 0 && descriptor.revents != 0); +} + +static std::optional ProbeDeferredReceive( + const DeferredReceiveWork& work, NetworkPollContract::Timeout::TimePoint now) { + if (work.initialResult) { + return *work.initialResult; + } + if (!IsIpTopDevice(work.deviceFd) || + !SocketIdentityIsCurrent(work.wiiFd, work.nativeFd, work.socketGeneration)) { + return -SO_ENOTCONN; + } + const bool expired = work.timeout.IsExpired(now); + if (!expired && !ReceiveIsReady(work)) { + return std::nullopt; + } + const int32_t result = ReceiveFromNow(work.wiiFd, work.flags, work.dataAddress, work.dataSize); + // A competing read or a spurious readiness event must not shorten the wait. + if (result == -SO_EAGAIN && !expired) { + return std::nullopt; + } + return result; +} + bool Network_HLE_ProcessCompletions(CpuContext* cpu) { if (!cpu) { return false; @@ -1067,5 +1191,28 @@ bool Network_HLE_ProcessCompletions(CpuContext* cpu) { DeliverDeferredNetworkResult(cpu, store, route, result); handledAny = true; } + for (auto it = store.pendingReceives.begin(); it != store.pendingReceives.end();) { + const DeferredReceiveWork& work = *it; + const DeferredNetworkRoute route = work.route; + if (route.kind == DeferredNetworkCompletionKind::SyncWaitQueue && + !DeferredNetworkWaiterIsValid(route)) { + it = store.pendingReceives.erase(it); + continue; + } + std::optional result; + try { + result = ProbeDeferredReceive(work, now); + } catch (const Memory::AccessViolation&) { + result = -SO_EINVAL; + } + if (!result) { + ++it; + continue; + } + g_lastSocketError = *result < 0 ? *result : 0; + it = store.pendingReceives.erase(it); + DeliverDeferredNetworkResult(cpu, store, route, *result); + handledAny = true; + } return handledAny; } diff --git a/runtime/src/hle/net/network_internal.h b/runtime/src/hle/net/network_internal.h index 97c06088d..c411b96f8 100644 --- a/runtime/src/hle/net/network_internal.h +++ b/runtime/src/hle/net/network_internal.h @@ -138,6 +138,7 @@ struct WiiSocket { // fd is recycled, so a new socket reports its own failures again. int32_t lastLoggedSendError = 0; int32_t lastLoggedRecvError = 0; + bool nativeNonblocking = true; }; extern std::array g_sockets; @@ -148,6 +149,7 @@ extern int32_t g_lastSocketError; // linger (IOS/Network/Socket.cpp:214-235 and 164-212). Defined below, next to // the deferred store it walks. void AbortPendingDeferredConnects(uint32_t wiiFd, int32_t result); +void AbortPendingDeferredReceives(uint32_t wiiFd, int32_t result); enum class NasSslWriteAction { PassThrough, @@ -215,7 +217,14 @@ int ProbeConnectSettled(NativeSocket socket, int timeoutMs); int32_t ClassifySettledConnect(NativeSocket socket); bool WaitForReadable(NativeSocket socket, int timeoutMs); void CloseNativeSocket(NativeSocket socket); -void SetNonBlocking(NativeSocket socket, bool enabled); +bool SetNonBlocking(NativeSocket socket, bool enabled); +inline bool SetSocketNonBlocking(WiiSocket& socket, bool enabled) { + if (!SetNonBlocking(socket.native, enabled)) { + return false; + } + socket.nativeNonblocking = enabled; + return true; +} int32_t ReconnectWiiSocket(WiiSocket& socket, uint16_t port); int MapWiiAf(uint32_t af); int MapNativeAfToWii(int af); @@ -244,6 +253,8 @@ void WritePollResults(uint32_t outAddress, // network_socket.cpp void CleanupAllWiiSockets(); sockaddr_in ReadWiiSockAddr(uint32_t addr); +int32_t ReceiveFromNow(uint32_t fd, uint32_t flags, uint32_t dataAddress, + uint32_t dataSize, uint32_t fromAddress = 0, uint32_t fromSize = 0); int32_t HandleIpTopIoctl(uint32_t cmd, uint32_t inBuf, uint32_t inLen, uint32_t outBuf, uint32_t outLen); int32_t HandleIpTopIoctlv(uint32_t cmd, const std::vector& in, diff --git a/runtime/src/hle/net/network_socket.cpp b/runtime/src/hle/net/network_socket.cpp index f8a55fb2f..8f91d2c4a 100644 --- a/runtime/src/hle/net/network_socket.cpp +++ b/runtime/src/hle/net/network_socket.cpp @@ -1,4 +1,5 @@ #include "network_internal.h" +#include "gx_guest_write.h" namespace NetworkHle { @@ -35,6 +36,7 @@ int32_t DeleteWiiSocket(uint32_t fd) { // own kept the guest OSThread asleep until the next scheduler pump and then // reported -SO_EBADF instead. AbortPendingDeferredConnects(fd, -SO_ENOTCONN); + AbortPendingDeferredReceives(fd, -SO_ENOTCONN); return 0; } @@ -73,6 +75,47 @@ static void WriteWiiSockAddr(uint32_t addr, const sockaddr_in& native, uint32_t Memory::Write8(addr + 1, kWiiAfInet); Memory::Write16(addr + 2, ntohs(native.sin_port)); Memory::Write32(addr + 4, ntohl(native.sin_addr.s_addr)); + GxGuestWrite::NotifyWrite(addr, 8u); +} + +int32_t ReceiveFromNow(uint32_t fd, uint32_t flags, uint32_t dataAddress, + uint32_t dataSize, uint32_t fromAddress, uint32_t fromSize) { + WiiSocket* socket = GetWiiSocket(fd); + if (!socket) return -SO_EBADF; + if (dataSize > static_cast(INT32_MAX) || + (dataSize && (!dataAddress || !Memory::Contains(dataAddress, dataSize))) || + (fromAddress && (fromSize < 8u || !Memory::Contains(fromAddress, 8u)))) { + return -SO_EINVAL; + } + char empty = 0; + char* data = dataSize + ? reinterpret_cast(Memory::GetPointer(dataAddress, dataSize)) : ∅ + const bool restoreBlocking = !socket->nativeNonblocking; + if (!SetSocketNonBlocking(*socket, true)) { + return SocketErrorResult(NativeLastError()); + } + sockaddr_in from{}; + socklen_t fromLen = sizeof(from); + const int ret = recvfrom(socket->native, data, static_cast(dataSize), + static_cast(flags & 0x03u), + fromAddress ? reinterpret_cast(&from) : nullptr, + fromAddress ? &fromLen : nullptr); + const int nativeError = ret < 0 ? NativeLastError() : 0; + if (restoreBlocking && !SetSocketNonBlocking(*socket, false)) { + NetFail("recv fd=%u failed to restore host socket mode host=%d", fd, NativeLastError()); + } + if (ret > 0) { + GxGuestWrite::NotifyWrite(dataAddress, static_cast(ret)); + } + if (ret >= 0 && fromAddress) { + WriteWiiSockAddr(fromAddress, from, static_cast(fromLen)); + } + const int32_t result = ret >= 0 ? SocketResult(ret) : SocketErrorResult(nativeError); + if (ret < 0 && result != -SO_EAGAIN && result != socket->lastLoggedRecvError) { + socket->lastLoggedRecvError = result; + NetFail("recv failed fd=%u host=%d wii=%d", fd, nativeError, result); + } + return result; } static int MapSockOptLevel(uint32_t level) { @@ -284,6 +327,9 @@ int32_t HandleIpTopIoctl(uint32_t cmd, uint32_t inBuf, uint32_t inLen, uint32_t } const int32_t result = SocketResult(shutdown(s->native, static_cast(how)), false); + if (result == 0 && (how == 0 || how == 2)) { + AbortPendingDeferredReceives(wiiFd, 0); + } if (how == 1 || how == 2) { // Dolphin aborts a pending blocking connect with -SO_ENETUNREACH // when the write half is shut down. @@ -481,47 +527,16 @@ int32_t HandleIpTopIoctlv(uint32_t cmd, const std::vector& in, const s return result; } case IOCTLV_SO_RECVFROM: { - if (in.empty() || out.empty()) return -SO_EINVAL; - const uint32_t fd = Memory::Read32(in[0].address); - WiiSocket* s = GetWiiSocket(fd); - if (!s) return -SO_EBADF; - const uint32_t rawFlags = Memory::Read32(in[0].address + 4); - uint32_t flags = rawFlags & 0x03; - const bool forceNonBlock = (rawFlags & 0x04u) != 0; - char* data = reinterpret_cast(Memory::GetPointer(out[0].address, out[0].size)); - sockaddr_in from{}; - socklen_t fromLen = sizeof(from); - sockaddr* fromPtr = out.size() > 1 && out[1].address && out[1].size >= 8 ? reinterpret_cast(&from) : nullptr; - int ret = 0; - - ret = recvfrom(s->native, data, static_cast(out[0].size), static_cast(flags), fromPtr, - fromPtr ? &fromLen : nullptr); - int nativeErr = ret < 0 ? NativeLastError() : 0; - // Nonblocking sockets get -SO_EAGAIN immediately (Dolphin's retry predicate - // short-circuits on nonBlock/forceNonBlock, IOS/Network/Socket.cpp:715-718); - // waiting here anyway stalled the whole emulation thread on every empty read. - constexpr int kStreamRecvWaitMs = 1000; - const int streamWaitMs = (forceNonBlock || s->nonblocking) ? 0 : kStreamRecvWaitMs; - const bool waited = ret < 0 && !fromPtr && s->type == SOCK_STREAM && - IsWouldBlockError(nativeErr) && WaitForReadable(s->native, streamWaitMs); - if (waited) { - ret = recvfrom(s->native, data, static_cast(out[0].size), static_cast(flags), nullptr, - nullptr); - // WaitForReadable also returns true for POLLERR/POLLHUP, so the retry - // is where a reset connection surfaces; the first call's would-block - // error must not be reused or the guest retries that socket forever. - nativeErr = ret < 0 ? NativeLastError() : 0; - } - - if (ret >= 0 && fromPtr) { - WriteWiiSockAddr(out[1].address, from, static_cast(fromLen)); - } - const int32_t result = ret >= 0 ? SocketResult(ret) : SocketErrorResult(nativeErr); - if (ret < 0 && result != -SO_EAGAIN && result != s->lastLoggedRecvError) { - s->lastLoggedRecvError = result; - NetFail("recv failed fd=%u host=%d wii=%d", fd, nativeErr, result); + if (in.empty() || out.empty() || in[0].size < 8u || + !in[0].address || !Memory::Contains(in[0].address, 8u)) { + return -SO_EINVAL; } - return result; + const uint32_t fd = Memory::Read32(in[0].address); + const uint32_t flags = Memory::Read32(in[0].address + 4u); + const bool hasFrom = out.size() > 1 && out[1].address && out[1].size >= 8u; + return ReceiveFromNow(fd, flags, out[0].address, out[0].size, + hasFrom ? out[1].address : 0u, + hasFrom ? out[1].size : 0u); } default: return -SO_EINVAL; diff --git a/runtime/src/hle/net/network_ssl.cpp b/runtime/src/hle/net/network_ssl.cpp index 9e4fd76d9..e94271399 100644 --- a/runtime/src/hle/net/network_ssl.cpp +++ b/runtime/src/hle/net/network_ssl.cpp @@ -920,7 +920,7 @@ int32_t HandleSslIoctlv(uint32_t cmd, const std::vector& in, const std ssl.native = socket->native; ssl.plaintextWfc = false; socket->nonblocking = false; - SetNonBlocking(socket->native, false); + SetSocketNonBlocking(*socket, false); if (IsRetroPlaintextSslHost(ssl.hostname) && socket->peerPort == 443) { const int32_t reroute = ReconnectWiiSocket(*socket, 80); if (reroute != 0) { diff --git a/runtime/src/hle/os/os_interrupt.cpp b/runtime/src/hle/os/os_interrupt.cpp index 56f68714d..9e056e259 100644 --- a/runtime/src/hle/os/os_interrupt.cpp +++ b/runtime/src/hle/os/os_interrupt.cpp @@ -76,6 +76,12 @@ bool OS_HLE_InterruptsEnabled() noexcept return g_interrupts_enabled.load(std::memory_order_acquire); } +void OS_HLE_RestoreInterruptMirror(bool enabled) noexcept +{ + // An idle host frame must not update another guest thread's context flags. + g_interrupts_enabled.store(enabled, std::memory_order_release); +} + extern "C" uint32_t OS____InterruptInit_801a661c(uint32_t r3, uint32_t r4, uint32_t r5, uint32_t r6, uint32_t r7, uint32_t r8) { (void)r3; diff --git a/runtime/src/hle/os/os_scheduler.cpp b/runtime/src/hle/os/os_scheduler.cpp index ffb0e8c1c..d513c890b 100644 --- a/runtime/src/hle/os/os_scheduler.cpp +++ b/runtime/src/hle/os/os_scheduler.cpp @@ -103,10 +103,12 @@ static void TryInvokeSwitchCallback(uint32_t oldCtx, uint32_t newCtx, CpuContext } if (TranslatedFunctionRegistry::FindByAddressPtr(callbackAddr)) { - CpuContext* ctx = cpu ? cpu : &GetPersistentCpuContext(); - ctx->gpr[3] = oldCtx; - ctx->gpr[4] = newCtx; - InvokeIndirectCpu(callbackAddr, ctx); + const CpuContext* live = cpu ? cpu : &GetPersistentCpuContext(); + CpuContext callbackCpu = *live; + callbackCpu.gpr[3] = oldCtx; + callbackCpu.gpr[4] = newCtx; + CpuContextScope scope(&callbackCpu); + InvokeIndirectCpu(callbackAddr, &callbackCpu); return; } diff --git a/runtime/src/hle/storage/nand_async.cpp b/runtime/src/hle/storage/nand_async.cpp index aeb196271..dbc9ff619 100644 --- a/runtime/src/hle/storage/nand_async.cpp +++ b/runtime/src/hle/storage/nand_async.cpp @@ -5,100 +5,6 @@ #include "nand_internal.h" -// Guest callback dispatch: host NAND work completes synchronously, so an "async" call just -// queues its guest completion callback here and an HLE pump dispatches it later. - -struct PendingNandCallback { - uint32_t callbackPtr = 0; - int32_t result = 0; - uint32_t commandBlock = 0; -}; - -static std::mutex g_pendingNandCallbackMutex; -static std::deque g_pendingNandCallbacks; - -static void DispatchNandCallback(CpuContext* cpu, uint32_t callbackPtr, int32_t result, uint32_t commandBlock) { - if (callbackPtr == 0) { - return; - } - - // The callback signature is: void callback(int result, void* commandBlock) - // We need to invoke it through the translated function system - if (!TranslatedFunctionRegistry::FindByAddressPtr(callbackPtr)) { - LogNandError("InvokeNandCallback", "callback 0x%08X not found in registry", callbackPtr); - return; - } - - if (cpu) { - // IOS/NAND completions are asynchronous side calls. Run them on a - // scratch guest CPU context so callback argument setup and any - // transient LR/CTR/nonvolatile register changes do not corrupt the - // interrupted thread that happened to pump the completion queue. - CpuContext callbackCpu = *cpu; - callbackCpu.gpr[3] = static_cast(result); - callbackCpu.gpr[4] = commandBlock; - CpuContextScope callbackScope(&callbackCpu); - InvokeIndirectCpu(callbackPtr, &callbackCpu); - } else { - auto& callbackCpu = GetPersistentCpuContext(); - callbackCpu.gpr[3] = static_cast(result); - callbackCpu.gpr[4] = commandBlock; - InvokeIndirectCpu(callbackPtr, &callbackCpu); - } -} - -static void InvokeNandCallback(uint32_t callbackPtr, int32_t result, uint32_t commandBlock) { - if (callbackPtr == 0) { - return; - } - - std::lock_guard lock(g_pendingNandCallbackMutex); - g_pendingNandCallbacks.push_back({callbackPtr, result, commandBlock}); -} - -void NandQueueIosCallback(uint32_t callbackPtr, int32_t result, uint32_t callbackArg) { - InvokeNandCallback(callbackPtr, result, callbackArg); -} - -// Guest NAND callbacks can re-enter this drain: RFL's hidden-Mii loader chains one async -// load per Mii as its own completion callback, and a database fattened by online play -// nested hundreds of frames deep and crashed at boot. Hardware is iterative via the IPC -// interrupt, so a nested drain refuses here and the outermost loop dispatches the queue flat. -static thread_local int g_nandCallbackDrainDepth = 0; - -bool NandProcessPendingCallbacks(CpuContext* cpu, int maxToProcess) { - if (maxToProcess <= 0) { - return false; - } - if (g_nandCallbackDrainDepth != 0) { - return false; - } - struct DrainScope { - DrainScope() { ++g_nandCallbackDrainDepth; } - ~DrainScope() { --g_nandCallbackDrainDepth; } - } drainScope; - - // Host-side NAND work finishes immediately. Drain a generous chunk of the guest - // callback queue in one pump so multi-step chains (open -> read -> close -> user cb) - // do not get stranded across many retrace ticks and trip the RFL/Mii error path. - const size_t maxCallbacksThisPump = std::max(static_cast(maxToProcess), 64); - size_t processed = 0; - while (processed < maxCallbacksThisPump) { - PendingNandCallback cb{}; - { - std::lock_guard lock(g_pendingNandCallbackMutex); - if (g_pendingNandCallbacks.empty()) { - break; - } - cb = g_pendingNandCallbacks.front(); - g_pendingNandCallbacks.pop_front(); - } - DispatchNandCallback(cpu, cb.callbackPtr, cb.result, cb.commandBlock); - ++processed; - } - return processed != 0; -} - // Async NAND entry points run the sync call, queue the guest completion callback, and return. // `params`'s last two args must stay named callbackPtr/commandBlockPtr since the macro bodies // reference them directly. PPC_NATIVE_OVERRIDE calls stay written out verbatim, not macro-generated, @@ -108,7 +14,7 @@ bool NandProcessPendingCallbacks(CpuContext* cpu, int maxToProcess) { #define NAND_ASYNC_FWD_BODY(Name, Sync, params, sync_args) \ extern "C" int32_t Name params { \ int32_t result = Sync sync_args; \ - InvokeNandCallback(callbackPtr, result, commandBlockPtr); \ + NandQueueIosCallback(callbackPtr, result, commandBlockPtr); \ return result; \ } @@ -117,7 +23,7 @@ bool NandProcessPendingCallbacks(CpuContext* cpu, int maxToProcess) { #define NAND_ASYNC_FWD_BODY_OKZERO(Name, Sync, params, sync_args) \ extern "C" int32_t Name params { \ int32_t result = Sync sync_args; \ - InvokeNandCallback(callbackPtr, result, commandBlockPtr); \ + NandQueueIosCallback(callbackPtr, result, commandBlockPtr); \ return (result < 0) ? result : NAND_RESULT_OK; \ } diff --git a/runtime/src/hle/storage/nand_callbacks.cpp b/runtime/src/hle/storage/nand_callbacks.cpp new file mode 100644 index 000000000..f0b40c974 --- /dev/null +++ b/runtime/src/hle/storage/nand_callbacks.cpp @@ -0,0 +1,90 @@ +#include "nand_internal.h" +#include "guest_callback_state.h" + +// Guest callback dispatch: host NAND work completes synchronously, so an "async" call just +// queues its guest completion callback here and an HLE pump dispatches it later. + +struct PendingNandCallback { + uint32_t callbackPtr = 0; + int32_t result = 0; + uint32_t commandBlock = 0; +}; + +static std::mutex g_pendingNandCallbackMutex; +static std::deque g_pendingNandCallbacks; + +static void DispatchNandCallback(CpuContext* cpu, uint32_t callbackPtr, int32_t result, uint32_t commandBlock) { + if (callbackPtr == 0) { + return; + } + + // The callback signature is: void callback(int result, void* commandBlock) + // We need to invoke it through the translated function system + if (!TranslatedFunctionRegistry::FindByAddressPtr(callbackPtr)) { + LogNandError("InvokeNandCallback", "callback 0x%08X not found in registry", callbackPtr); + return; + } + + if (cpu) { + // IOS/NAND completions are asynchronous side calls. Run them on a + // scratch guest CPU context so callback argument setup and any + // transient LR/CTR/nonvolatile register changes do not corrupt the + // interrupted thread that happened to pump the completion queue. + CpuContext callbackCpu = *cpu; + callbackCpu.gpr[3] = static_cast(result); + callbackCpu.gpr[4] = commandBlock; + CpuContextScope callbackScope(&callbackCpu); + InvokeIndirectCpu(callbackPtr, &callbackCpu); + } else { + auto& callbackCpu = GetPersistentCpuContext(); + callbackCpu.gpr[3] = static_cast(result); + callbackCpu.gpr[4] = commandBlock; + InvokeIndirectCpu(callbackPtr, &callbackCpu); + } +} + +void NandQueueIosCallback(uint32_t callbackPtr, int32_t result, uint32_t commandBlock) { + if (callbackPtr == 0) { + return; + } + + std::lock_guard lock(g_pendingNandCallbackMutex); + g_pendingNandCallbacks.push_back({callbackPtr, result, commandBlock}); +} + +// Guest NAND callbacks can re-enter this drain: RFL's hidden-Mii loader chains one async +// load per Mii as its own completion callback, and a database fattened by online play +// nested hundreds of frames deep and crashed at boot. Hardware is iterative via the IPC +// interrupt, so a nested drain refuses here and the outermost loop dispatches the queue flat. +bool NandProcessPendingCallbacks(CpuContext* cpu, int maxToProcess) { + if (maxToProcess <= 0) { + return false; + } + if (GuestCallbacks::nandDraining) { + return false; + } + struct DrainScope { + DrainScope() { GuestCallbacks::nandDraining = true; } + ~DrainScope() { GuestCallbacks::nandDraining = false; } + } drainScope; + + // Host-side NAND work finishes immediately. Drain a generous chunk of the guest + // callback queue in one pump so multi-step chains (open -> read -> close -> user cb) + // do not get stranded across many retrace ticks and trip the RFL/Mii error path. + const size_t maxCallbacksThisPump = std::max(static_cast(maxToProcess), 64); + size_t processed = 0; + while (processed < maxCallbacksThisPump) { + PendingNandCallback cb{}; + { + std::lock_guard lock(g_pendingNandCallbackMutex); + if (g_pendingNandCallbacks.empty()) { + break; + } + cb = g_pendingNandCallbacks.front(); + g_pendingNandCallbacks.pop_front(); + } + DispatchNandCallback(cpu, cb.callbackPtr, cb.result, cb.commandBlock); + ++processed; + } + return processed != 0; +} diff --git a/runtime/src/hle/storage/nand_fs.cpp b/runtime/src/hle/storage/nand_fs.cpp index 3952c324b..d5a174fb1 100644 --- a/runtime/src/hle/storage/nand_fs.cpp +++ b/runtime/src/hle/storage/nand_fs.cpp @@ -6,7 +6,9 @@ #include "isa/big_endian.h" #include "hle/storage/riivolution.h" #include "nand_guest_path.h" +#include "host_file.h" #include "runtime_log.h" +#include "u8_archive.h" // ============================================================================ // Configuration @@ -102,12 +104,7 @@ std::string HostPathText(const std::filesystem::path& path) { } FILE* NandFopen(const std::filesystem::path& path, const char* mode) { -#ifdef _WIN32 - const std::wstring wideMode(mode, mode + std::strlen(mode)); - return _wfopen(path.c_str(), wideMode.c_str()); -#else - return std::fopen(path.c_str(), mode); -#endif + return RuntimeHostFile::Open(path, mode); } bool NandRemove(const std::filesystem::path& path) { @@ -279,12 +276,6 @@ std::filesystem::path TranslateNandPath(const char* wiiPath) { // U8 archive helper (used to seed FaceLib resources from the ISO) // ============================================================================ -struct U8Node { - uint32_t typeAndNameOffset; - uint32_t dataOffset; - uint32_t size; -}; - static bool ExtractFromU8(const std::filesystem::path& archivePath, const char* targetName, std::vector& outData) { std::ifstream file(archivePath, std::ios::binary); @@ -294,7 +285,7 @@ static bool ExtractFromU8(const std::filesystem::path& archivePath, const char* file.seekg(0, std::ios::end); const std::streamsize size = file.tellg(); - if (size < 0x20) { + if (size < 0x20 || static_cast(size) > std::vector{}.max_size()) { return false; } file.seekg(0, std::ios::beg); @@ -304,68 +295,7 @@ static bool ExtractFromU8(const std::filesystem::path& archivePath, const char* return false; } - const uint32_t magic = BigEndian::Read32(data.data()); - if (magic != 0x55AA382Du) { // "U\xAA8-" - return false; - } - - const uint32_t rootOffset = BigEndian::Read32(data.data() + 0x04); - if (rootOffset + 0x0C > data.size()) { - return false; - } - - const U8Node* root = reinterpret_cast(data.data() + rootOffset); - const uint32_t nodeCount = BigEndian::Read32(reinterpret_cast(&root->size)); - const uint32_t nodeTableSize = nodeCount * sizeof(U8Node); - const uint32_t stringTableOffset = rootOffset + nodeTableSize; - if (stringTableOffset >= data.size()) { - return false; - } - - struct DirFrame { - std::string path; - uint32_t endIndex; - }; - std::vector stack; - stack.push_back({"", nodeCount}); - - for (uint32_t index = 1; index < nodeCount; ++index) { - while (!stack.empty() && index >= stack.back().endIndex) { - stack.pop_back(); - } - if (stack.empty()) { - break; - } - - const U8Node* node = reinterpret_cast(data.data() + rootOffset + index * sizeof(U8Node)); - const uint32_t typeAndName = BigEndian::Read32(reinterpret_cast(&node->typeAndNameOffset)); - const uint32_t nameOffset = typeAndName & 0x00FFFFFFu; - const bool isDir = (typeAndName >> 24) != 0; - if (stringTableOffset + nameOffset >= data.size()) { - return false; - } - - const char* name = reinterpret_cast(data.data() + stringTableOffset + nameOffset); - const std::string fullPath = stack.back().path + name; - - if (isDir) { - const uint32_t endIndex = BigEndian::Read32(reinterpret_cast(&node->size)); - stack.push_back({fullPath + "/", endIndex}); - continue; - } - - if (fullPath == targetName || std::strcmp(name, targetName) == 0) { - const uint32_t fileOffset = BigEndian::Read32(reinterpret_cast(&node->dataOffset)); - const uint32_t fileSize = BigEndian::Read32(reinterpret_cast(&node->size)); - if (fileOffset + fileSize > data.size()) { - return false; - } - outData.assign(data.begin() + fileOffset, data.begin() + fileOffset + fileSize); - return true; - } - } - - return false; + return RuntimeU8::Extract(data, targetName, outData); } // The only FaceLib resource the game ever seeds; every call site used to pass it diff --git a/runtime/src/hle/storage/nand_internal.h b/runtime/src/hle/storage/nand_internal.h index ef9caacc3..a87cf56d0 100644 --- a/runtime/src/hle/storage/nand_internal.h +++ b/runtime/src/hle/storage/nand_internal.h @@ -57,6 +57,7 @@ inline uint32_t NandTitleIdLo() { return ::GuestRegion::CurrentFacts().gameCode; // - never by inspecting the format string - and both levels print the same way; // the split records intent and is the single place to add filtering later. void LogNandError(const char* func, const char* fmt, ...); +void NandQueueIosCallback(uint32_t callbackPtr, int32_t result, uint32_t callbackArg); void LogNandWarning(const char* func, const char* fmt, ...); // An empty optional means continue opening normally; otherwise return the diff --git a/runtime/src/hle/storage/nand_isfs.cpp b/runtime/src/hle/storage/nand_isfs.cpp index 26f808d9f..937290255 100644 --- a/runtime/src/hle/storage/nand_isfs.cpp +++ b/runtime/src/hle/storage/nand_isfs.cpp @@ -3,6 +3,7 @@ // Shared state and helpers live in nand_internal.h. #include "nand_internal.h" +#include "host_file.h" #include "discord_presence.h" #include "runtime_log.h" @@ -652,20 +653,21 @@ extern "C" int32_t NAND_IOS_Ioctl_HLE( } case ISFS_IOCTL_CREATEFILE: { - if (!inBufPtr || inLen < 0x4c) { + if (!inBufPtr || inLen < 0x4c || !Memory::Contains(inBufPtr, 0x4c)) { return ISFS_EINVAL; } const char* path = (const char*)Memory::GetPointer(inBufPtr + 6); + if (!std::memchr(path, '\0', 64)) { + return ISFS_EINVAL; + } const std::filesystem::path hostPath = TranslateNandPath(path); + if (hostPath.empty()) { + return ISFS_EINVAL; + } CreateParentDirectories(hostPath); - // Create empty file - FILE* f = NandFopen(hostPath, "wb"); - if (f) { - std::fclose(f); - return ISFS_OK; - } - return ISFS_EIO; + const int result = RuntimeHostFile::CreateExclusive(hostPath); + return result == 0 ? ISFS_OK : result == EEXIST ? ISFS_EEXIST : ISFS_EIO; } case ISFS_IOCTL_GETFILESTATS: { diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 56bec9f7a..3026abd6e 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -1035,6 +1035,12 @@ LONG CALLBACK SehLogger(EXCEPTION_POINTERS* info) { info->ExceptionRecord->ExceptionInformation[0] != 0)) { return EXCEPTION_CONTINUE_EXECUTION; } + // First-chance software exceptions belong to their frame handlers. + if (info->ExceptionRecord->ExceptionCode == kCppExceptionCodeGcc || + info->ExceptionRecord->ExceptionCode == kCppExceptionCodeMsvc || + (info->ExceptionRecord->ExceptionCode & 0x20000000u) != 0) { + return EXCEPTION_CONTINUE_SEARCH; + } if (g_suppressSehReporting && g_sehJumpTarget) { g_sehLastExceptionCode = info->ExceptionRecord->ExceptionCode; g_sehLastExceptionAddress = reinterpret_cast(info->ExceptionRecord->ExceptionAddress); @@ -1049,24 +1055,12 @@ LONG CALLBACK SehLogger(EXCEPTION_POINTERS* info) { if (g_suppressSehReporting) { return EXCEPTION_CONTINUE_SEARCH; } - // Let C++ exceptions propagate to std::terminate so we can log their what(). - if (info->ExceptionRecord->ExceptionCode == kCppExceptionCodeGcc || - info->ExceptionRecord->ExceptionCode == kCppExceptionCodeMsvc) { - return EXCEPTION_CONTINUE_SEARCH; - } if (info->ExceptionRecord->ExceptionCode == 0x40010006 || // DBG_PRINTEXCEPTION_C info->ExceptionRecord->ExceptionCode == 0x4001000A || // DBG_PRINTEXCEPTION_WIDE_C (OutputDebugStringW) info->ExceptionRecord->ExceptionCode == 0x406D1388 || // SetThreadName info->ExceptionRecord->ExceptionCode == kAsanFatalAppExit) { // ASan reporting - let it print first return EXCEPTION_CONTINUE_SEARCH; } - // Software-raised exceptions (customer bit set) are used for internal control flow by - // system DLLs (e.g. msxml6 while mscms parses a display colour profile) and are caught - // by their own frame handlers. Only hardware faults are fatal at first chance; anything - // else that truly goes unhandled reaches UnhandledSehFilter. - if ((info->ExceptionRecord->ExceptionCode & 0x20000000u) != 0) { - return EXCEPTION_CONTINUE_SEARCH; - } return ReportFatalSehAndExit(info); } diff --git a/runtime/src/memory.cpp b/runtime/src/memory.cpp index 7225f4b51..ad8d3d33a 100644 --- a/runtime/src/memory.cpp +++ b/runtime/src/memory.cpp @@ -47,7 +47,9 @@ template uint8_t MemoryInline::ReadResolvedFallback(uint32_t); template uint16_t MemoryInline::ReadResolvedFallback(uint32_t); template uint32_t MemoryInline::ReadResolvedFallback(uint32_t); template uint64_t MemoryInline::ReadResolvedFallback(uint32_t); -float MemoryInline::ReadResolvedFallbackFloat32(uint32_t addr) { return Memory::ReadFloat32(addr); } +double MemoryInline::ReadResolvedFallbackFloat32(uint32_t addr) { + return PpcBitCastToDoubleInline(PpcConvertToDoubleBitsInline(Memory::Read32(addr))); +} double MemoryInline::ReadResolvedFallbackFloat64(uint32_t addr) { return Memory::ReadFloat64(addr); } template void MemoryInline::WriteResolvedFallback(uint32_t addr, T value) { @@ -88,10 +90,11 @@ uint64_t& NextDeferredReadToken() { bool RangesOverlap(uint32_t firstStart, size_t firstLength, uint32_t secondStart, size_t secondLength) { - const uint64_t firstEnd = static_cast(firstStart) + firstLength; - const uint64_t secondEnd = static_cast(secondStart) + secondLength; - return static_cast(firstStart) < secondEnd && - static_cast(secondStart) < firstEnd; + if (firstLength == 0 || secondLength == 0) + return false; + return firstStart <= secondStart + ? static_cast(secondStart - firstStart) < firstLength + : static_cast(firstStart - secondStart) < secondLength; } void RefreshDeferredReadPage(uint32_t page) { @@ -148,7 +151,7 @@ void ClearPageTable() { uint64_t Memory::RegisterDeferredRead(uint32_t addr, size_t length, DeferredReadCallback callback, void* user) { if (length == 0 || callback == nullptr || - static_cast(addr) + length > (uint64_t{1} << 32)) { + length > GuestFlat::kGuestSpaceSize - addr) { return 0; } @@ -189,6 +192,8 @@ void Memory::ClearDeferredReads() { } bool MemoryInline::ResolveDeferredReads(uint32_t addr, size_t length) { + if (length > GuestFlat::kGuestSpaceSize - addr) + return false; std::vector matches; std::vector affectedPages; { @@ -324,8 +329,7 @@ Region* TryResolveRegion(uint32_t address, size_t length) { const uint64_t base = region.config.baseAddress; const uint64_t limit = base + region.storageSize; const uint64_t addr = address; - const uint64_t end = addr + length; - if (addr >= base && end <= limit) { + if (addr >= base && addr <= limit && length <= limit - addr) { return ®ion; } } @@ -566,6 +570,11 @@ void Memory::Init(size_t mem1Size) { void Memory::Init(const Config& config) { std::lock_guard lock(RegionMutex()); + for (const auto& regionConfig : config.regions) { + if (regionConfig.sizeBytes > GuestFlat::kGuestSpaceSize - regionConfig.baseAddress) { + throw std::invalid_argument("Guest memory region exceeds the address space: " + regionConfig.name); + } + } auto& regions = Regions(); regions.clear(); ClearPageTable(); diff --git a/runtime/tests/audio_backend_ownership_tests.cpp b/runtime/tests/audio_backend_ownership_tests.cpp new file mode 100644 index 000000000..6d5ee5100 --- /dev/null +++ b/runtime/tests/audio_backend_ownership_tests.cpp @@ -0,0 +1,108 @@ +#include "audio_backend.h" +#include + +#include +#include + +struct SDL_AudioStream { int id; }; + +namespace { +int references = 1; +int initCalls = 0; +int quitCalls = 0; +int streams = 0; +int opens = 0; +bool failInit = false; +bool failOpen = false; +bool failResume = false; +bool failGain = false; +float gain = 1.0f; + +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} +} + +extern "C" const char* SDL_GetError() { return "injected failure"; } +extern "C" bool SDL_InitSubSystem(uint32_t flags) { + Require(flags == SDL_INIT_AUDIO, "unexpected subsystem"); + ++initCalls; + if (failInit) return false; + ++references; + return true; +} +extern "C" void SDL_QuitSubSystem(uint32_t) { --references; ++quitCalls; } +extern "C" SDL_AudioStream* SDL_OpenAudioDeviceStream( + SDL_AudioDeviceID, const SDL_AudioSpec* spec, SDL_AudioStreamCallback, void*) { + ++opens; + Require(references == 2 && spec->channels == 2, "stream must own an audio reference"); + if (failOpen) return nullptr; + ++streams; + return new SDL_AudioStream{opens}; +} +extern "C" void SDL_DestroyAudioStream(SDL_AudioStream* stream) { + Require(stream && streams == 1, "stream destroyed twice"); + --streams; + delete stream; +} +extern "C" bool SDL_ResumeAudioStreamDevice(SDL_AudioStream*) { return !failResume; } +extern "C" bool SDL_SetAudioStreamGain(SDL_AudioStream*, float value) { + gain = value; + return !failGain; +} +extern "C" int SDL_GetAudioStreamQueued(SDL_AudioStream*) { return 0; } +extern "C" bool SDL_PutAudioStreamData(SDL_AudioStream*, const void*, int) { return true; } + +int main() { + try { + auto& audio = AudioBackend::Instance(); + const int16_t samples[] = {1, 2}; + audio.Shutdown(); + Require(references == 1 && quitCalls == 0, "shutdown released somebody else's reference"); + failInit = true; + Require(!audio.Init(32000, 2), "failed subsystem init accepted"); + audio.Shutdown(); + Require(references == 1 && quitCalls == 0, "failed init acquired ownership"); + failInit = false; + failOpen = true; + Require(!audio.Init(32000, 2), "failed open accepted"); + audio.Shutdown(); + Require(references == 1 && quitCalls == 1, "failed open leaked subsystem reference"); + failOpen = false; + Require(audio.Init(32000, 2), "initial stream failed"); + const int initialized = initCalls; + const int opened = opens; + Require(audio.Init(32000, 2) && opens == opened, "same format reopened stream"); + Require(audio.Init(48000, 2) && initCalls == initialized && streams == 1, + "format replacement acquired another subsystem reference"); + failOpen = true; + Require(!audio.Init(32000, 2) && streams == 0, "failed replacement retained a stream"); + Require(!audio.PushSamplesLE16(samples, 2), "failed replacement stayed ready"); + failOpen = false; + Require(audio.Init(48000, 2) && streams == 1, "old format falsely reported ready"); + for (int failure = 0; failure < 2; ++failure) { + failResume = failure == 0; + failGain = failure == 1; + Require(!audio.Init(32000, 2) && streams == 0, "failed setup leaked a stream"); + failResume = failGain = false; + Require(audio.Init(48000, 2) && initCalls == initialized, + "retry lost or duplicated subsystem ownership"); + } + audio.SetMasterVolume(0.25f); + audio.SetSilenced(true); + Require(gain == 0.0f && audio.Init(32000, 2) && gain == 0.0f, + "format replacement lost silence"); + audio.SetSilenced(false); + Require(gain == 0.25f, "silence lost user volume"); + audio.Shutdown(); + audio.Shutdown(); + Require(streams == 0 && references == 1 && quitCalls == 2, "shutdown was unbalanced"); + Require(audio.Init(32000, 2), "shutdown/init failed"); + audio.Shutdown(); + Require(references == 1 && quitCalls == 3, "new lifetime was unbalanced"); + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } + return 0; +} diff --git a/runtime/tests/fiber_manager_tests.cpp b/runtime/tests/fiber_manager_tests.cpp new file mode 100644 index 000000000..e82bd14e2 --- /dev/null +++ b/runtime/tests/fiber_manager_tests.cpp @@ -0,0 +1,850 @@ +#include "abi_bridge.h" +#include "fiber_manager.h" +#include "guest_function_imports.h" +#include "hle/os/os_internal.h" +#include "hle_stubs.h" +#include "memory_slow_test_support.h" + +// Count native stack lifetimes while exercising the platform's real handoff. +#define HostContext FiberTestBackend +#include "../src/host_context.cpp" +#undef HostContext + +#include +#include +#include +#include +#include + +extern "C" void OSExitThread_HLE_801aa0f0(CpuContext*); +extern "C" void OSCancelThread_HLE_801aa1d4(CpuContext*); +extern "C" void OSJoinThread_HLE_801aa3ac(CpuContext*); +void NandQueueIosCallback(uint32_t, int32_t, uint32_t); +bool NandProcessPendingCallbacks(CpuContext*, int); +void LogNandError(const char*, const char*, ...) { std::abort(); } + +extern "C" const uint8_t kMkwGuestRegion = 0; +extern "C" uint32_t PPC_Cntlzw(uint32_t value) { return PPC_CntlzwInline(value); } + +namespace { +void Check(bool condition, const char* expression, int line) +{ + if (!condition) { + std::fprintf(stderr, "fiber regression at line %d: %s\n", line, expression); + std::_Exit(1); + } +} +#define CHECK(expression) Check((expression), #expression, __LINE__) + +std::unordered_set g_liveStacks; +size_t g_created = 0; +size_t g_destroyed = 0; +std::array g_memory{}; +CpuContext g_cpu{}; +std::unordered_map g_entries; +int g_unlockCalls = 0; +int g_stage = 0; +bool g_reuseReturns = false; + +constexpr uint32_t kMain = 0x80010000; +constexpr uint32_t kWorker = 0x80011000; +constexpr uint32_t kOther = 0x80012000; +constexpr uint32_t kJoiner = 0x80013000; +constexpr uint32_t kMutexWaiter = 0x80014000; +constexpr uint32_t kMutex = 0x80018000; +constexpr uint32_t kSecondMutex = 0x80018100; +constexpr uint32_t kOutput = 0x80019000; +constexpr uint32_t kObject = 0x8001a000; +constexpr uint32_t kVtable = 0x8001b000; +constexpr uint32_t kEntry = 0x80020000; +constexpr uint32_t kOtherEntry = 0x80020100; +constexpr uint32_t kCallback = 0x80020200; +constexpr uint32_t kExitValue = 0x1234abcd; + +using Manager = Fiber::GuestFiberManager; + +void Register(uint32_t address, void (*entry)(CpuContext*)) +{ + g_entries[address] = {entry}; +} + +void SetIdentity(uint32_t thread) +{ + Memory::Write32(kOSCurrentContextAddr, thread); + Memory::Write32(kOSRunningContextAddr, thread); + Memory::Write16(thread + kThreadStateOffset, kThreadStateRunning); +} + +void Switch(uint32_t thread, CpuContext* cpu) +{ + SetIdentity(thread); + Manager::SwitchToThread(thread, cpu); +} + +void SeedThread(uint32_t thread, uint32_t priority, uint32_t srr1 = 0x9032) +{ + std::memset(Memory::GetPointer(thread, 0x318), 0, 0x318); + Memory::Write32(thread + 4, 0x80100000 - thread % 0x10000); + Memory::Write32(thread + 8, 0x8038efa0); + Memory::Write32(thread + 0x34, 0x8038cc00); + Memory::Write32(thread + 0x84, 0x801aa0f0); + Memory::Write32(thread + 0x19c, srr1); + Memory::Write32(thread + kThreadPriorityOffset, priority); + Memory::Write32(thread + kThreadBasePriorityOffset, priority); + Memory::Write16(thread + kThreadStateOffset, kThreadStateReady); +} + +void Create(uint32_t thread, uint32_t entry, uint32_t argument, uint32_t priority, + uint32_t srr1 = 0x9032) +{ + SeedThread(thread, priority, srr1); + CHECK(Manager::CreateGuestFiber(thread, entry, argument, Memory::Read32(thread + 4))); +} + +void Enqueue(uint32_t thread) +{ + Memory::Write16(thread + kThreadStateOffset, kThreadStateReady); + const uint32_t priority = Memory::Read32(thread + kThreadPriorityOffset); + InsertThreadIntoQueueByPriority(kThreadQueueArrayAddr + priority * 8, thread, priority); + Manager::ResumeGuestThread(thread); +} + +void WaitOn(uint32_t thread, uint32_t queue) +{ + Memory::Write16(thread + kThreadStateOffset, kThreadStateWaiting); + Memory::Write32(thread + kThreadSuspendOffset, 1); + Memory::Write32(thread + kThreadQueueOffset, queue); + Memory::Write32(queue, thread); + Memory::Write32(queue + 4, thread); +} + +void UnlockAll(CpuContext* cpu) +{ + ++g_unlockCalls; + const uint32_t thread = cpu->gpr[3]; + for (uint32_t mutex = Memory::Read32(thread + kThreadMutexQueueOffset); mutex != 0;) { + const uint32_t next = Memory::Read32(mutex + kMutexThreadNextOffset); + CHECK(Memory::Read32(mutex + kMutexOwnerOffset) == thread); + Memory::Write32(thread + kThreadMutexQueueOffset, next); + if (next == 0) { + Memory::Write32(thread + kThreadMutexTailOffset, 0); + } else { + Memory::Write32(next + kMutexThreadPrevOffset, 0); + } + Memory::Write32(mutex + kMutexOwnerOffset, 0); + Memory::Write32(mutex + kMutexCountOffset, 0); + cpu->gpr[3] = mutex; + OSWakeupThread_HLE_801aaaa4(cpu); + mutex = next; + } + Memory::Write32(thread + kThreadMutexQueueOffset, 0); + Memory::Write32(thread + kThreadMutexTailOffset, 0); +} + +struct Fixture { + Fixture() + { + CHECK(g_liveStacks.empty()); + g_memory.fill(0); + g_cpu = {}; + g_cpu.gpr[1] = 0x80100000; + g_cpu.gpr[29] = 0x29292929; + g_cpu.gpr[31] = 0x31313131; + g_entries.clear(); + Register(0x801a8088, UnlockAll); + g_unlockCalls = 0; + g_stage = 0; + g_currentCpuContext = &g_cpu; + MkwApplyHostNiMode(0); + g_interrupts_enabled.store(true); + SeedThread(kMain, 10); + SetIdentity(kMain); + Manager::Initialize(); + const auto generation = Manager::GetThreadGeneration(kMain); + CHECK(Manager::RegisterMainThreadAsFiber(kMain, &g_cpu)); + CHECK(Manager::GetThreadGeneration(kMain) == generation); + } + + ~Fixture() + { + Manager::Shutdown(); + CHECK(g_liveStacks.empty()); + CHECK(g_created == g_destroyed); + g_currentCpuContext = nullptr; + } +}; + +void ScratchWorker(CpuContext* cpu) +{ + CHECK(cpu == &GetPersistentCpuContext()); + CHECK(TryGetCpuContext() == cpu); + CHECK(cpu->gpr[3] == 73); + CHECK(OS_HLE_InterruptsEnabled()); + CHECK((Memory::Read16(kWorker + 0x1a2) & 2) != 0); + cpu->gpr[29] = 0xabcdef29; + cpu->fpscr = 4; + MkwApplyHostNiMode(cpu->fpscr); + { + CpuContext scratch = *cpu; + scratch.gpr[29] = 0xcafe0029; + scratch.fpscr = 0; + CpuContextScope scope(&scratch); + OS__DisableInterrupts_801a65ac(); + Switch(kMain, &scratch); + CHECK(TryGetCpuContext() == &scratch); + CHECK(scratch.gpr[29] == 0xcafe0029); + CHECK(cpu->gpr[29] == 0xabcdef29); + CHECK(!OS_HLE_InterruptsEnabled()); + CHECK(!g_mkwHostNiActive); + scratch.gpr[31] = 0xfeed0031; + CHECK(TryGetCpuContext()->gpr[31] == 0xfeed0031); + } + CHECK(TryGetCpuContext() == cpu); + CHECK(g_mkwHostNiActive); + cpu->gpr[3] = kExitValue; +} + +void TestNestedScratch() +{ + Fixture fixture; + Register(kEntry, ScratchWorker); + Create(kWorker, kEntry, 73, 3); + { + CpuContext outer = g_cpu; + outer.gpr[29] = 0xaaaa0029; + outer.fpscr = 4; + CpuContextScope outerScope(&outer); + { + CpuContext inner = outer; + inner.gpr[29] = 0xbbbb0029; + inner.fpscr = 0; + CpuContextScope innerScope(&inner); + OS__DisableInterrupts_801a65ac(); + Switch(kWorker, &inner); + CHECK(TryGetCpuContext() == &inner); + CHECK(inner.gpr[29] == 0xbbbb0029); + CHECK(g_cpu.gpr[29] == 0x29292929); + CHECK(g_cpu.gpr[31] == 0x31313131); + CHECK(!OS_HLE_InterruptsEnabled()); + CHECK((Memory::Read16(kMain + 0x1a2) & 2) == 0); + CHECK(!g_mkwHostNiActive); + } + CHECK(TryGetCpuContext() == &outer); + CHECK(outer.gpr[29] == 0xaaaa0029); + CHECK(g_mkwHostNiActive); + } + CHECK(TryGetCpuContext() == &g_cpu); + CHECK(!g_mkwHostNiActive); + OS__RestoreInterrupts_801a65d4(1); + Enqueue(kMain); + Switch(kWorker, &g_cpu); + CHECK(TryGetCpuContext() == &g_cpu); + CHECK(g_cpu.gpr[29] == 0x29292929); + CHECK(OS_HLE_InterruptsEnabled()); + CHECK(!g_mkwHostNiActive); + CHECK(Manager::IsTerminated(kWorker)); + CHECK(g_liveStacks.empty()); +} + +void SwitchCallback(CpuContext* cpu) +{ + CHECK(cpu != &GetPersistentCpuContext()); + CHECK(TryGetCpuContext() == cpu); + CHECK(cpu->gpr[3] == kWorker || cpu->gpr[3] == kOther); + CHECK(cpu->gpr[4] == kOther || cpu->gpr[4] == kMain); + cpu->gpr[3] = 0xcccc0003; + cpu->gpr[4] = 0xcccc0004; + cpu->gpr[29] = 0xcccc0029; + cpu->gpr[31] = 0xcccc0031; + cpu->fpscr = 4; + MkwApplyHostNiMode(4); +} + +void ReturningWorker(CpuContext* cpu) +{ + CHECK(TryGetCpuContext() == &GetPersistentCpuContext()); + CHECK(cpu->gpr[3] == 41); + CHECK(OS_HLE_InterruptsEnabled()); + cpu->gpr[29] = 0x11110029; + g_stage = 1; + cpu->gpr[3] = kExitValue; +} + +void FollowingWorker(CpuContext* cpu) +{ + CHECK(g_stage == 1); + CHECK(Manager::IsTerminated(kWorker)); + CHECK(Memory::Read32(kWorker + kThreadExitValueOffset) == kExitValue); + CHECK(Memory::Read32(kMutex + kMutexOwnerOffset) == 0); + CHECK(Memory::Read32(kMutex + kMutexCountOffset) == 0); + CHECK(Memory::Read16(kJoiner + kThreadStateOffset) == kThreadStateReady); + CHECK(Memory::Read16(kMutexWaiter + kThreadStateOffset) == kThreadStateReady); + CHECK(Memory::Read16(kMain + kThreadStateOffset) == kThreadStateWaiting); + CHECK(!OS_HLE_InterruptsEnabled()); + CHECK(!g_mkwHostNiActive); + CHECK(cpu->gpr[29] != 0xcccc0029); + g_stage = 2; + Enqueue(kMain); + cpu->gpr[3] = 0x55; +} + +void TestExitSchedulingAndCleanup() +{ + Fixture fixture; + Register(kEntry, ReturningWorker); + Register(kOtherEntry, FollowingWorker); + Register(kCallback, SwitchCallback); + Memory::Write32(kSwitchThreadCallbackPtrAddr, kCallback); + Create(kWorker, kEntry, 41, 3); + Create(kOther, kOtherEntry, 0, 5, 0x1032); + SeedThread(kJoiner, 8); + SeedThread(kMutexWaiter, 8); + WaitOn(kJoiner, kWorker + kThreadJoinQueueOffset); + WaitOn(kMutexWaiter, kMutex); + Memory::Write32(kMutex + kMutexOwnerOffset, kWorker); + Memory::Write32(kMutex + kMutexCountOffset, 2); + Memory::Write32(kWorker + kThreadMutexQueueOffset, kMutex); + Memory::Write32(kWorker + kThreadMutexTailOffset, kMutex); + Memory::Write16(kOther + kThreadAttrOffset, 1); + Memory::Write32(kThreadListHeadAddr, kMain); + Memory::Write32(kThreadListTailAddr, kOther); + Memory::Write32(kMain + kThreadListNextOffset, kWorker); + Memory::Write32(kWorker + kThreadListPrevOffset, kMain); + Memory::Write32(kWorker + kThreadListNextOffset, kOther); + Memory::Write32(kOther + kThreadListPrevOffset, kWorker); + Memory::Write16(kMain + kThreadStateOffset, kThreadStateWaiting); + Enqueue(kOther); + Switch(kWorker, &g_cpu); + CHECK(g_stage == 2); + CHECK(g_unlockCalls == 2); + CHECK(Memory::Read16(kOther + kThreadStateOffset) == 0); + CHECK(Memory::Read32(kThreadListTailAddr) == kWorker); + CHECK(Memory::Read32(kWorker + kThreadListNextOffset) == 0); + CHECK(g_cpu.gpr[29] == 0x29292929); + CHECK(g_cpu.gpr[31] == 0x31313131); + CHECK(TryGetCpuContext() == &g_cpu); + CHECK(OS_HLE_InterruptsEnabled()); + CHECK(!g_mkwHostNiActive); + CHECK(g_liveStacks.empty()); + g_cpu.gpr[3] = kWorker; + g_cpu.gpr[4] = kOutput; + OSJoinThread_HLE_801aa3ac(&g_cpu); + CHECK(g_cpu.gpr[3] == 1); + CHECK(Memory::Read32(kOutput) == kExitValue); + CHECK(Memory::Read16(kWorker + kThreadStateOffset) == 0); +} + +void FirstMutexWaiter(CpuContext* cpu) +{ + CHECK(Memory::Read32(kSchedulerIdleFlagAddr) == 0); + CHECK(Memory::Read32(kMutex + kMutexOwnerOffset) == 0); + CHECK(Memory::Read32(kSecondMutex + kMutexOwnerOffset) == kWorker); + CHECK(Memory::Read32(kWorker + kThreadMutexQueueOffset) == kSecondMutex); + CHECK(Memory::Read32(kWorker + kThreadJoinQueueOffset) == kJoiner); + CHECK(Memory::Read16(kJoiner + kThreadStateOffset) == kThreadStateWaiting); + CHECK(Memory::Read16(kMutexWaiter + kThreadStateOffset) == kThreadStateWaiting); + CHECK(Manager::IsTerminated(kWorker)); + CHECK(g_stage == 1); + ++g_stage; + if (cpu->gpr[3] == 0) Enqueue(kMain); + cpu->gpr[3] = kExitValue; +} + +void SeedCleanupWaiters(bool cancelOther) +{ + Register(kEntry, ReturningWorker); + Register(kOtherEntry, FirstMutexWaiter); + Create(kWorker, kEntry, 41, 3); + Create(kOther, kOtherEntry, cancelOther ? 1 : 0, 1); + SeedThread(kJoiner, 2); + SeedThread(kMutexWaiter, 4); + WaitOn(kOther, kMutex); + WaitOn(kMutexWaiter, kSecondMutex); + WaitOn(kJoiner, kWorker + kThreadJoinQueueOffset); + for (uint32_t thread : {kOther, kJoiner, kMutexWaiter}) { + Memory::Write32(thread + kThreadSuspendOffset, 0); + } + Memory::Write32(kMutex + kMutexOwnerOffset, kWorker); + Memory::Write32(kMutex + kMutexCountOffset, 1); + Memory::Write32(kMutex + kMutexThreadNextOffset, kSecondMutex); + Memory::Write32(kSecondMutex + kMutexOwnerOffset, kWorker); + Memory::Write32(kSecondMutex + kMutexCountOffset, 1); + Memory::Write32(kSecondMutex + kMutexThreadPrevOffset, kMutex); + Memory::Write32(kWorker + kThreadMutexQueueOffset, kMutex); + Memory::Write32(kWorker + kThreadMutexTailOffset, kSecondMutex); +} + +void TestExitPreemptsAtFirstWakeup() +{ + Fixture fixture; + SeedCleanupWaiters(false); + Memory::Write16(kMain + kThreadStateOffset, kThreadStateWaiting); + Switch(kWorker, &g_cpu); + CHECK(g_stage == 2); + CHECK(g_unlockCalls == 2); + CHECK(Memory::Read32(kWorker + kThreadExitValueOffset) == kExitValue); + CHECK(Memory::Read32(kSecondMutex + kMutexOwnerOffset) == kWorker); + CHECK(Memory::Read16(kJoiner + kThreadStateOffset) == kThreadStateWaiting); + CHECK(Memory::Read16(kMutexWaiter + kThreadStateOffset) == kThreadStateWaiting); + CHECK(g_liveStacks.empty()); +} + +void TestCancelOtherPreemptsDuringCleanup() +{ + Fixture fixture; + SeedCleanupWaiters(true); + Memory::Write32(kJoiner + kThreadSuspendOffset, 1); + Memory::Write32(kMutexWaiter + kThreadSuspendOffset, 1); + Memory::Write32(kWorker + kThreadSuspendOffset, 1); + Memory::Write32(kWorker + kThreadExitValueOffset, 0x5678); + g_stage = 1; + g_cpu.gpr[3] = kWorker; + OSCancelThread_HLE_801aa1d4(&g_cpu); + CHECK(g_stage == 2); + CHECK(g_unlockCalls == 2); + CHECK(Memory::Read32(kSecondMutex + kMutexOwnerOffset) == 0); + CHECK(Memory::Read32(kWorker + kThreadMutexQueueOffset) == 0); + CHECK(Memory::Read32(kWorker + kThreadJoinQueueOffset) == 0); + CHECK(Memory::Read32(kWorker + kThreadExitValueOffset) == 0x5678); + CHECK(Memory::Read16(kJoiner + kThreadStateOffset) == kThreadStateReady); + CHECK(Memory::Read16(kMutexWaiter + kThreadStateOffset) == kThreadStateReady); + CHECK(Manager::GetCurrentGuestThread() == kMain); + CHECK(OS_HLE_InterruptsEnabled()); + CHECK(g_liveStacks.empty()); +} + +void ExplicitExit(CpuContext* cpu) +{ + cpu->gpr[3] = kExitValue; + OSExitThread_HLE_801aa0f0(cpu); + CHECK(false); +} + +void TestRepeatedExplicitExits() +{ + Fixture fixture; + Register(kEntry, ExplicitExit); + const size_t before = g_destroyed; + for (int i = 0; i < 32; ++i) { + Create(kWorker, kEntry, 0, 3); + Enqueue(kMain); + Switch(kWorker, &g_cpu); + CHECK(g_liveStacks.empty()); + CHECK(g_destroyed == before + i + 1); + CHECK(Manager::IsTerminated(kWorker)); + CHECK(Manager::GetFiber(kWorker)->fiber == nullptr); + CHECK(Memory::Read32(kWorker + kThreadExitValueOffset) == kExitValue); + } +} + +void TestInactiveReplacementAndCancel() +{ + Fixture fixture; + Register(kEntry, ExplicitExit); + const size_t before = g_destroyed; + Create(kWorker, kEntry, 0, 3); + const auto originalGeneration = Manager::GetThreadGeneration(kWorker); + CHECK(Manager::CreateGuestFiber(kWorker, kEntry, 0, 0x80101000)); + const auto replacedGeneration = Manager::GetThreadGeneration(kWorker); + CHECK(replacedGeneration != originalGeneration); + CHECK(g_destroyed == before + 1); + CHECK(g_liveStacks.size() == 1); + Memory::Write32(kSchedulerIdleFlagAddr, 3); + g_cpu.gpr[3] = kWorker; + OSCancelThread_HLE_801aa1d4(&g_cpu); + CHECK(g_destroyed == before + 2); + CHECK(g_liveStacks.empty()); + CHECK(Manager::IsTerminated(kWorker)); + CHECK(Manager::GetThreadGeneration(kWorker) != replacedGeneration); + CHECK(Memory::Read32(kSchedulerIdleFlagAddr) == 3); + CHECK(Manager::GetCurrentGuestThread() == kMain); +} + +void ExitInsideCallback(CpuContext* cpu) +{ + CpuContext scratch = *cpu; + scratch.gpr[29] = 0xdead0029; + scratch.gpr[3] = kExitValue; + CpuContextScope scope(&scratch); + OSExitThread_HLE_801aa0f0(&scratch); + CHECK(false); +} + +void AfterCallbackOwnerExit(CpuContext* cpu) +{ + CHECK(g_liveStacks.size() == 1); + CHECK(TryGetCpuContext() == cpu); + CHECK(cpu == &GetPersistentCpuContext()); + CHECK(cpu->gpr[29] != 0xdead0029); + TryGetCpuContext()->gpr[31] = 0x56780031; + CHECK(cpu->gpr[31] == 0x56780031); + g_stage = 1; + Enqueue(kMain); + cpu->gpr[3] = kExitValue; +} + +void RecordNandCallback(CpuContext* cpu) +{ + CHECK(cpu->gpr[3] == 23 && cpu->gpr[4] == 42); + CHECK(!NandProcessPendingCallbacks(cpu, 1)); + ++g_stage; +} + +void DrainNandAndExit(CpuContext* cpu) +{ + NandProcessPendingCallbacks(cpu, 1); + CHECK(false); +} + +void DrainAfterOwnerExit(CpuContext* cpu) +{ + CHECK(NandProcessPendingCallbacks(cpu, 1)); + CHECK(g_stage == 1); + CHECK(!NandProcessPendingCallbacks(cpu, 1)); + Enqueue(kMain); +} + +void TestNandCallbackOwnerExit() +{ + Fixture fixture; + Register(kEntry, DrainNandAndExit); + Register(kOtherEntry, DrainAfterOwnerExit); + Register(kCallback, ExitInsideCallback); + Register(kCallback + 4, RecordNandCallback); + NandQueueIosCallback(kCallback, 0, 0); + NandQueueIosCallback(kCallback + 4, 23, 42); + Create(kWorker, kEntry, 0, 3); + Create(kOther, kOtherEntry, 0, 5); + Memory::Write16(kMain + kThreadStateOffset, kThreadStateWaiting); + Enqueue(kOther); + Switch(kWorker, &g_cpu); + CHECK(g_stage == 1 && g_liveStacks.empty()); +} + +void YieldNandCallback(CpuContext* cpu) +{ + CHECK(!NandProcessPendingCallbacks(cpu, 1)); + Switch(kMain, cpu); + CHECK(!NandProcessPendingCallbacks(cpu, 1)); + CHECK(g_stage == 1); + ++g_stage; +} + +void DrainYieldingNand(CpuContext* cpu) +{ + CHECK(NandProcessPendingCallbacks(cpu, 1)); + CHECK(g_stage == 2); + Enqueue(kMain); +} + +void TestYieldingNandCallback() +{ + Fixture fixture; + Register(kEntry, DrainYieldingNand); + Register(kCallback, YieldNandCallback); + Register(kCallback + 4, RecordNandCallback); + NandQueueIosCallback(kCallback, 0, 0); + NandQueueIosCallback(kCallback + 4, 23, 42); + Create(kWorker, kEntry, 0, 3); + Switch(kWorker, &g_cpu); + CHECK(g_stage == 0); + CHECK(NandProcessPendingCallbacks(&g_cpu, 1)); + CHECK(g_stage == 1); + Switch(kWorker, &g_cpu); + CHECK(g_stage == 2 && g_liveStacks.empty()); +} + +void TestCallbackOwnerExit() +{ + Fixture fixture; + Register(kEntry, ExitInsideCallback); + Register(kOtherEntry, AfterCallbackOwnerExit); + Create(kWorker, kEntry, 0, 3); + Create(kOther, kOtherEntry, 0, 5); + Memory::Write16(kMain + kThreadStateOffset, kThreadStateWaiting); + Enqueue(kOther); + Switch(kWorker, &g_cpu); + CHECK(g_stage == 1); + CHECK(TryGetCpuContext() == &g_cpu); + CHECK(g_cpu.gpr[29] == 0x29292929); + CHECK(g_cpu.gpr[31] == 0x31313131); + CHECK(g_liveStacks.empty()); +} + +void Replacement(CpuContext* cpu) +{ + CHECK(cpu->gpr[3] == 91); + CHECK(TryGetCpuContext() == &GetPersistentCpuContext()); + g_stage = 2; + cpu->gpr[3] = kExitValue; +} + +void ReusingWorker(CpuContext* cpu) +{ + CHECK(Manager::CreateGuestFiber(kWorker, kOtherEntry, 91, 0x80101000)); + CHECK(!g_liveStacks.empty()); + CHECK(!Manager::IsTerminated(kWorker)); + CHECK(Manager::GetFiber(kWorker)->cpuContext.gpr[3] == 91); + g_stage = 1; + cpu->gpr[29] = 0xdead0029; + if (!g_reuseReturns) { + Switch(kMain, cpu); + CHECK(false); + } +} + +void TestActiveReuse(bool returnFromEntry) +{ + Fixture fixture; + g_reuseReturns = returnFromEntry; + Register(kEntry, ReusingWorker); + Register(kOtherEntry, Replacement); + Create(kWorker, kEntry, 0, 3); + const size_t before = g_destroyed; + Switch(kWorker, &g_cpu); + CHECK(g_stage == 1); + CHECK(g_destroyed == before + 1); + CHECK(!Manager::IsTerminated(kWorker)); + CHECK(Manager::GetFiber(kWorker)->cpuContext.gpr[3] == 91); + CHECK(Manager::GetFiber(kWorker)->cpuContext.gpr[29] == 0); + CHECK(g_cpu.gpr[29] == 0x29292929); + SetIdentity(kMain); + Enqueue(kMain); + Switch(kWorker, &g_cpu); + CHECK(g_stage == 2); + CHECK(g_destroyed == before + 2); + CHECK(g_liveStacks.empty()); +} + +void DeferredEntry(CpuContext* cpu) +{ + CHECK(cpu == &GetPersistentCpuContext()); + CHECK(TryGetCpuContext() == cpu); + CHECK(cpu->gpr[3] == kObject); + CHECK(OS_HLE_InterruptsEnabled()); + g_stage = 1; + cpu->gpr[3] = kExitValue; +} + +void TestDirectSchedulerYield() +{ + Fixture fixture; + Register(0x8024373c, DeferredEntry); + Create(kWorker, 0x8024373c, kObject, 3); + OS__DisableInterrupts_801a65ac(); + Switch(kWorker, &g_cpu); + CHECK(g_stage == 0); + CHECK(TryGetCpuContext() == &g_cpu); + CHECK(g_cpu.gpr[29] == 0x29292929); + CHECK(!OS_HLE_InterruptsEnabled()); + CHECK((Memory::Read16(kWorker + 0x1a2) & 2) != 0); + Memory::Write32(kObject, kVtable); + Memory::Write32(kVtable + 0xc, kEntry); + SetIdentity(kMain); + Enqueue(kMain); + Switch(kWorker, &g_cpu); + CHECK(g_stage == 1); + CHECK(!OS_HLE_InterruptsEnabled()); + CHECK(TryGetCpuContext() == &g_cpu); + CHECK(g_liveStacks.empty()); +} + +void DisabledSchedulerEntry(CpuContext* cpu) +{ + Memory::Write32(kSchedulerIdleFlagAddr, 1); + cpu->gpr[3] = kExitValue; +} + +void DisabledSchedulerExplicitEntry(CpuContext* cpu) +{ + DisabledSchedulerEntry(cpu); + OSExitThread_HLE_801aa0f0(cpu); +} + +void TestDisabledSchedulerFallback(bool explicitExit) +{ + Fixture fixture; + Register(kEntry, explicitExit ? DisabledSchedulerExplicitEntry : DisabledSchedulerEntry); + Create(kWorker, kEntry, 0, 3); + Switch(kWorker, &g_cpu); + CHECK(g_unlockCalls == 1); + CHECK(Manager::IsTerminated(kWorker)); + CHECK(Memory::Read32(kWorker + kThreadExitValueOffset) == kExitValue); + CHECK(Memory::Read32(kOSRunningContextAddr) == 0); + CHECK(Memory::Read32(kOSCurrentContextAddr) == 0); + CHECK(TryGetCpuContext() == &g_cpu); + CHECK(OS_HLE_InterruptsEnabled()); + CHECK(g_liveStacks.empty()); +} + +void TestNullAmbientAndSameFiber() +{ + Fixture fixture; + Register(kEntry, ExplicitExit); + Create(kWorker, kEntry, 0, 3, 0x1032); + g_currentCpuContext = nullptr; + MkwApplyHostNiMode(4); + const uint32_t control = MkwGetHostFpControl(); + Manager::SwitchToThread(kMain, &g_cpu); + CHECK(g_currentCpuContext == nullptr); + CHECK(MkwGetHostFpControl() == control); + Enqueue(kMain); + Switch(kWorker, &g_cpu); + CHECK(g_currentCpuContext == nullptr); + CHECK(MkwGetHostFpControl() == control); + CHECK(g_mkwHostNiActive); + CHECK(OS_HLE_InterruptsEnabled()); + CHECK(g_liveStacks.empty()); +} +} // namespace + +namespace HostContext { +using Handle = void*; +using Entry = void (*)(void*); +bool InitializeScheduler(Handle* scheduler) { return FiberTestBackend::InitializeScheduler(scheduler); } +void ShutdownScheduler(Handle scheduler) { FiberTestBackend::ShutdownScheduler(scheduler); } +Handle Create(size_t size, Entry entry, void* argument) +{ + Handle handle = FiberTestBackend::Create(size, entry, argument); + if (handle) { + CHECK(g_liveStacks.insert(handle).second); + ++g_created; + } + return handle; +} +bool IsCurrent(Handle handle) { return FiberTestBackend::IsCurrent(handle); } +void Destroy(Handle handle) +{ + CHECK(!IsCurrent(handle)); + CHECK(g_liveStacks.erase(handle) == 1); + ++g_destroyed; + FiberTestBackend::Destroy(handle); +} +void Switch(Handle target) { FiberTestBackend::Switch(target); } +} // namespace HostContext + +CpuContext& GetPersistentCpuContext() { return g_cpu; } +const TranslatedFunctionInfo* TranslatedFunctionRegistry::FindByAddressPtr(uint32_t address) +{ + const auto it = g_entries.find(address); + return it == g_entries.end() ? nullptr : &it->second; +} +void InvokeIndirectCpu(uint32_t address, CpuContext* cpu) +{ + const auto* info = TranslatedFunctionRegistry::FindByAddressPtr(address); + CHECK(info != nullptr); + if (TryGetCpuContext() == cpu) { + info->entry(cpu); + } else { + CpuContextScope scope(cpu); + info->entry(cpu); + } +} +[[noreturn]] void InvokeIndirectJump(uint32_t, CpuContext*) { std::abort(); } +void ShowRuntimeFatalPopup(std::string_view, std::string_view) noexcept { std::abort(); } + +Memory::AccessViolation::AccessViolation(uint32_t address, size_t length, std::string_view reason) + : std::runtime_error(std::string(reason)), address_(address), length_(length), reason_(reason) {} +bool Memory::Contains(uint32_t address, size_t length) +{ + return address >= 0x80000000 && address - 0x80000000 < g_memory.size() && + length <= g_memory.size() - (address - 0x80000000); +} +uint8_t* Memory::GetPointer(uint32_t address, size_t length) +{ + if (!Contains(address, length)) throw AccessViolation(address, length, "test memory bounds"); + return g_memory.data() + address - 0x80000000; +} +uint8_t* Memory::GetPointer(uint32_t address) { return GetPointer(address, 1); } +uint16_t MemoryInline::Read16Slow(uint32_t address) +{ + const uint8_t* p = Memory::GetPointer(address, 2); + return static_cast((p[0] << 8) | p[1]); +} +uint32_t MemoryInline::Read32Slow(uint32_t address) +{ + const uint8_t* p = Memory::GetPointer(address, 4); + return (uint32_t(p[0]) << 24) | (uint32_t(p[1]) << 16) | (uint32_t(p[2]) << 8) | p[3]; +} +void MemoryInline::Write16Slow(uint32_t address, uint16_t value) +{ + uint8_t* p = Memory::GetPointer(address, 2); + p[0] = static_cast(value >> 8); + p[1] = static_cast(value); +} +void MemoryInline::Write32Slow(uint32_t address, uint32_t value) +{ + uint8_t* p = Memory::GetPointer(address, 4); + p[0] = static_cast(value >> 24); + p[1] = static_cast(value >> 16); + p[2] = static_cast(value >> 8); + p[3] = static_cast(value); +} + +namespace OsHleInternal { +std::atomic g_interrupts_enabled{true}; +bool ProcessSleepTimers(CpuContext*) { return false; } +bool ProcessAlarmQueue(CpuContext*, int) { return false; } +void CancelSleepTimer(uint32_t) {} +void ClearOutstandingPark(uint32_t) {} +} // namespace OsHleInternal + +bool OS_HLE_InterruptsEnabled() noexcept { return g_interrupts_enabled.load(); } +void OS_HLE_RestoreInterruptMirror(bool enabled) noexcept { g_interrupts_enabled.store(enabled); } +extern "C" int32_t OS__RestoreInterrupts_801a65d4(int32_t level) +{ + const bool previous = g_interrupts_enabled.exchange(level != 0); + const uint32_t current = Memory::Read32(kOSCurrentContextAddr); + if (current != 0) { + const uint16_t flags = Memory::Read16(current + 0x1a2); + Memory::Write16(current + 0x1a2, static_cast(level ? flags | 2 : flags & ~2)); + } + return previous ? 1 : 0; +} +extern "C" int32_t OS__DisableInterrupts_801a65ac() { return OS__RestoreInterrupts_801a65d4(0); } +extern "C" int32_t OS__EnableInterrupts_801a65c0() { return OS__RestoreInterrupts_801a65d4(1); } +extern "C" void OS__SetCurrentContext_801a1e70(uint32_t context) +{ + Memory::Write32(kOSCurrentContextAddr, context); +} +extern "C" void OS__ClearContext_801a2098(uint32_t context) +{ + Memory::Write16(context + 0x1a0, 0); + Memory::Write16(context + 0x1a2, 0); +} +extern "C" [[noreturn]] void OS__LoadContext_801a1f58(CpuContext*) { std::abort(); } +extern "C" void OSSleepThread_HLE_801aa9b8(CpuContext*) { std::abort(); } + +namespace GuestFunctions { +Entry BaseTranslation(uint32_t address) +{ + CHECK(address == 0x801a1ed8); + return +[](CpuContext* cpu) { cpu->gpr[3] = 0; }; +} +} // namespace GuestFunctions +void VI_HLE_ForceRetrace(CpuContext*) {} +void VI_HLE_PollRetrace(CpuContext*) {} +void VI_HLE_WaitForNextRetracePoll() { std::abort(); } +bool VI_HLE_IsAdvancingRetrace() { return false; } +void Audio_HLE_Poll(CpuContext*) {} + +int main() +{ + const auto previousMainGeneration = Manager::GetThreadGeneration(kMain); + TestNestedScratch(); + CHECK(Manager::GetThreadGeneration(kMain) != previousMainGeneration); + TestExitSchedulingAndCleanup(); + TestExitPreemptsAtFirstWakeup(); + TestCancelOtherPreemptsDuringCleanup(); + TestRepeatedExplicitExits(); + TestInactiveReplacementAndCancel(); + TestCallbackOwnerExit(); + TestNandCallbackOwnerExit(); + TestYieldingNandCallback(); + TestActiveReuse(false); + TestActiveReuse(true); + TestDirectSchedulerYield(); + TestDisabledSchedulerFallback(false); + TestDisabledSchedulerFallback(true); + TestNullAmbientAndSameFiber(); +} diff --git a/runtime/tests/fiber_support/abi_bridge.h b/runtime/tests/fiber_support/abi_bridge.h new file mode 100644 index 000000000..42ba94320 --- /dev/null +++ b/runtime/tests/fiber_support/abi_bridge.h @@ -0,0 +1,22 @@ +#pragma once + +// Test dispatch seam; the scheduler and fiber implementations are compiled unchanged. +#include "ppc_runtime.h" +#include "guest_abi/guest_region.h" + +#include + +struct TranslatedFunctionInfo { + void (*entry)(CpuContext*); +}; + +class TranslatedFunctionRegistry { +public: + static const TranslatedFunctionInfo* FindByAddressPtr(uint32_t address); +}; + +CpuContext& GetPersistentCpuContext(); +void InvokeIndirectCpu(uint32_t address, CpuContext* cpu); +[[noreturn]] void InvokeIndirectJump(uint32_t address, CpuContext* cpu); + +#define REGISTER_NATIVE_FUNCTION(address, fn) diff --git a/runtime/tests/fiber_support/hle_stubs.h b/runtime/tests/fiber_support/hle_stubs.h new file mode 100644 index 000000000..e46baac3a --- /dev/null +++ b/runtime/tests/fiber_support/hle_stubs.h @@ -0,0 +1,12 @@ +#pragma once + +#include "abi_bridge.h" + +void VI_HLE_ForceRetrace(CpuContext*); +void VI_HLE_PollRetrace(CpuContext*); +void VI_HLE_WaitForNextRetracePoll(); +bool VI_HLE_IsAdvancingRetrace(); +void Audio_HLE_Poll(CpuContext*); +bool OS_HLE_InterruptsEnabled() noexcept; + +#define PPC_NATIVE_OVERRIDE_VOID(address, name, args, call) diff --git a/runtime/tests/gx_fifo_invalidation_tests.cpp b/runtime/tests/gx_fifo_invalidation_tests.cpp new file mode 100644 index 000000000..cf715f82f --- /dev/null +++ b/runtime/tests/gx_fifo_invalidation_tests.cpp @@ -0,0 +1,198 @@ +#include "gx_test_common.hpp" +#include "../src/hle/gx/gx_internal.h" +#include "isa/big_endian.h" +#include "memory_slow_test_support.h" + +#include + +namespace aurora::gfx::testing { +extern std::vector storageData; +} + +namespace { +constexpr uint32_t kGuestBase = 0x80001000; +std::array g_guestBytes{}; +std::vector> g_deferredBursts; +} + +// Host seams; the runtime parser and Aurora encoder/decoder are linked unchanged. +extern "C" const uint8_t kMkwGuestRegion = 0; +GxDisplayListState g_dlRecordState; +std::atomic_bool g_auroraFrameActive{false}; +std::atomic_bool g_auroraFrameHadWork{false}; +bool g_alphaCompareValid = false; + +namespace GxWorker { +namespace detail { +std::atomic g_active{false}; +constinit thread_local bool t_onWorker = false; +constinit thread_local int t_inlineDepth = 0; +} +void FifoBurst(const uint8_t* bytes, uint32_t size) { g_deferredBursts.emplace_back(bytes, bytes + size); } +} + +bool Memory::Contains(uint32_t address, size_t length) { + return address >= kGuestBase && address - kGuestBase <= g_guestBytes.size() && + length <= g_guestBytes.size() - (address - kGuestBase); +} +uint8_t MemoryInline::Read8Slow(uint32_t address) { return g_guestBytes.at(address - kGuestBase); } +uint16_t MemoryInline::Read16Slow(uint32_t address) { return BigEndian::Read16(&g_guestBytes.at(address - kGuestBase)); } +uint32_t MemoryInline::Read32Slow(uint32_t address) { return BigEndian::Read32(&g_guestBytes.at(address - kGuestBase)); } +void MemoryInline::Write8Slow(uint32_t address, uint8_t value) { g_guestBytes.at(address - kGuestBase) = value; } +void MemoryInline::Write16Slow(uint32_t address, uint16_t value) { + BigEndian::Write16(&g_guestBytes.at(address - kGuestBase), value); +} +void MemoryInline::Write32Slow(uint32_t address, uint32_t value) { + BigEndian::Write32(&g_guestBytes.at(address - kGuestBase), value); +} +void* GuestToHostPtr(uint32_t address, size_t length) { + return Memory::Contains(address, length) ? &g_guestBytes.at(address - kGuestBase) : nullptr; +} +void EnsureAuroraFrameActive() {} +void EnsureAuroraFrameForFifo() {} +void WriteDisplayListData(uint32_t value, uint32_t size) { + size = size == 0 ? 4 : size; + for (uint32_t i = 0; i < size; ++i) { + Memory::Write8(g_dlRecordState.writePtr++, static_cast(value >> (8 * (size - i - 1)))); + ++g_dlRecordState.count; + } +} +extern "C" void GX__CallDisplayList_80172f64(uint32_t address, uint32_t size) { + GXCallDisplayList(GuestToHostPtr(address, size), size); +} + +extern void GxWorkerReplayFifoBurst(const uint8_t* bytes, uint32_t size); + +namespace { +class RuntimeFifoInvalidationTest : public GXFifoTest { +protected: + void SetUp() override { + GXFifoTest::SetUp(); + g_hleGxState = {}; + g_dlRecordState = {}; + g_deferredBursts.clear(); + GxWorker::detail::g_active = false; + GxWorker::detail::t_onWorker = false; + g_alphaCompareValid = false; + } + + void TearDown() override { + GxWorker::detail::g_active = false; + GxWorker::detail::t_onWorker = false; + GXFifoTest::TearDown(); + } + + void seed_cache() { + for (auto& array : gxState().arrays) { + array.cachedRange = {32, 64}; + } + gxState().dirty = 0; + } + + void expect_invalidated() { + for (const auto& array : gxState().arrays) { + EXPECT_EQ(array.cachedRange.size, 0u); + } + EXPECT_NE(gxState().dirty & aurora::gx::DirtyImmediates, 0u); + } +}; + +TEST_F(RuntimeFifoInvalidationTest, ScalarCommandClearsEveryCachedArray) { + seed_cache(); + HleFifoWrite(0x48, 1); + const auto bytes = capture_fifo(); + ASSERT_EQ(bytes, (std::vector{0x48})); + EXPECT_EQ(gxState().arrays[GX_VA_POS].cachedRange.size, 64u); + decode_fifo(bytes); + expect_invalidated(); +} + +TEST_F(RuntimeFifoInvalidationTest, BurstFastPrefixAndFallbackKeepCommandOrder) { + const std::vector stream{0x61, 0x41, 0, 0, 1, 0x48, 0x61, 0x42, 0, 0, 2, 0x48}; + GX_HLE_FIFO_WriteBurst(stream.data(), stream.size()); + EXPECT_EQ(capture_fifo(), stream); + EXPECT_EQ(g_hleGxState.fifoByteCount, 0u); +} + +TEST_F(RuntimeFifoInvalidationTest, PartialPacketPayloadDoesNotBecomeAnInvalidation) { + const std::array prefix{0x08, 0x50, 0, 0}; + const std::array tail{0x48, 0, 0x48}; + GX_HLE_FIFO_WriteBurst(prefix.data(), prefix.size()); + EXPECT_TRUE(capture_fifo().empty()); + GX_HLE_FIFO_WriteBurst(tail.data(), tail.size()); + EXPECT_EQ(capture_fifo(), (std::vector{0x48})); + EXPECT_EQ(g_hleGxState.fifoByteCount, 0u); +} + +TEST_F(RuntimeFifoInvalidationTest, DeferredBurstOwnsBytesUntilWorkerReplay) { + GxWorker::detail::g_active = true; + std::array stream{0x48}; + GX_HLE_FIFO_WriteBurst(stream.data(), stream.size()); + stream[0] = 0; + EXPECT_TRUE(capture_fifo().empty()); + ASSERT_EQ(g_deferredBursts.size(), 1u); + GxWorker::detail::t_onWorker = true; + const auto& copy = g_deferredBursts.front(); + GxWorkerReplayFifoBurst(copy.data(), copy.size()); + EXPECT_EQ(capture_fifo(), (std::vector{0x48})); + EXPECT_EQ(g_deferredBursts.size(), 1u); +} + +TEST_F(RuntimeFifoInvalidationTest, RecordingDefersInvalidationUntilPlayback) { + g_dlRecordState = {kGuestBase, 32, kGuestBase, 0, true}; + HleFifoWrite(0x48, 1); + EXPECT_TRUE(capture_fifo().empty()); + EXPECT_EQ(g_guestBytes[0], 0x48); + EXPECT_EQ(g_dlRecordState.count, 1u); + g_dlRecordState.active = false; + GX_HLE_FIFO_WriteBurst(g_guestBytes.data(), 1); + seed_cache(); + decode_fifo(capture_fifo()); + expect_invalidated(); +} + +TEST_F(RuntimeFifoInvalidationTest, CallPacketForwardsChildInvalidation) { + g_guestBytes[0] = 0x48; + const std::array call{0x40, 0x80, 0, 0x10, 0, 0, 0, 0, 1}; + GX_HLE_FIFO_WriteBurst(call.data(), call.size()); + seed_cache(); + decode_fifo(capture_fifo()); + expect_invalidated(); +} + +TEST_F(RuntimeFifoInvalidationTest, SameAddressUploadsNewVerticesAfterInvalidation) { + auto& storage = aurora::gfx::testing::storageData; + storage.clear(); + std::array vertices{1, 2, 3, 4, 5, 6, 7, 8, 9}; + GXClearVtxDesc(); + GXSetVtxDesc(GX_VA_POS, GX_INDEX8); + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_U8, 0); + GXSetArray(GX_VA_POS, vertices.data(), vertices.size(), 3, false); + const auto draw = [&] { + GXBegin(GX_TRIANGLES, GX_VTXFMT0, 3); + GXPosition1x8(0); + GXPosition1x8(1); + GXPosition1x8(2); + GXEnd(); + decode_fifo(capture_fifo()); + }; + draw(); + const auto first = gxState().arrays[GX_VA_POS].cachedRange; + ASSERT_EQ(first.size, vertices.size()); + ASSERT_LE(first.offset + first.size, storage.size()); + EXPECT_TRUE(std::equal(vertices.begin(), vertices.end(), storage.begin() + first.offset)); + + vertices.fill(42); + draw(); + EXPECT_EQ(gxState().arrays[GX_VA_POS].cachedRange.offset, first.offset); + EXPECT_NE(storage[first.offset], 42); + + HleFifoWrite(0x48, 1); + draw(); + const auto second = gxState().arrays[GX_VA_POS].cachedRange; + ASSERT_EQ(second.size, vertices.size()); + ASSERT_LE(second.offset + second.size, storage.size()); + EXPECT_NE(second.offset, first.offset); + EXPECT_TRUE(std::equal(vertices.begin(), vertices.end(), storage.begin() + second.offset)); +} +} // namespace diff --git a/runtime/tests/host_file_tests.cpp b/runtime/tests/host_file_tests.cpp new file mode 100644 index 000000000..47b5209ab --- /dev/null +++ b/runtime/tests/host_file_tests.cpp @@ -0,0 +1,193 @@ +#include "host_file.h" + +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +namespace { +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} +std::string Read(const std::filesystem::path& path) { + std::ifstream input(path, std::ios::binary); + return {std::istreambuf_iterator(input), std::istreambuf_iterator()}; +} +struct ShortWrites : RuntimeHostFile::FileOperations { + static size_t Write(FILE* file, const char* data, size_t size) { + return FileOperations::Write(file, data, std::min(size, size_t{3})); + } +}; +struct FailedWrite : RuntimeHostFile::FileOperations { + static size_t Write(FILE* file, const char* data, size_t size) { + if (std::ftell(file) != 0) { errno = ENOSPC; return 0; } + return FileOperations::Write(file, data, std::min(size, size_t{4})); + } +}; +struct FailedFlush : RuntimeHostFile::FileOperations { + static bool Flush(FILE*) { return false; } +}; +struct FailedClose : RuntimeHostFile::FileOperations { + static bool Close(FILE* file) { FileOperations::Close(file); return false; } +}; +struct FailedPublish : RuntimeHostFile::FileOperations { + static bool Publish(const std::filesystem::path&, const std::filesystem::path&, bool) { return false; } +}; +struct PublishedFailure : RuntimeHostFile::FileOperations { + static bool Publish(const std::filesystem::path& temporary, const std::filesystem::path& target, bool replace) { + FileOperations::Publish(temporary, target, replace); + return false; + } +}; +#ifdef _WIN32 +void CheckPermissions(const std::filesystem::path& path) { + HANDLE token = nullptr; + Require(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token), "cannot inspect test user"); + DWORD needed = 0; + GetTokenInformation(token, TokenUser, nullptr, 0, &needed); + std::vector user(needed); + const bool read = GetTokenInformation(token, TokenUser, user.data(), needed, &needed) != 0; + CloseHandle(token); + Require(read, "cannot read test user SID"); + LPWSTR sid = nullptr; + Require(ConvertSidToStringSidW(reinterpret_cast(user.data())->User.Sid, &sid), "cannot format test user SID"); + const std::wstring aclText = L"D:P(A;;FA;;;" + std::wstring(sid) + L")"; + LocalFree(sid); + PSECURITY_DESCRIPTOR descriptor = nullptr; + Require(ConvertStringSecurityDescriptorToSecurityDescriptorW(aclText.c_str(), SDDL_REVISION_1, + &descriptor, nullptr), "cannot create private test ACL"); + const bool set = SetFileSecurityW(path.c_str(), DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, descriptor) != 0; + LocalFree(descriptor); + Require(set, "cannot apply private test ACL"); + const auto readAcl = [&] { + DWORD securitySize = 0; + GetFileSecurityW(path.c_str(), DACL_SECURITY_INFORMATION, nullptr, 0, &securitySize); + std::vector security(securitySize); + Require(GetFileSecurityW(path.c_str(), DACL_SECURITY_INFORMATION, security.data(), securitySize, &securitySize), + "cannot inspect test ACL"); + // Windows may normalize this bookkeeping bit during replacement. + Require(SetSecurityDescriptorControl(security.data(), SE_DACL_AUTO_INHERITED, 0), + "cannot normalize ACL inheritance metadata"); + LPWSTR text = nullptr; + const bool converted = ConvertSecurityDescriptorToStringSecurityDescriptorW(security.data(), SDDL_REVISION_1, + DACL_SECURITY_INFORMATION, &text, nullptr) != 0; + Require(converted, "cannot format test ACL"); + const std::wstring result(text); + LocalFree(text); + return result; + }; + const auto originalAcl = readAcl(); + Require(RuntimeHostFile::WriteAtomically(path, "private contents"), "private file replacement failed"); + const auto replacedAcl = readAcl(); + if (replacedAcl != originalAcl) { + std::wcerr << L"original ACL: " << originalAcl << L"\nreplacement ACL: " << replacedAcl << L'\n'; + } + Require(replacedAcl == originalAcl, "replacement changed the file ACL"); +} +#endif +template +void CheckFailure(const std::filesystem::path& path) { + Require(!RuntimeHostFile::WriteAtomically(path, "replacement contents"), "failure was reported as success"); + Require(Read(path) == "original contents", "a failed publication changed the destination"); + for (const auto& entry : std::filesystem::directory_iterator(path.parent_path())) { + Require(entry.path() == path, "a failed publication left a temporary file"); + } +} +} // namespace + +int main() { + const auto root = std::filesystem::temp_directory_path() / ("wiicomp-host-file-" + std::to_string( + std::chrono::steady_clock::now().time_since_epoch().count())); + bool claimed = false; + try { + claimed = std::filesystem::create_directory(root); + Require(claimed, "cannot claim test directory"); + const auto path = root / "Config.toml"; + Require(RuntimeHostFile::WriteAtomically(path, "original contents"), "initial publication failed"); + CheckFailure(path); + CheckFailure(path); + CheckFailure(path); + CheckFailure(path); + Require(RuntimeHostFile::WriteAtomically(path, "short writes are retried"), "short writes were not retried"); + Require(Read(path) == "short writes are retried", "short writes produced incomplete contents"); + Require(!RuntimeHostFile::WriteAtomically(path, "do not replace", false), "exclusive publication overwrote a file"); + Require(Read(path) == "short writes are retried", "exclusive publication truncated a file"); + Require(!RuntimeHostFile::WriteAtomically(path, "complete after publication"), "injected post-publication failure was lost"); + Require(Read(path) == "complete after publication", "post-publication failure left partial contents"); + +#ifndef _WIN32 + std::filesystem::permissions(path, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write); + Require(RuntimeHostFile::WriteAtomically(path, "private contents"), "private file replacement failed"); + Require((std::filesystem::status(path).permissions() & std::filesystem::perms::all) == + (std::filesystem::perms::owner_read | std::filesystem::perms::owner_write), "replacement changed file permissions"); + const auto blocked = root / "blocked"; + std::filesystem::create_directory(blocked); + std::filesystem::permissions(blocked, std::filesystem::perms::owner_read | std::filesystem::perms::owner_exec); + if (::geteuid() != 0) { + const bool createFailed = RuntimeHostFile::CreateExclusive(blocked / "save") == EIO; + const bool writeFailed = !RuntimeHostFile::WriteAtomically(blocked / "Config.toml", "unwritable"); + std::filesystem::permissions(blocked, std::filesystem::perms::owner_all); + Require(createFailed && writeFailed, "unwritable parent was accepted"); + } else { + std::filesystem::permissions(blocked, std::filesystem::perms::owner_all); + } +#else + CheckPermissions(path); +#endif + const auto link = root / "linked.toml"; + std::error_code ec; + std::filesystem::create_symlink(path.filename(), link, ec); + if (!ec) { + Require(RuntimeHostFile::WriteAtomically(link, "through the link"), "symlink target replacement failed"); + Require(std::filesystem::is_symlink(link) && Read(path) == "through the link", "replacement destroyed a symlink"); + const auto dangling = root / "dangling.toml"; + std::filesystem::create_symlink("absent.toml", dangling); + Require(!RuntimeHostFile::WriteAtomically(dangling, "no target"), "dangling symlink was replaced"); + Require(std::filesystem::is_symlink(dangling), "dangling symlink disappeared"); + } else { + std::cout << "symlink creation unavailable: " << ec.message() << '\n'; + } + + const auto save = root / std::filesystem::path(u8"save-\u00e9-\u65e5.dat"); + Require(RuntimeHostFile::CreateExclusive(save) == 0, "Unicode exclusive create failed"); + { std::ofstream output(save, std::ios::binary); output << "save sentinel"; } + Require(RuntimeHostFile::CreateExclusive(save) == EEXIST && Read(save) == "save sentinel", "duplicate create destroyed a save"); + Require(RuntimeHostFile::CreateExclusive(root) != 0 && std::filesystem::is_directory(root), "create accepted a directory"); + Require(RuntimeHostFile::CreateExclusive(root / "absent" / "save") == EIO, "missing parent create did not fail"); + Require(RuntimeHostFile::CreateExclusive(root / "failed-close") == EIO, "create ignored close failure"); + + const auto raced = root / "racing.dat"; + std::atomic ready{0}, successes{0}, duplicates{0}; + std::atomic start{false}; + std::vector creators; + for (unsigned i = 0; i < 16; ++i) { + creators.emplace_back([&] { + ++ready; + while (!start.load()) std::this_thread::yield(); + const int result = RuntimeHostFile::CreateExclusive(raced); + if (result == 0) ++successes; + else if (result == EEXIST) ++duplicates; + }); + } + while (ready.load() != 16) std::this_thread::yield(); + start = true; + for (auto& creator : creators) creator.join(); + Require(successes == 1 && duplicates == 15, "simultaneous creates did not have exactly one winner"); + const auto directory = root / "directory"; + std::filesystem::create_directory(directory); + Require(!RuntimeHostFile::WriteAtomically(directory, "not a file"), "atomic publication replaced a directory"); + std::filesystem::remove_all(root); + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + std::error_code ignored; + if (claimed) std::filesystem::remove_all(root, ignored); + return 1; + } + return 0; +} diff --git a/runtime/tests/host_io/SDL3/SDL_audio.h b/runtime/tests/host_io/SDL3/SDL_audio.h new file mode 100644 index 000000000..a53f78d88 --- /dev/null +++ b/runtime/tests/host_io/SDL3/SDL_audio.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +using SDL_AudioDeviceID = uint32_t; +using SDL_AudioFormat = uint32_t; +struct SDL_AudioStream; +struct SDL_AudioSpec { + SDL_AudioFormat format; + int channels; + int freq; +}; +using SDL_AudioStreamCallback = void (*)(void*, SDL_AudioStream*, int, int); +inline constexpr SDL_AudioFormat SDL_AUDIO_S16LE = 0x8010u; +inline constexpr SDL_AudioDeviceID SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK = 0xFFFFFFFFu; + +extern "C" { +const char* SDL_GetError(); +SDL_AudioStream* SDL_OpenAudioDeviceStream(SDL_AudioDeviceID, const SDL_AudioSpec*, + SDL_AudioStreamCallback, void*); +void SDL_DestroyAudioStream(SDL_AudioStream*); +bool SDL_ResumeAudioStreamDevice(SDL_AudioStream*); +bool SDL_SetAudioStreamGain(SDL_AudioStream*, float); +int SDL_GetAudioStreamQueued(SDL_AudioStream*); +bool SDL_PutAudioStreamData(SDL_AudioStream*, const void*, int); +} diff --git a/runtime/tests/host_io/SDL3/SDL_init.h b/runtime/tests/host_io/SDL3/SDL_init.h new file mode 100644 index 000000000..9b6d39ce9 --- /dev/null +++ b/runtime/tests/host_io/SDL3/SDL_init.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +inline constexpr uint32_t SDL_INIT_AUDIO = 0x10u; +extern "C" bool SDL_InitSubSystem(uint32_t); +extern "C" void SDL_QuitSubSystem(uint32_t); diff --git a/runtime/tests/memory_access_tests.cpp b/runtime/tests/memory_access_tests.cpp new file mode 100644 index 000000000..3d7735ad2 --- /dev/null +++ b/runtime/tests/memory_access_tests.cpp @@ -0,0 +1,335 @@ +#include "memory.h" +#include "guest_flat_memory.h" +#include "hle/hollywood_gpio.h" +#include "isa/ppc_isa_float.h" +#include "isa/ppc_isa_fpenv.h" +#include "system_bridge.h" + +#include +#include +#include +#include +#include + +// Link with memory.cpp and the platform's real guest_flat_memory implementation. +namespace RecompMod { +std::atomic g_executableWriteGuardEnabled{false}; +std::atomic g_executableWriteGuardPages[kExecutableWriteGuardPageCount]{}; +std::atomic g_executableWriteGuardCoarsePages[kExecutableWriteGuardCoarsePageCount]{}; +std::atomic g_executableWriteGuardMidPages[kExecutableWriteGuardMidPageCount]{}; +uint32_t CurrentTranslatedExecutionAddress() noexcept { return 0; } +bool HandleExecutableWrite(uint32_t, size_t, uint64_t) { return false; } +} + +void SystemBridge::DumpCpuState(const CpuContext*) {} +void ShowRuntimeFatalPopup(std::string_view, std::string_view) noexcept { std::abort(); } +bool HollywoodGpio_Read32(uint32_t, uint32_t&) { return false; } +bool HollywoodGpio_Write32(uint32_t, uint32_t) { return false; } +extern "C" void GX_HLE_FIFO_WriteFloat(float) { std::abort(); } +extern "C" void GX_HLE_FIFO_Write32(uint32_t) { std::abort(); } +extern "C" void GX_HLE_FIFO_Write16(uint16_t) { std::abort(); } +extern "C" void GX_HLE_FIFO_Write8(uint8_t) { std::abort(); } + +namespace { +constexpr size_t kBankSize = 2 * MemoryInline::kPageSize + 0x1000; +constexpr uint32_t kSmallBase = 0xE0000000u; +constexpr uint32_t kTopBase = 0xFFFF0000u; +constexpr std::array kBanks{ + 0u, 0x80000000u, 0xC0000000u, 0x10000000u, 0x90000000u, 0xD0000000u}; + +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +Memory::Config TestConfig() { + Memory::Config config; + for (uint32_t base : kBanks) + config.regions.push_back({"bank", base, kBankSize}); + config.regions.push_back({"small", kSmallBase, 0x1000}); + config.regions.push_back({"top", kTopBase, 0x10000}); + return config; +} + +void TestInvalidConfig() { + const auto* saved = Memory::GetPointer(0x80000000u); + for (const auto& region : std::array{ + Memory::RegionConfig{"huge", 0x80000000u, SIZE_MAX}, + Memory::RegionConfig{"wrap", kTopBase, 0x10001}, + Memory::RegionConfig{"wrap", 1u, SIZE_MAX}}) { + Memory::Config invalid; + invalid.regions.push_back(region); + bool rejected = false; + try { + Memory::Init(invalid); + } catch (const std::invalid_argument&) { + rejected = true; + } + Require(rejected, "invalid region size was accepted"); + Require(Memory::GetPointer(0x80000000u) == saved, + "invalid region size discarded the existing memory map"); + } +} + +void TestRanges() { + for (uint32_t base : kBanks) { + const uint32_t end = base + static_cast(kBankSize); + Require(Memory::Contains(base, 0), "empty range at bank start failed"); + Require(Memory::Contains(end, 0), "empty range at bank end failed"); + Require(!Memory::Contains(end + 1, 0), "empty range beyond bank end passed"); + Require(Memory::GetPointer(end, 0) == Memory::GetPointer(base) + kBankSize, + "bank end pointer changed"); + Require(Memory::Contains(base, kBankSize), "whole bank range failed"); + Require(!Memory::Contains(base, kBankSize + 1), "range beyond bank passed"); + Require(Memory::Contains(end - 8, 8), "exact endpoint failed"); + Require(!Memory::Contains(end - 8, 9), "one-byte overrun passed"); + Require(!Memory::Contains(end, 1), "nonempty bank end passed"); + Require(Memory::Contains(base + MemoryInline::kPageSize - 4, 16), + "valid page-crossing range failed"); + + for (uint32_t address : {base, base + 1, end - 1, end}) { + for (size_t length : {SIZE_MAX, SIZE_MAX - 1, SIZE_MAX - size_t(address), + size_t(GuestFlat::kGuestSpaceSize)}) { + uint8_t* pointer = Memory::GetPointer(base); + Require(!MemoryInline::TryGetPointerFast(address, length, pointer) && !pointer, + "fast range lookup accepted an oversized native length"); + Require(!Memory::Contains(address, length), + "region lookup accepted an oversized native length"); + Require(!MemoryInline::TryGetWritablePointerFast(address, length, pointer), + "writable lookup accepted an oversized native length"); + } + } + uint8_t* pointer = nullptr; + Require(MemoryInline::TryGetPointerFast(end, 0, pointer), + "partial-page end pointer failed"); + Require(!MemoryInline::TryGetWritablePointerFast(end, 0, pointer), + "writable lookup accepted an empty range"); + } + + Require(!Memory::Contains(0x70000000u, 0), "unmapped empty range passed"); + Require(Memory::Contains(UINT32_MAX, 1), "last guest byte failed"); + Require(!Memory::Contains(UINT32_MAX, 2), "guest address wrap passed"); + Require(Memory::Contains(kTopBase, 0x10000), "range ending at 4 GiB failed"); + Require(!Memory::Contains(kTopBase, 0x10001), "range exceeding 4 GiB passed"); + bool rejected = false; + try { + (void)Memory::GetPointer(0x80000001u, SIZE_MAX); + } catch (const Memory::AccessViolation& error) { + rejected = error.address() == 0x80000001u && error.length() == SIZE_MAX; + } + Require(rejected, "GetPointer did not reject the overflowing range"); + + for (size_t bank = 0; bank < 2; ++bank) { + const uint32_t physical = bank == 0 ? 0u : 0x10000000u; + const uint32_t value = 0x12345678u + static_cast(bank); + Memory::Write32(physical + 0x100, value); + Require(Memory::Read32(physical + 0x80000100u) == value && + Memory::Read32(physical + 0xC0000100u) == value, + "cached/uncached aliases lost coherence"); + } + + Require(MemoryInline::ResolveRangeHost(0u, 0, 0, true, false) == nullptr, + "resolved range accepted zero length"); + Require(MemoryInline::ResolveRangeHost(0u, 0, UINT32_MAX, true, false) == nullptr, + "resolved range accepted an enormous length"); + Require(MemoryInline::ResolveRangeHost(0u, 0, MemoryInline::kPageSize + 1, true, false) == nullptr, + "resolved range exceeded its maximum length"); + Require(MemoryInline::ResolveRangeHost(UINT32_MAX, 0, 2, true, false) == nullptr, + "resolved range wrapped the guest address"); + Require(MemoryInline::ResolveRangeHost(0xCBFFFFFFu, 0, 2, false, true) == nullptr, + "resolved write crossed into MMIO"); + Require(MemoryInline::ResolveRangeHost(0xCDFFFFFFu, 0, 2, false, true) == nullptr, + "resolved write crossed out of MMIO"); + if (!GuestFlat::RequiresCheckedAccess()) { + Require(MemoryInline::ResolveRangeHost(UINT32_MAX, 0, 1, true, false) == + MKW_FLAT_GUEST_BASE + UINT32_MAX, + "resolved range ending at 4 GiB failed"); + Require(MemoryInline::ResolveRangeHost(UINT32_MAX, 1, 1, true, false) == MKW_FLAT_GUEST_BASE, + "effective-address addition stopped wrapping at 32 bits"); + Require(MemoryInline::ResolveRangeHost(4u, -4, 1, true, false) == MKW_FLAT_GUEST_BASE, + "negative effective-address offset failed"); + } +} + +void TestWritableRanges() { + uint8_t* pointer = nullptr; + Require(MemoryInline::TryGetWritablePointerFast(kSmallBase + 0xFFC, 4, pointer), + "small mapping exact endpoint failed"); + Require(!MemoryInline::TryGetWritablePointerFast(kSmallBase + 0xFFC, 5, pointer), + "small mapping overrun passed"); + Require(!MemoryInline::TryGetWritablePointerFast(kSmallBase + 0x1001, 1, pointer), + "small mapping offset beyond limit passed"); + Require(!MemoryInline::TryGetWritablePointerFast(UINT32_MAX, 2, pointer), + "writable range wrapped the guest address"); + const uint32_t executable = 0x80004000u; + RecompMod::g_executableWriteGuardPages[executable >> 12].store(1); + RecompMod::g_executableWriteGuardCoarsePages[executable >> 20].store(1); + Memory::RefreshWritableFastPathsForExecutableRanges(); + Require(MemoryInline::g_sparseWritablePageTables[executable >> 20] != nullptr, + "test did not reach sparse writable lookup"); + Require(MemoryInline::TryGetWritablePointerFast(executable - 8, 8, pointer), + "sparse writable exact endpoint failed"); + Require(!MemoryInline::TryGetWritablePointerFast(executable - 1, 2, pointer), + "sparse writable range crossed an executable page"); + Require(!MemoryInline::TryGetWritablePointerFast(executable, 1, pointer), + "sparse writable range entered an executable page"); + Require(!MemoryInline::TryGetWritablePointerFast(executable - 1, SIZE_MAX, pointer), + "sparse writable lookup accepted an overflowing length"); + RecompMod::g_executableWriteGuardPages[executable >> 12].store(0); + RecompMod::g_executableWriteGuardCoarsePages[executable >> 20].store(0); + Memory::RefreshWritableFastPathsForExecutableRanges(); +} + +bool CountRead(void* user) { + ++*static_cast(user); + return true; +} + +void TestDeferredRanges() { + unsigned calls = 0; + Require(Memory::RegisterDeferredRead(0x80000001u, SIZE_MAX, CountRead, &calls) == 0, + "deferred registration accepted an overflowing native length"); + Require(Memory::RegisterDeferredRead(UINT32_MAX, 2, CountRead, &calls) == 0, + "deferred registration accepted a wrapped guest range"); + Require(Memory::RegisterDeferredRead(0x80000100u, 0, CountRead, &calls) == 0, + "deferred registration accepted an empty range"); + Require(Memory::RegisterDeferredRead(0x80000100u, 4, nullptr, &calls) == 0, + "deferred registration accepted no callback"); + Require(Memory::RegisterDeferredRead(0x80000100u, 4, CountRead, &calls) != 0, + "valid deferred registration failed"); + Require(!MemoryInline::ResolveDeferredReads(0x80000001u, SIZE_MAX) && calls == 0, + "overflowing resolution consumed a deferred read"); + Require(MemoryInline::ResolveDeferredReads(0x80000101u, 0) && calls == 0, + "empty resolution consumed a deferred read"); + Require(MemoryInline::ResolveDeferredReads(0x80000104u, 1) && calls == 0, + "touching endpoints counted as overlapping"); + Require(MemoryInline::ResolveDeferredReads(0x800000FFu, 2) && calls == 1, + "overlapping resolution did not consume the deferred read once"); + Require(MemoryInline::ResolveDeferredReads(0x80000100u, 4) && calls == 1, + "materialized deferred read fired twice"); + Require(Memory::RegisterDeferredRead(UINT32_MAX, 1, CountRead, &calls) != 0, + "deferred range ending at 4 GiB failed"); + Require(MemoryInline::ResolveDeferredReads(UINT32_MAX, 1) && calls == 2, + "last-byte deferred range did not resolve"); + Memory::ClearDeferredReads(); +} + +void TestDeferredEndpoint() { + if (GuestFlat::RequiresCheckedAccess()) return; + unsigned calls = 0; + Require(Memory::RegisterDeferredRead(UINT32_MAX, 1, CountRead, &calls) != 0, + "last-byte deferred registration failed"); + Require(GuestFlat::HandleAccessViolation(MKW_FLAT_GUEST_BASE + UINT32_MAX, false), + "last-byte deferred fault was not handled"); + Require(calls == 1, "last-byte deferred fault did not materialize its callback"); + Require(MemoryInline::ResolveDeferredReads(UINT32_MAX, 1) && calls == 1, + "last-byte fault left a pending callback"); + Memory::ClearDeferredReads(); +} + +struct SingleCase { uint32_t single; uint64_t widened; }; +constexpr SingleCase kSingles[]{ + {0x00000000u, 0x0000000000000000ull}, {0x80000000u, 0x8000000000000000ull}, + {0x00000001u, 0x36A0000000000000ull}, {0x80000001u, 0xB6A0000000000000ull}, + {0x0040000Bu, 0x38000002C0000000ull}, {0x8040000Bu, 0xB8000002C0000000ull}, + {0x007FFFFFu, 0x380FFFFFC0000000ull}, {0x807FFFFFu, 0xB80FFFFFC0000000ull}, + {0x00800000u, 0x3810000000000000ull}, {0x80800000u, 0xB810000000000000ull}, + {0x00800001u, 0x3810000020000000ull}, {0x3F800000u, 0x3FF0000000000000ull}, + {0xBF800000u, 0xBFF0000000000000ull}, {0x7F7FFFFFu, 0x47EFFFFFE0000000ull}, + {0x7F800000u, 0x7FF0000000000000ull}, {0xFF800000u, 0xFFF0000000000000ull}, + {0x7F800001u, 0x7FF0000020000000ull}, {0xFF800001u, 0xFFF0000020000000ull}, + {0x7FC12345u, 0x7FF82468A0000000ull}, {0xFFC12345u, 0xFFF82468A0000000ull}, +}; + +MKW_PPC_NO_INLINE double FlatSingle(uint32_t address) { + return MemoryInline::FlatReadFloat32(address); +} + +MKW_PPC_NO_INLINE double ResolvedSingle(uint8_t* host, uint32_t offset, uint32_t address) { + return MemoryInline::ReadResolvedFloat32(host, offset, address); +} + +struct PendingSingle { + uint32_t address; + uint32_t bits; + unsigned calls = 0; +}; + +bool MaterializeSingle(void* user) { + auto& pending = *static_cast(user); + Memory::Write32(pending.address, pending.bits); + ++pending.calls; + return true; +} + +void CheckSingle(uint32_t address, const SingleCase& value) { + Memory::Write32(address, value.single); + CpuContext cpu{}; + cpu.fpr[2].d = FlatSingle(address); + Require(PpcBitCastToU64Inline(cpu.fpr[2].d) == value.widened, "flat single load changed the stored bits"); + auto* host = Memory::GetPointer(address - 4, 8); + cpu.fpr[3].d = ResolvedSingle(host, 4, address); + Require(PpcBitCastToU64Inline(cpu.fpr[3].d) == value.widened, "resolved single load changed the stored bits"); + cpu.fpr[4].d = ResolvedSingle(nullptr, 0, address); + Require(PpcBitCastToU64Inline(cpu.fpr[4].d) == value.widened, "fallback single load changed the stored bits"); + Require(PpcBitCastToU64Inline(MemoryInline::ReadResolvedFallbackFloat32(address)) == value.widened, + "checked single load changed the stored bits"); +} + +void TestSingleLoads() { + struct RestoreControl { + uint32_t saved = MkwGetHostFpControl(); + ~RestoreControl() { MkwRestoreHostMxcsr(saved); } + } restore; + std::array controls{ + restore.saved & ~kMkwFpControlFlushToZeroBits, + restore.saved | kMkwFpControlFlushToZeroBits, + restore.saved & ~kMkwFpControlFlushToZeroBits, + restore.saved | kMkwFpControlFlushToZeroBits}; +#if defined(__x86_64__) + controls[2] |= 1u << 15; + controls[3] &= ~(1u << 15); +#endif + for (uint32_t control : controls) { + MkwRestoreHostMxcsr(control); + for (const auto& value : kSingles) { + for (uint32_t bank : kBanks) { + CheckSingle(bank + 0x100u, value); + CheckSingle(bank + MemoryInline::kPageSize - 2, value); + CheckSingle(bank + static_cast(kBankSize) - 4, value); + } + CheckSingle(kSmallBase + 4, value); + CheckSingle(UINT32_MAX - 3, value); + PendingSingle pending{0x80000200u, value.single}; + Memory::Write32(pending.address, 0); + Require(Memory::RegisterDeferredRead(pending.address, 4, MaterializeSingle, &pending) != 0, + "deferred single registration failed"); + Require(PpcBitCastToU64Inline(ResolvedSingle(nullptr, 0, pending.address)) == value.widened && + pending.calls == 1, + "single fallback did not materialize the deferred value"); + } + } +} +} // namespace + +int main(int argc, char** argv) { + try { + Memory::Init(TestConfig()); + const std::string_view selection = argc > 1 ? argv[1] : "all"; + Require(selection == "all" || selection == "ranges" || selection == "single-loads" || + selection == "deferred-endpoint", + "expected all, ranges, single-loads or deferred-endpoint"); + if (selection == "all" || selection == "ranges") { + TestRanges(); + TestWritableRanges(); + TestDeferredRanges(); + TestInvalidConfig(); + } + if (selection == "all" || selection == "deferred-endpoint") TestDeferredEndpoint(); + if (selection == "all" || selection == "single-loads") TestSingleLoads(); + Memory::Reset(); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/runtime/tests/memory_slow_test_support.h b/runtime/tests/memory_slow_test_support.h new file mode 100644 index 000000000..99ee2aaf0 --- /dev/null +++ b/runtime/tests/memory_slow_test_support.h @@ -0,0 +1,17 @@ +#pragma once + +#include "guest_abi/memory.h" + +// Leave fast mappings empty so scalar accesses use each harness's bounded store. +namespace MemoryInline { +PageEntry g_pageTable[kPageCount]{}; +uintptr_t g_fullPageBias[kPageCount]{}; +uintptr_t g_fullReadablePageBias[kPageCount]{}; +uintptr_t g_fullWritablePageBias[kPageCount]{}; +const SparseWritablePageTable* g_sparseWritablePageTables[kPageCount]{}; +uint8_t g_deferredReadCoveredPages[kPageCount]{}; +} + +namespace RecompMod { +std::atomic g_executableWriteGuardPages[kExecutableWriteGuardPageCount]{}; +} diff --git a/runtime/tests/network_receive_tests.cpp b/runtime/tests/network_receive_tests.cpp new file mode 100644 index 000000000..17a82ad38 --- /dev/null +++ b/runtime/tests/network_receive_tests.cpp @@ -0,0 +1,491 @@ +#include "../src/hle/net/network_internal.h" +#include "fiber_manager.h" +#include "gx_guest_write.h" +#include "memory_slow_test_support.h" + +#include +#include +#include + +namespace { +constexpr uint32_t kBase = 0x80000000u; +constexpr uint32_t kControl = kBase + 0x1000u; +constexpr uint32_t kVectors = kBase + 0x1100u; +constexpr uint32_t kThread = kBase + 0x2000u; +constexpr uint32_t kQueue = kBase + 0x3000u; +constexpr uint32_t kData = kBase + 0x4000u; +std::vector guest(4u * 1024u * 1024u); +bool invalidOutput = false; +bool terminated = false; +uint64_t threadGeneration = 1; +bool deviceOpen = true; +int wakeups = 0; +int injectedReadError = 0; +std::vector> callbacks; + +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +int TestRecvFrom(NetworkHle::NativeSocket fd, char* data, int length, int flags, + sockaddr* from, socklen_t* fromLength) { + if (injectedReadError) { +#ifdef _WIN32 + WSASetLastError(std::exchange(injectedReadError, 0)); +#else + errno = std::exchange(injectedReadError, 0); +#endif + return -1; + } + return static_cast(recvfrom(fd, data, length, flags, from, fromLength)); +} +} + +// Exercise the production scheduler and receive code with only the surrounding +// runtime services replaced. Native socket I/O still goes over loopback. +#define recvfrom TestRecvFrom +#include "../src/hle/net/network_socket.cpp" +#undef recvfrom +#include "../src/hle/net/network_deferred.cpp" + +extern "C" const uint8_t kMkwGuestRegion = 0; +Memory::AccessViolation::AccessViolation(uint32_t address, size_t length, std::string_view reason) + : std::runtime_error(std::string(reason)), address_(address), length_(length), reason_(reason) {} +bool Memory::Contains(uint32_t address, size_t length) { + return !(invalidOutput && address == kData) && address >= kBase && + static_cast(address - kBase) + length <= guest.size(); +} +uint8_t* Memory::GetPointer(uint32_t address, size_t length) { + if (!Contains(address, length)) throw AccessViolation(address, length, "test mapping"); + return guest.data() + address - kBase; +} +uint8_t MemoryInline::Read8Slow(uint32_t address) { return *Memory::GetPointer(address, 1); } +uint16_t MemoryInline::Read16Slow(uint32_t address) { + return static_cast((Memory::Read8(address) << 8) | Memory::Read8(address + 1)); +} +uint32_t MemoryInline::Read32Slow(uint32_t address) { + return (static_cast(Memory::Read16(address)) << 16) | Memory::Read16(address + 2); +} +uint64_t MemoryInline::Read64Slow(uint32_t address) { + return (static_cast(Memory::Read32(address)) << 32) | Memory::Read32(address + 4); +} +void MemoryInline::Write8Slow(uint32_t address, uint8_t value) { *Memory::GetPointer(address, 1) = value; } +void MemoryInline::Write16Slow(uint32_t address, uint16_t value) { + Memory::Write8(address, static_cast(value >> 8)); + Memory::Write8(address + 1, static_cast(value)); +} +void MemoryInline::Write32Slow(uint32_t address, uint32_t value) { + Memory::Write16(address, static_cast(value >> 16)); + Memory::Write16(address + 2, static_cast(value)); +} +void MemoryInline::Write64Slow(uint32_t address, uint64_t value) { + Memory::Write32(address, static_cast(value >> 32)); + Memory::Write32(address + 4, static_cast(value)); +} +bool Fiber::GuestFiberManager::IsTerminated(uint32_t) { return terminated; } +uint64_t Fiber::GuestFiberManager::GetThreadGeneration(uint32_t) { return threadGeneration; } +void OS_HLE_WakeupThreadNoReschedule(CpuContext*, uint32_t queue) { + Require(queue == kQueue, "woke the wrong queue"); + ++wakeups; + Memory::Write16(kThread + 0x2C8u, 1); +} +void NandQueueIosCallback(uint32_t callback, int32_t result, uint32_t argument) { + callbacks.emplace_back(callback, result, argument); +} + +namespace NetworkHle { +std::array g_sockets; +int32_t g_lastSocketError = 0; +bool EnsureSocketRuntime() { return true; } +bool SetNonBlocking(NativeSocket socket, bool enabled) { +#ifdef _WIN32 + u_long mode = enabled ? 1 : 0; + return ioctlsocket(socket, FIONBIO, &mode) == 0; +#else + const int flags = fcntl(socket, F_GETFL, 0); + return flags >= 0 && fcntl(socket, F_SETFL, enabled ? flags | O_NONBLOCK : flags & ~O_NONBLOCK) == 0; +#endif +} +int NativeLastError() { +#ifdef _WIN32 + return WSAGetLastError(); +#else + return errno; +#endif +} +bool IsWouldBlockError(int error) { +#ifdef _WIN32 + return error == WSAEWOULDBLOCK; +#else + return error == EWOULDBLOCK || error == EAGAIN; +#endif +} +int32_t TranslateSocketError(int error, bool) { + if (IsWouldBlockError(error)) return -SO_EAGAIN; +#ifdef _WIN32 + if (error == WSAECONNRESET) return -SO_ECONNRESET; +#else + if (error == ECONNRESET) return -SO_ECONNRESET; +#endif + return -SO_EINVAL; +} +int32_t SocketErrorResult(int error, bool readWrite) { + return g_lastSocketError = TranslateSocketError(error, readWrite); +} +int32_t SocketResult(int result, bool readWrite) { + g_lastSocketError = result < 0 ? TranslateSocketError(NativeLastError(), readWrite) : 0; + return result < 0 ? g_lastSocketError : result; +} +int NormalizeConnectError(int error) { return error; } +void NetFail(const char*, ...) {} +WiiSocket* GetWiiSocket(uint32_t fd) { + return fd < g_sockets.size() && g_sockets[fd].native != kInvalidSocket ? &g_sockets[fd] : nullptr; +} +bool SocketIdentityIsCurrent(uint32_t fd, NativeSocket native, uint64_t generation) { + const auto* socket = GetWiiSocket(fd); + return socket && socket->native == native && socket->generation == generation; +} +std::optional GetDeviceKind(uint32_t fd) { + return fd == 2000 && deviceOpen ? std::optional{DeviceKind::IpTop} : std::nullopt; +} +std::vector ReadVectors(uint32_t address, uint32_t count) { + std::vector result; + for (uint32_t i = 0; i < count; ++i) result.push_back(RuntimeHle::ReadIoVector(address, i)); + return result; +} +void CloseNativeSocket(NativeSocket socket) { +#ifdef _WIN32 + closesocket(socket); +#else + close(socket); +#endif +} +void ClearSslSessionsForSocket(uint32_t) {} +const std::array& RuntimeMacAddress() { static const std::array mac{}; return mac; } +NasSslWriteAction PreparePlainNasTcpWrite(WiiSocket&, const uint8_t*, uint32_t, std::vector&) { + return NasSslWriteAction::PassThrough; +} +int MapWiiAf(uint32_t) { return AF_INET; } +int MapWiiSocketType(uint32_t) { return SOCK_STREAM; } +int MapNativeAfToWii(int family) { return family == AF_INET ? kWiiAfInet : -1; } +int MapWiiAddrInfoFamily(uint32_t) { return AF_INET; } +int MapWiiAddrInfoSocketType(uint32_t) { return SOCK_STREAM; } +uint32_t MapNativeSocketTypeToWii(int) { return kWiiSockStream; } +int32_t AddWiiSocket(NativeSocket, int, int, int) { throw std::runtime_error("unexpected socket allocation"); } +std::string ReadGuestString(uint32_t, uint32_t) { return {}; } +void ZeroMemoryRange(uint32_t address, uint32_t size) { std::memset(Memory::GetPointer(address, size), 0, size); } +void CopyToGuest(uint32_t address, const void* data, uint32_t size) { + std::memcpy(Memory::GetPointer(address, size), data, size); +} +bool ValidatePollRequest(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t&) { return false; } +std::vector CopyPollDescriptors(uint32_t, uint32_t) { return {}; } +void WritePollResults(uint32_t, const std::vector&) {} +bool CopiedPollSocketIsStillValid(const NetworkPollContract::CopiedDescriptor&) { return false; } +int ProbeConnectSettled(NativeSocket, int) { return 0; } +int32_t ClassifySettledConnect(NativeSocket) { return 0; } +} + +namespace { +using namespace NetworkHle; +using NetworkDeferredContract::StartDisposition; + +struct Loopback { + NativeSocket peer = kInvalidSocket; + Loopback() { + NativeSocket listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + Require(listener != kInvalidSocket, "listener failed"); + sockaddr_in address{}; + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + Require(bind(listener, reinterpret_cast(&address), sizeof(address)) == 0 && + listen(listener, 1) == 0, "loopback bind failed"); + socklen_t length = sizeof(address); + Require(getsockname(listener, reinterpret_cast(&address), &length) == 0, + "loopback address failed"); + peer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + Require(connect(peer, reinterpret_cast(&address), length) == 0, "loopback connect failed"); + auto& retained = g_sockets[0]; + retained = {}; + retained.native = accept(listener, nullptr, nullptr); + retained.generation = 1; + CloseNativeSocket(listener); + Require(retained.native != kInvalidSocket && SetSocketNonBlocking(retained, true), "accept failed"); + } + ~Loopback() { + if (peer != kInvalidSocket) CloseNativeSocket(peer); + if (g_sockets[0].native != kInvalidSocket) DeleteWiiSocket(0); + } + void Send(const char* bytes) { + const int size = static_cast(std::strlen(bytes)); + Require(send(peer, bytes, size, 0) == size, "loopback send failed"); + WaitReady(); + } + void WaitReady() { + NetworkPollContract::NativePollFd descriptor{}; + descriptor.fd = g_sockets[0].native; + descriptor.events = POLLRDNORM; +#ifdef _WIN32 + const int result = WSAPoll(&descriptor, 1, 1000); +#else + const int result = poll(&descriptor, 1, 1000); +#endif + Require(result > 0, "loopback fixture did not become readable"); + } +}; + +void ResetRequest(uint32_t flags = 0, uint32_t size = 16) { + auto& store = GetDeferredNetworkStore(); + Require(store.pendingReceives.empty(), "previous receive was stranded"); + store.syncResults.clear(); + callbacks.clear(); + wakeups = 0; + terminated = invalidOutput = false; + deviceOpen = true; + std::fill(guest.begin(), guest.end(), 0xA5); + Memory::Write32(kBase + 0xE4, kThread); + Memory::Write32(kControl, 0); + Memory::Write32(kControl + 4, flags); + Memory::Write32(kVectors, kControl); + Memory::Write32(kVectors + 4, 8); + Memory::Write32(kVectors + 8, kData); + Memory::Write32(kVectors + 12, size); +} + +auto Start(bool async = false) { + const auto result = async + ? Network_HLE_StartIoctlvAsync(2000, IOCTLV_SO_RECVFROM, 1, 1, kVectors, 0x1234, 0x5678) + : Network_HLE_StartIoctlvSync(2000, IOCTLV_SO_RECVFROM, 1, 1, kVectors, kQueue); + if (!async && result.disposition == StartDisposition::Started) { + Memory::Write16(kThread + 0x2C8, 4); + Memory::Write32(kThread + 0x2DC, kQueue); + Memory::Write32(kThread + 0x2E0, 0); + Memory::Write32(kThread + 0x2E4, 0); + Memory::Write32(kQueue, kThread); + Memory::Write32(kQueue + 4, kThread); + } + return result; +} + +void Pump() { CpuContext cpu{}; Network_HLE_ProcessCompletions(&cpu); } + +void ExpectResult(uint64_t token, int32_t expected, bool async) { + Require(HandleIpTopIoctl(IOCTL_SO_GETLASTERROR, 0, 0, 0, 0) == (expected < 0 ? expected : 0), + "completion published an incorrect last socket error"); + if (async) { + Require(callbacks.size() == 1 && callbacks[0] == std::tuple{0x1234u, expected, 0x5678u}, + "wrong callback result or count"); + } else { + int32_t result = 0; + Require(wakeups == 1 && Network_HLE_TakeSyncResult(token, &result) && result == expected, + "wrong synchronous result"); + Require(!Network_HLE_TakeSyncResult(token, &result), "sync result delivered twice"); + } + Pump(); + Require(GetDeferredNetworkStore().pendingReceives.empty(), "completed receive was retained"); + Require(async ? callbacks.size() == 1 : wakeups == 1, "completion delivered twice"); +} + +void TestReceive(bool async) { + Loopback connection; + ResetRequest(); + auto started = Start(async); + Require(started.disposition == StartDisposition::Started, "empty blocking receive was not parked"); + const auto remaining = GetDeferredNetworkStore().pendingReceives.front().timeout.Deadline() - + NetworkPollContract::Timeout::Clock::now(); + Require(remaining > std::chrono::milliseconds(500) && remaining <= std::chrono::milliseconds(1000), + "receive timeout changed from 1000 milliseconds"); + Pump(); + Require(wakeups == 0 && callbacks.empty() && Memory::Read8(kData) == 0xA5, + "empty receive completed or touched output"); + const auto generation = GxGuestWrite::GenerationForRange(kData, 16); + // The descriptor/control storage can be recycled after async submission. + if (async) { + terminated = true; + Memory::Write32(kVectors + 8, 0xFFFFFFFFu); + Memory::Write32(kControl, 23); + } + connection.Send("abc"); + Pump(); + ExpectResult(started.token, 3, async); + Require(std::memcmp(Memory::GetPointer(kData, 3), "abc", 3) == 0 && Memory::Read8(kData + 3) == 0xA5, + "receive wrote the wrong output range"); + Require(GxGuestWrite::GenerationForRange(kData, 16) != generation, "receive did not invalidate guest data"); + + ResetRequest(); + started = Start(async); + auto& work = GetDeferredNetworkStore().pendingReceives.front(); + work.timeout = NetworkPollContract::Timeout::FromMilliseconds(0); + Pump(); + ExpectResult(started.token, -SO_EAGAIN, async); + Require(Memory::Read8(kData) == 0xA5, "timeout touched output"); + + ResetRequest(); + started = Start(async); + GetDeferredNetworkStore().pendingReceives.front().timeout = NetworkPollContract::Timeout::FromMilliseconds(0); + connection.Send("late"); + Pump(); + ExpectResult(started.token, 4, async); + + ResetRequest(); + started = Start(async); + connection.Send("r"); +#ifdef _WIN32 + injectedReadError = WSAEWOULDBLOCK; +#else + injectedReadError = EWOULDBLOCK; +#endif + Pump(); + Require(GetDeferredNetworkStore().pendingReceives.size() == 1 && wakeups == 0 && callbacks.empty(), + "lost readiness shortened the receive wait"); + Pump(); + ExpectResult(started.token, 1, async); + + ResetRequest(); + started = Start(async); + connection.Send("x"); + invalidOutput = true; + Pump(); + invalidOutput = false; + ExpectResult(started.token, -SO_EINVAL, async); + Require(ReceiveFromNow(0, 0, kData, 16) == 1, "invalid output consumed socket bytes"); + + ResetRequest(); + started = Start(async); + ++g_sockets[0].generation; + connection.Send("g"); + Pump(); + ExpectResult(started.token, -SO_ENOTCONN, async); + Require(ReceiveFromNow(0, 0, kData, 16) == 1, "stale socket generation consumed bytes"); + + ResetRequest(); + started = Start(async); + connection.Send("d"); + deviceOpen = false; + Pump(); + deviceOpen = true; + ExpectResult(started.token, -SO_ENOTCONN, async); + Require(ReceiveFromNow(0, 0, kData, 16) == 1, "closed IOS device consumed bytes"); + + ResetRequest(); + started = Start(async); + connection.Send("e"); +#ifdef _WIN32 + injectedReadError = WSAECONNRESET; +#else + injectedReadError = ECONNRESET; +#endif + Pump(); + ExpectResult(started.token, -SO_ECONNRESET, async); + Require(ReceiveFromNow(0, 0, kData, 16) == 1, "reset injection lost fixture data"); + + ResetRequest(); + started = Start(async); + shutdown(connection.peer, 1); + connection.WaitReady(); + Pump(); + ExpectResult(started.token, 0, async); +} + +void TestCancellationAndModes() { + Loopback connection; + for (int cancellation = 0; cancellation < 4; ++cancellation) { + ResetRequest(); + const auto started = Start(); + Require(started.disposition == StartDisposition::Started, "cancel fixture did not park"); + if (cancellation == 0) terminated = true; + if (cancellation == 1) Memory::Write64(kQueue + 8, started.token + 1); + if (cancellation == 2) Memory::Write32(kThread + 0x2DC, kQueue + 16); + if (cancellation == 3) ++threadGeneration; + connection.Send("c"); + Pump(); + Require(wakeups == 0 && callbacks.empty() && GetDeferredNetworkStore().pendingReceives.empty(), + "cancelled waiter completed"); + Require(Memory::Read8(kData) == 0xA5 && ReceiveFromNow(0, 0, kData, 16) == 1, + "cancelled waiter wrote data or consumed bytes"); + } + ResetRequest(4); + Require(Start().disposition == StartDisposition::NotApplicable && + HandleIpTopIoctlv(IOCTLV_SO_RECVFROM, {{kControl, 8}}, {{kData, 16}}) == -SO_EAGAIN, + "forced nonblocking receive parked"); + ResetRequest(); + g_sockets[0].nonblocking = true; + Require(Start().disposition == StartDisposition::NotApplicable, "nonblocking socket parked"); + g_sockets[0].nonblocking = false; + ResetRequest(0, 0); + const int32_t expectedZero = ReceiveFromNow(0, 0, kData, 0); + auto result = Start(); + Require(result.disposition == StartDisposition::NotApplicable && + HandleIpTopIoctlv(IOCTLV_SO_RECVFROM, {{kControl, 8}}, {{kData, 0}}) == expectedZero, + "zero-size stream receive parked"); + ResetRequest(2); + connection.Send("peek"); + result = Start(); + Require(result.disposition == StartDisposition::ImmediateResult && result.result == 4 && + ReceiveFromNow(0, 0, kData, 16) == 4, "MSG_PEEK consumed bytes"); + Require(SetSocketNonBlocking(g_sockets[0], false), "blocking fixture failed"); + ResetRequest(); + result = Start(); + Require(result.disposition == StartDisposition::Started && !g_sockets[0].nativeNonblocking, + "receive did not restore native blocking mode"); + AbortPendingDeferredReceives(0, -SO_ENOTCONN); + Pump(); + ExpectResult(result.token, -SO_ENOTCONN, false); + ResetRequest(); + Memory::Write32(kVectors + 4, 4); + result = Start(); + Require(result.disposition == StartDisposition::ImmediateResult && result.result == -SO_EINVAL, + "short control vector accepted"); + result = Network_HLE_StartIoctlvSync(2000, IOCTLV_SO_RECVFROM, 0xFFFFFFFFu, 2, kVectors, kQueue); + Require(result.disposition == StartDisposition::ImmediateResult && result.result == -SO_EINVAL, + "overflowed vector count accepted"); + ResetRequest(); + Memory::Write32(kVectors + 16, kData + 64); + Memory::Write32(kVectors + 20, 8); + result = Network_HLE_StartIoctlvSync(2000, IOCTLV_SO_RECVFROM, 1, 2, kVectors, kQueue); + Require(result.disposition == StartDisposition::NotApplicable && + HandleIpTopIoctlv(IOCTLV_SO_RECVFROM, {{kControl, 8}}, {{kData, 16}, {kData + 64, 8}}) == -SO_EAGAIN, + "source-address receive parked or waited"); +} + +void TestCloseAndShutdown(bool async) { + for (int action = 0; action < 3; ++action) { + Loopback connection; + ResetRequest(); + const auto started = Start(async); + Require(started.disposition == StartDisposition::Started, "teardown fixture did not park"); + if (action == 0) { + Require(DeleteWiiSocket(0) == 0, "socket close failed"); + } else if (action == 1) { + CleanupAllWiiSockets(); + } else { + Memory::Write32(kControl + 4, 0); + Require(HandleIpTopIoctl(IOCTL_SO_SHUTDOWN, kControl, 8, 0, 0) == 0, + "read shutdown failed"); + } + Pump(); + ExpectResult(started.token, action == 2 ? 0 : -SO_ENOTCONN, async); + Require(Memory::Read8(kData) == 0xA5, "teardown touched receive data"); + } +} +} + +int main() { +#ifdef _WIN32 + WSADATA data{}; + if (WSAStartup(MAKEWORD(2, 2), &data) != 0) return 1; +#endif + try { + TestReceive(false); + TestReceive(true); + TestCancellationAndModes(); + TestCloseAndShutdown(false); + TestCloseAndShutdown(true); + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +#ifdef _WIN32 + WSACleanup(); +#endif + return 0; +} diff --git a/runtime/tests/pad_trigger_support/hle_stubs.h b/runtime/tests/pad_trigger_support/hle_stubs.h new file mode 100644 index 000000000..a0a56e35d --- /dev/null +++ b/runtime/tests/pad_trigger_support/hle_stubs.h @@ -0,0 +1,4 @@ +#pragma once + +#define PPC_NATIVE_OVERRIDE(...) +#define PPC_NATIVE_OVERRIDE_VOID(...) diff --git a/runtime/tests/pad_trigger_support/memory.h b/runtime/tests/pad_trigger_support/memory.h new file mode 100644 index 000000000..4714ae5cc --- /dev/null +++ b/runtime/tests/pad_trigger_support/memory.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include + +class Memory { +public: + class AccessViolation : public std::runtime_error { + public: + using std::runtime_error::runtime_error; + }; + + static uint8_t* GetPointer(uint32_t address, size_t length); +}; diff --git a/runtime/tests/pad_trigger_tests.cpp b/runtime/tests/pad_trigger_tests.cpp new file mode 100644 index 000000000..24d64207c --- /dev/null +++ b/runtime/tests/pad_trigger_tests.cpp @@ -0,0 +1,129 @@ +#include "pad_trigger_support/hle_stubs.h" +#include +#include + +#include +#include + +namespace { +struct TestPort { + bool assigned = true; + bool connected = true; + std::vector primary; + std::vector alternate; + std::array held{}; +}; +std::array g_ports; +bool g_blocked = false; + +bool TestGamepadButton(SDL_Gamepad* gamepad, SDL_GamepadButton button) { + const auto port = reinterpret_cast(gamepad) - 1; + return g_ports.at(port).held.at(button); +} +} + +#define SDL_GetGamepadButton TestGamepadButton +#include "../src/hle/input/pad.cpp" +#undef SDL_GetGamepadButton + +namespace InputBindings { +bool InputBlocked() noexcept { return g_blocked; } +void Apply(PADStatus*) noexcept {} +} + +namespace MotionTrick { +void Apply(PADStatus*) noexcept {} +} + +BOOL PADInit() { return TRUE; } +BOOL PADReset(u32) { return TRUE; } +BOOL PADRecalibrate(u32) { return TRUE; } +void PADControlMotor(u32, u32) {} +u32 PADRead(PADStatus*) { return 0; } +s32 PADGetIndexForPort(u32 port) { return g_ports.at(port).assigned ? static_cast(port) : -1; } +SDL_Gamepad* PADGetSDLGamepadForIndex(u32 index) { + return g_ports.at(index).connected ? reinterpret_cast(static_cast(index) + 1) : nullptr; +} +PADButtonMapping* PADGetButtonMappings(u32 port, u32* count) { + auto& mappings = g_ports.at(port).primary; + *count = static_cast(mappings.size()); + return mappings.empty() ? nullptr : mappings.data(); +} +PADButtonMapping* PADGetAltButtonMappings(u32 port, u32* count) { + auto& mappings = g_ports.at(port).alternate; + *count = static_cast(mappings.size()); + return mappings.empty() ? nullptr : mappings.data(); +} +uint8_t* Memory::GetPointer(uint32_t, size_t) { + static std::array bytes{}; + return bytes.data(); +} + +namespace { +int g_failures = 0; + +void Check(bool ok, const char* what) { + if (!ok) { + std::printf("FAIL: %s\n", what); + ++g_failures; + } +} + +std::array ReadTriggers() { + std::array statuses{}; + FillTriggersHeldByButtons(statuses.data()); + return statuses; +} +} + +int main() { + g_ports[0].primary = {{SDL_GAMEPAD_BUTTON_SOUTH, PAD_BUTTON_A}, + {SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, PAD_TRIGGER_L}}; + g_ports[0].alternate = {{SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, PAD_TRIGGER_R}}; + g_ports[0].held[SDL_GAMEPAD_BUTTON_LEFT_SHOULDER] = true; + g_ports[0].held[SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER] = true; + auto status = ReadTriggers(); + Check(status[0].triggerLeft == 255, "primary mapping count reaches scanner"); + Check(status[0].triggerRight == 255, "alternate mapping count reaches scanner independently"); + + g_ports[0].held[SDL_GAMEPAD_BUTTON_LEFT_SHOULDER] = false; + status = ReadTriggers(); + Check(status[0].triggerLeft == 0 && status[0].triggerRight == 255, "released buttons do not pull triggers"); + g_ports[0].held[SDL_GAMEPAD_BUTTON_LEFT_SHOULDER] = true; + + g_ports[0].primary.clear(); + g_ports[0].alternate = {{SDL_GAMEPAD_BUTTON_SOUTH, PAD_BUTTON_A}, + {SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, PAD_TRIGGER_L}, + {SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, PAD_TRIGGER_R}}; + status = ReadTriggers(); + Check(status[0].triggerLeft == 255 && status[0].triggerRight == 255, + "nonempty alternate mappings work with an empty primary set"); + + g_ports[0].alternate = {{PAD_NATIVE_BUTTON_INVALID, PAD_TRIGGER_L}, + {SDL_GAMEPAD_BUTTON_COUNT, PAD_TRIGGER_R}, + {SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, PAD_BUTTON_A}}; + status = ReadTriggers(); + Check(status[0].triggerLeft == 0 && status[0].triggerRight == 0, + "invalid native buttons and unrelated bindings do not pull triggers"); + + for (auto& port : g_ports) { + port.primary = {{SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, PAD_TRIGGER_L}}; + port.alternate.clear(); + port.held[SDL_GAMEPAD_BUTTON_LEFT_SHOULDER] = true; + } + g_ports[1].assigned = false; + g_ports[2].connected = false; + status = ReadTriggers(); + Check(status[0].triggerLeft == 255 && status[3].triggerLeft == 255, "connected ports are scanned"); + Check(status[1].triggerLeft == 0 && status[2].triggerLeft == 0, "unassigned and disconnected ports are skipped"); + + status = {}; + status[0].err = PAD_ERR_NO_CONTROLLER; + status[0].triggerLeft = 17; + FillTriggersHeldByButtons(status.data()); + Check(status[0].triggerLeft == 17, "error statuses retain their original trigger values"); + g_blocked = true; + status = ReadTriggers(); + Check(status[0].triggerLeft == 0 && status[3].triggerLeft == 0, "blocked input is withheld"); + return g_failures == 0 ? 0 : 1; +} diff --git a/runtime/tests/ppc_cr_logical_tests.cpp b/runtime/tests/ppc_cr_logical_tests.cpp new file mode 100644 index 000000000..e401b4e7d --- /dev/null +++ b/runtime/tests/ppc_cr_logical_tests.cpp @@ -0,0 +1,73 @@ +#include "isa/ppc_isa_cr.h" + +#include +#include +#include +#include + +namespace { + +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +void CheckLogical() { + constexpr uint32_t truth[8][4] = { + {1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {1, 1, 1, 0}, + {0, 0, 0, 1}, {1, 0, 0, 1}, {1, 0, 1, 1}, {0, 1, 1, 1} + }; + for (uint32_t op = 0; op < 8; ++op) + for (uint32_t ba = 0; ba < 32; ++ba) + for (uint32_t bb = 0; bb < 32; ++bb) + for (uint32_t bt = 0; bt < 32; ++bt) + for (uint32_t seed : {0u, 0xFFFFFFFFu, 0xA5C39678u, 0x5A3C6987u}) + for (uint32_t bits = 0; bits < 4; ++bits) { + const uint32_t aMask = 1u << (31u - ba); + const uint32_t bMask = 1u << (31u - bb); + uint32_t initial = (seed & ~aMask) | ((bits & 2u) ? aMask : 0u); + initial = (initial & ~bMask) | ((bits & 1u) ? bMask : 0u); + const uint32_t a = (initial >> (31u - ba)) & 1u; + const uint32_t b = (initial >> (31u - bb)) & 1u; + const uint32_t mask = 1u << (31u - bt); + const uint32_t expected = (initial & ~mask) | (truth[op][a * 2u + b] ? mask : 0u); + Require(PpcCrLogicalResident(initial, op, bt, ba, bb) == expected, "resident CR logical truth table"); + } +} + +void CheckNorCr0() { + struct Case { uint32_t left, right, result, cr0; }; + constexpr Case cases[] = { + {0u, 0u, 0xFFFFFFFFu, 8u}, + {0x80000000u, 0u, 0x7FFFFFFFu, 4u}, + {0xFFFFFFFFu, 0u, 0u, 2u}, + {0x7FFFFFFFu, 0u, 0x80000000u, 8u}, + {0x80000000u, 0x7FFFFFFEu, 1u, 4u}, + {0xAAAAAAAAu, 0x55555555u, 0u, 2u} + }; + for (const auto& test : cases) + for (uint32_t xer : {0u, 0x7FFFFFFFu, 0x80000000u, 0xFFFFFFFFu}) { + const uint32_t result = ~(test.left | test.right); + Require(result == test.result, "nor result"); + uint32_t cr = 0xF5A39678u; + SetCRResident(cr, xer, 0, static_cast(result), int32_t{0}); + const uint32_t expected = ((test.cr0 | (xer >> 31)) << 28) | 0x05A39678u; + Require(cr == expected, "nor CR0 LT/GT/EQ/SO and other fields"); + Require(GetCRBitResident(cr, 0, 0) == (test.cr0 == 8u), "nor LT branch"); + Require(GetCRBitResident(cr, 0, 1) == (test.cr0 == 4u), "nor GT branch"); + Require(GetCRBitResident(cr, 0, 2) == (test.cr0 == 2u), "nor EQ branch"); + Require(GetCRBitResident(cr, 0, 3) == ((xer >> 31) != 0), "nor SO branch"); + } +} + +} + +int main() { + try { + CheckLogical(); + CheckNorCr0(); + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; + } +} diff --git a/runtime/tests/psq_gqr_state_free_tests.cpp b/runtime/tests/psq_gqr_state_free_tests.cpp new file mode 100644 index 000000000..9e2dd4f56 --- /dev/null +++ b/runtime/tests/psq_gqr_state_free_tests.cpp @@ -0,0 +1,96 @@ +#include "isa/ppc_isa_quantized.h" + +#include +#include +#include + +static void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +template +static double ReferenceLoad(uint32_t type, uint32_t scale, uint32_t address) { + if constexpr (W == 0) { + switch (type) { + case 0: return PpcLoadPairPsqFloatFastInline(address); + case 4: return PpcLoadPairPsqIntegerFastInline(address, scale); + case 5: return PpcLoadPairPsqIntegerFastInline(address, scale); + case 6: return PpcLoadPairPsqIntegerFastInline(address, scale); + case 7: return PpcLoadPairPsqIntegerFastInline(address, scale); + } + } else { + switch (type) { + case 0: return PpcLoadSinglePsqFloatFastInline(address); + case 4: return PpcLoadSinglePsqQuantizedFastInline(address, scale); + case 5: return PpcLoadSinglePsqQuantizedFastInline(address, scale); + case 6: return PpcLoadSinglePsqQuantizedFastInline(address, scale); + case 7: return PpcLoadSinglePsqQuantizedFastInline(address, scale); + } + } + std::abort(); +} + +template +static void ReferenceStore(uint32_t type, uint32_t scale, uint32_t address, double value) { + if constexpr (W == 0) { + switch (type) { + case 0: PpcStorePairPsqFloatFastInline(address, value); return; + case 4: PpcStorePairPsqQuantizedFastInline(address, value, scale); return; + case 5: PpcStorePairPsqQuantizedFastInline(address, value, scale); return; + case 6: PpcStorePairPsqQuantizedFastInline(address, value, scale); return; + case 7: PpcStorePairPsqQuantizedFastInline(address, value, scale); return; + } + } else { + switch (type) { + case 0: PpcStoreSinglePsqFloatFastInline(address, value); return; + case 4: PpcStoreSinglePsqQuantizedFastInline(address, value, scale); return; + case 5: PpcStoreSinglePsqQuantizedFastInline(address, value, scale); return; + case 6: PpcStoreSinglePsqQuantizedFastInline(address, value, scale); return; + case 7: PpcStoreSinglePsqQuantizedFastInline(address, value, scale); return; + } + } + std::abort(); +} + +template +static void Check() { + constexpr uint64_t values[] = { + 0x3F800000C0200000ull, 0x0000000080000000ull, + 0x477FFF00C7000000ull, 0x7FC01234FFC01234ull + }; + for (uint32_t type : {0u, 4u, 5u, 6u, 7u}) + for (uint32_t scale = 0; scale < 64; ++scale) + for (uint64_t bits : values) { + const uint32_t half = (scale << 8) | type; + const uint32_t loadGqr = (half << 16) | 0x2105u; + const uint32_t storeGqr = half | 0x21050000u; + constexpr uint32_t address = 32; + PsqTestMemory::bytes.fill(0xCD); + BigEndian::Write64(PsqTestMemory::Pointer(address), bits); + const auto expected = PpcBitCastToU64Inline(ReferenceLoad(type, scale, address)); + const auto loaded = PpcBitCastToU64Inline(PPC_PsqLStateInline(loadGqr, address)); + Require(loaded == expected, "state-free GQR load bits"); + + const double value = PpcBitCastToDoubleInline(bits); + PsqTestMemory::bytes.fill(0xCD); + ReferenceStore(type, scale, address, value); + const auto expectedBytes = PsqTestMemory::bytes; + PsqTestMemory::bytes.fill(0xCD); + PPC_PsqStStateInline(storeGqr, address, value); + Require(PsqTestMemory::bytes == expectedBytes, "state-free GQR store bytes and untouched lanes"); + } +} + +template static void CheckIndex() { Check<0, I>(); Check<1, I>(); } + +int main() { + try { + Require(TryGetCpuContext() == nullptr, "state-free PSQ needs no current context"); + CheckIndex<0>(); CheckIndex<1>(); CheckIndex<2>(); CheckIndex<3>(); + CheckIndex<4>(); CheckIndex<5>(); CheckIndex<6>(); CheckIndex<7>(); + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; + } +} diff --git a/runtime/tests/runtime_config_io_tests.cpp b/runtime/tests/runtime_config_io_tests.cpp new file mode 100644 index 000000000..c0ed38391 --- /dev/null +++ b/runtime/tests/runtime_config_io_tests.cpp @@ -0,0 +1,118 @@ +#include "runtime_config.h" + +#include +#include +#include +#include + +namespace { +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} +std::string Read(const std::filesystem::path& path) { + std::ifstream input(path, std::ios::binary); + return {std::istreambuf_iterator(input), std::istreambuf_iterator()}; +} +void Write(const std::filesystem::path& path, const std::string& text) { + std::ofstream output(path, std::ios::binary); + output << text; + output.close(); + Require(static_cast(output), "test fixture write failed"); +} +struct FailedWrite : RuntimeHostFile::FileOperations { + static size_t Write(FILE* file, const char* data, size_t size) { + if (std::ftell(file) != 0) { errno = ENOSPC; return 0; } + return FileOperations::Write(file, data, std::min(size, size_t{3})); + } +}; +void Reject(const std::filesystem::path& path, const std::string& text) { + Write(path, text); + Require(!RuntimeConfigFile::WriteSettingFile(path, "video", "show_fps", "true"), "invalid config was updated"); + Require(Read(path) == text, "invalid config bytes were changed"); +} +} // namespace + +int main() { + const auto root = std::filesystem::temp_directory_path() / ("wiicomp-config-io-" + std::to_string( + std::chrono::steady_clock::now().time_since_epoch().count())); + bool claimed = false; + try { + claimed = std::filesystem::create_directory(root); + Require(claimed, "cannot claim test directory"); + const auto path = root / "Config.toml"; + const std::string original = "# keep\n[paths]\ndvd_root = 'a/b'\n[video]\nshow_fps = false\n"; + Write(path, original); + Require(!RuntimeConfigFile::WriteSettingFile(path, "video", "show_fps", "true"), "write failure was ignored"); + Require(Read(path) == original, "failed config update lost previous settings"); + Require(std::distance(std::filesystem::directory_iterator(root), std::filesystem::directory_iterator{}) == 1, + "failed config update left a temporary file"); + Require(RuntimeConfigFile::WriteSettingFile(path, "video", "show_fps", "true"), "config update failed"); + auto config = toml::parse_str(Read(path)); + Require(config.at("video").at("show_fps").as_boolean() && + config.at("paths").at("dvd_root").as_string() == "a/b" && Read(path).find("# keep") != std::string::npos, + "config update lost unrelated fields or comments"); + + Reject(path, "[video]\nshow_fps = false\nshow_fps = true\n"); + Reject(path, "[video]\nshow_fps = false\n[video]\n"); + Reject(path, "video = 1\n"); + Reject(path, "[[video]]\nshow_fps = false\n"); + Reject(path, "[video\nshow_fps = false\n"); + Write(path, original); + Require(!RuntimeConfigFile::WriteSettingFile(path, "video", "show_fps", "["), "invalid value was accepted"); + Require(Read(path) == original, "invalid value changed the document"); + Write(path, "[video]\nshow_fps = false\n"); + Require(RuntimeConfigFile::WriteSettingFile(path, "controller.wii_accel_offsets", "x", "0.5"), "missing parent tables were not created"); + config = toml::parse_str(Read(path)); + Require(config.at("controller").at("wii_accel_offsets").at("x").as_floating() == 0.5, + "new nested setting was lost"); + + std::vector writers; + std::atomic successes{0}; + for (unsigned i = 0; i < 12; ++i) { + writers.emplace_back([&, i] { + if (RuntimeConfigFile::WriteSettingFile(path, "parallel", "value_" + std::to_string(i), std::to_string(i))) ++successes; + }); + } + for (auto& writer : writers) writer.join(); + config = toml::parse_str(Read(path)); + Require(successes == 12 && config.at("parallel").as_table().size() == 12, "concurrent updates lost settings"); + for (unsigned i = 0; i < 12; ++i) { + Require(config.at("parallel").at("value_" + std::to_string(i)).as_integer() == i, "concurrent value was corrupted"); + } + + const auto absent = root / "new" / "Config.toml"; + Require(RuntimeConfigFile::WriteSettingFile(absent, "video", "show_fps", "true"), "missing config was not created"); + const auto directory = root / "directory.toml"; + std::filesystem::create_directory(directory); + Require(!RuntimeConfigFile::WriteSettingFile(directory, "video", "show_fps", "true") && + std::filesystem::is_directory(directory), "unreadable directory config was replaced"); +#ifndef _WIN32 + Write(path, original); + std::filesystem::permissions(path, std::filesystem::perms::none); + if (::geteuid() != 0) { + Require(!RuntimeConfigFile::WriteSettingFile(path, "video", "show_fps", "true"), "unreadable config was rewritten"); + } + std::filesystem::permissions(path, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write); + Require(Read(path) == original, "unreadable config bytes changed"); +#else + Write(path, original); + HANDLE locked = CreateFileW(path.c_str(), GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr); + Require(locked != INVALID_HANDLE_VALUE, "cannot lock config fixture"); + const bool updatedLocked = RuntimeConfigFile::WriteSettingFile(path, "video", "show_fps", "true"); + CloseHandle(locked); + Require(!updatedLocked && Read(path) == original, "locked config was replaced from an empty read"); + locked = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); + Require(locked != INVALID_HANDLE_VALUE, "cannot lock config replacement"); + const bool replacedLocked = RuntimeConfigFile::WriteSettingFile(path, "video", "show_fps", "true"); + CloseHandle(locked); + Require(!replacedLocked && Read(path) == original, "failed config replacement changed previous bytes"); +#endif + std::filesystem::remove_all(root); + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + std::error_code ignored; + if (claimed) std::filesystem::remove_all(root, ignored); + return 1; + } + return 0; +} diff --git a/runtime/tests/u8_archive_tests.cpp b/runtime/tests/u8_archive_tests.cpp new file mode 100644 index 000000000..9022555f7 --- /dev/null +++ b/runtime/tests/u8_archive_tests.cpp @@ -0,0 +1,78 @@ +#include "u8_archive.h" + +#include +#include +#include +#include + +namespace { +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} +void Word(std::vector& data, size_t offset, uint32_t value) { + for (unsigned i = 0; i < 4; ++i) data.at(offset + i) = static_cast(value >> ((3 - i) * 8)); +} +void Node(std::vector& data, unsigned index, uint32_t typeAndName, uint32_t offset, uint32_t size) { + const size_t at = 0x20 + index * 12; + Word(data, at, typeAndName); Word(data, at + 4, offset); Word(data, at + 8, size); +} +std::vector Archive() { + std::vector data(0x110); + Word(data, 0, 0x55AA382D); Word(data, 4, 0x20); Word(data, 8, 6 * 12 + 30); Word(data, 12, 0x100); + Node(data, 0, 0x01000000, 0, 6); + Node(data, 1, 0x01000001, 0, 5); + Node(data, 2, 0x01000005, 1, 4); + Node(data, 3, 9, 0x100, 4); + Node(data, 4, 0x01000015, 1, 5); + Node(data, 5, 27, 0x110, 0); + const char names[] = "\0dir\0sub\0RFL_Res.dat\0empty\0ok"; + std::copy(std::begin(names), std::end(names), data.begin() + 0x20 + 6 * 12); + data[0x100] = 1; data[0x101] = 2; data[0x102] = 3; data[0x103] = 4; + return data; +} +void Reject(const std::vector& data, const char* message) { + std::vector out{99}; + Require(!RuntimeU8::Extract(data, "RFL_Res.dat", out) && out == std::vector{99}, message); +} +} // namespace + +int main() { + try { + const auto valid = Archive(); + std::vector out; + Require(RuntimeU8::Extract(valid, "dir/sub/RFL_Res.dat", out) && out == std::vector({1,2,3,4}), "nested full-path extraction failed"); + Require(RuntimeU8::Extract(valid, "RFL_Res.dat", out), "basename extraction failed"); + Require(RuntimeU8::Extract(valid, "ok", out) && out.empty(), "zero-length EOF payload was rejected"); + Require(!RuntimeU8::Extract(valid, "missing", out), "missing target was found"); + auto data = valid; Word(data, 4, 0xfffffff8); Reject(data, "wrapped root was accepted"); + data = valid; Word(data, 4, static_cast(data.size() - 11)); Reject(data, "truncated root was accepted"); + data = valid; Word(data, 8, 0xffffffff); Reject(data, "wrapped metadata was accepted"); + data = valid; Word(data, 8, 6 * 12 - 1); Reject(data, "truncated node table was accepted"); + data = valid; Word(data, 0x28, 0xffffffff); Reject(data, "huge node count was accepted"); + data = valid; Word(data, 0x28, 0); Reject(data, "zero node count was accepted"); + data = valid; Word(data, 0x20, 0); Reject(data, "file root was accepted"); + data = valid; Word(data, 0x24, 1); Reject(data, "invalid root parent was accepted"); + data = valid; Word(data, 0x20 + 3 * 12, 0x00ffffff); Reject(data, "out-of-range name offset was accepted"); + data = valid; std::fill(data.begin() + 0x20 + 6 * 12 + 9, data.begin() + 0x20 + 6 * 12 + 30, 'x'); + Reject(data, "name terminated only in padding was accepted"); + data = valid; Word(data, 0x20 + 2 * 12 + 8, 6); Reject(data, "child directory escaped its parent"); + data = valid; Word(data, 0x20 + 2 * 12 + 8, 2); Reject(data, "backward directory range was accepted"); + data = valid; Word(data, 0x20 + 2 * 12 + 4, 0); Reject(data, "wrong nested parent was accepted"); + data = valid; Word(data, 0x20 + 5 * 12, 0x0200001b); Reject(data, "unknown node type was accepted"); + data = valid; Word(data, 0x20 + 3 * 12 + 4, 0xfffffffe); Word(data, 0x20 + 3 * 12 + 8, 4); + Reject(data, "wrapped matching payload was accepted"); + data = valid; Word(data, 0x20 + 5 * 12 + 4, 0xffffffff); Reject(data, "out-of-range empty payload was accepted"); + data = valid; Word(data, 0x20 + 5 * 12 + 4, 0x110); Word(data, 0x20 + 5 * 12 + 8, 1); + Reject(data, "invalid later unselected payload was ignored"); + data = valid; Word(data, 0x20 + 5 * 12, 0x00ffffff); Reject(data, "invalid later name was ignored"); + data = valid; Word(data, 12, 0xffffffff); + Require(RuntimeU8::Extract(data, "RFL_Res.dat", out), "unused header data offset constrained absolute file offsets"); + data = valid; Word(data, 0x20 + 3 * 12 + 4, 0x103); Word(data, 0x20 + 3 * 12 + 8, 1); + Require(RuntimeU8::Extract(data, "RFL_Res.dat", out) && out == std::vector{4}, "unaligned payload was rejected"); + for (size_t size = 0; size < 0x20; ++size) Reject(std::vector(size), "short header was accepted"); + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } + return 0; +} diff --git a/runtime/tests/windows_seh_order_tests.cmake b/runtime/tests/windows_seh_order_tests.cmake new file mode 100644 index 000000000..ac6049761 --- /dev/null +++ b/runtime/tests/windows_seh_order_tests.cmake @@ -0,0 +1,28 @@ +if(NOT DEFINED SEH_SOURCE) + message(FATAL_ERROR "SEH_SOURCE is required") +endif() +file(READ "${SEH_SOURCE}" source) +string(FIND "${source}" "LONG CALLBACK SehLogger(EXCEPTION_POINTERS* info)" begin) +string(FIND "${source}" "LONG WINAPI UnhandledSehFilter" end) +if(begin LESS 0 OR end LESS begin) + message(FATAL_ERROR "Cannot locate the production first-chance handler") +endif() +math(EXPR length "${end} - ${begin}") +string(SUBSTRING "${source}" ${begin} ${length} handler) +string(FIND "${handler}" "GuestFlat::HandleAccessViolation(" guest) +string(FIND "${handler}" "ExceptionCode == kCppExceptionCodeGcc" gcc) +string(FIND "${handler}" "ExceptionCode == kCppExceptionCodeMsvc" msvc) +string(FIND "${handler}" "ExceptionCode & 0x20000000u" customer) +string(FIND "${handler}" "if (g_suppressSehReporting && g_sehJumpTarget)" recovery) +string(FIND "${handler}" "longjmp(" jump) +string(FIND "${handler}" "return ReportFatalSehAndExit(info);" fatal) +if(guest LESS 0 OR gcc LESS guest OR msvc LESS gcc OR customer LESS msvc OR + recovery LESS customer OR jump LESS recovery OR fatal LESS jump) + message(FATAL_ERROR "First-chance software exceptions must pass through after GuestFlat and before constructor recovery") +endif() +math(EXPR filterLength "${recovery} - ${gcc}") +string(SUBSTRING "${handler}" ${gcc} ${filterLength} filter) +string(FIND "${filter}" "return EXCEPTION_CONTINUE_SEARCH;" passthrough) +if(passthrough LESS 0) + message(FATAL_ERROR "Software exceptions do not return to their frame handlers") +endif() diff --git a/translator/src/Translator.Core/CodeGen/CxxLinearCodeGenerator.EmitInstruction.cs b/translator/src/Translator.Core/CodeGen/CxxLinearCodeGenerator.EmitInstruction.cs index a561e0a89..229e9db6b 100644 --- a/translator/src/Translator.Core/CodeGen/CxxLinearCodeGenerator.EmitInstruction.cs +++ b/translator/src/Translator.Core/CodeGen/CxxLinearCodeGenerator.EmitInstruction.cs @@ -264,7 +264,9 @@ bool IsFloat(string destination) => var pairHelper = pair.ElementSizeBytes == 2 ? $"MemoryInline::ReadResolvedPair16({pair.Range}, {pair.RangeOffset}u)" : $"MemoryInline::ReadResolvedPair32({pair.Range}, {pair.RangeOffset}u)"; - string FastValue(string member, bool isFloat) => isFloat ? $"PpcBitCastToFloatInline(resolved_pair.{member})" : $"resolved_pair.{member}"; + string FastValue(string member, bool isFloat) => isFloat + ? $"PpcBitCastToDoubleInline(PpcConvertToDoubleBitsInline(resolved_pair.{member}))" + : $"resolved_pair.{member}"; string SlowHelper(bool isFloat) => isFloat ? $"Float{Bits(pair.ElementSizeBytes)}" : Bits(pair.ElementSizeBytes).ToString(); var firstMember = pair.Descending ? "second" : "first"; var secondMember = pair.Descending ? "first" : "second"; diff --git a/translator/src/Translator.Core/Disassembly/PpcDecoder.Primary31.cs b/translator/src/Translator.Core/Disassembly/PpcDecoder.Primary31.cs index f74bd8f36..0abacfef4 100644 --- a/translator/src/Translator.Core/Disassembly/PpcDecoder.Primary31.cs +++ b/translator/src/Translator.Core/Disassembly/PpcDecoder.Primary31.cs @@ -327,21 +327,19 @@ private static void DecodePrimary31(uint address, uint word, List op operands.Add(Reg(rb)); break; - case 498: // stwcx. rS,rA,rB - mnemonic = "stwcx."; - operands.Add(Reg(rs)); - operands.Add(Reg(ra)); - operands.Add(Reg(rb)); - isCond = true; - break; - - - default: // X-form instructions use the full 10-bit XO (no OE bit) // Handle them here with a secondary switch on xo10 switch (xo10) { + case 150 when rc: // stwcx. rS,rA,rB + mnemonic = "stwcx."; + operands.Add(Reg(rs)); + operands.Add(Reg(ra)); + operands.Add(Reg(rb)); + isCond = true; + break; + case 23: // lwzx rD,rA,rB mnemonic = "lwzx"; operands.Add(Reg(rs)); diff --git a/translator/src/Translator.Core/Lifting/PpcLifter.cs b/translator/src/Translator.Core/Lifting/PpcLifter.cs index bb57f8680..256f01c52 100644 --- a/translator/src/Translator.Core/Lifting/PpcLifter.cs +++ b/translator/src/Translator.Core/Lifting/PpcLifter.cs @@ -717,7 +717,8 @@ private static IReadOnlyList LiftInstruction( return new[] { new IrBinary(operands[0], IrValue.Register(operands[1]), IrValue.Imm(MaskUimm(ParseImmediate(operands[2]))), "or") }; case "nor" when operands.Count == 3: - return new[] { new IrBinary(operands[0], IrValue.Register(operands[1]), IrValue.Register(operands[2]), "nor") }; + case "nor." when operands.Count == 3: + return BinWithCrFlag(Reg(0), IrValue.Register(Reg(1)), IrValue.Register(Reg(2)), "nor", mnemonic.EndsWith('.')); case "nand" when operands.Count == 3: return new[] { new IrBinary(operands[0], IrValue.Register(operands[1]), IrValue.Register(operands[2]), "nand") }; diff --git a/translator/src/Translator.Core/Translation/GqrConstantPropagation.cs b/translator/src/Translator.Core/Translation/GqrConstantPropagation.cs index 630e6515e..831be84ef 100644 --- a/translator/src/Translator.Core/Translation/GqrConstantPropagation.cs +++ b/translator/src/Translator.Core/Translation/GqrConstantPropagation.cs @@ -35,14 +35,15 @@ public static GqrConstantFlow Analyze( { var isEntry = Comparer.Equals(block.Label, function.EntryLabel); var predecessors = cfg.Predecessors(block.Label) - .Where(p => !Comparer.Equals(p, block.Label) && outputInitialized[p]).ToArray(); + .Where(p => outputInitialized[p]).ToArray(); // An uninitialized predecessor is lattice top, not an empty // constant map. Do not manufacture a bottom state for blocks // that have not become reachable from the entry yet. if (!isEntry && predecessors.Length == 0) continue; var state = isEntry - ? new Dictionary(entryConstants ?? new Dictionary(), Comparer) + ? Intersect(predecessors.Select(p => output[p]).Prepend( + new Dictionary(entryConstants ?? new Dictionary(), Comparer))) : Intersect(predecessors.Select(p => output[p])); var newInput = new Dictionary(state, Comparer); foreach (var instruction in block.Instructions) diff --git a/translator/src/Translator.Core/Translation/GqrInterproceduralAnalysis.cs b/translator/src/Translator.Core/Translation/GqrInterproceduralAnalysis.cs index 4c44dfe36..30a2ab8ca 100644 --- a/translator/src/Translator.Core/Translation/GqrInterproceduralAnalysis.cs +++ b/translator/src/Translator.Core/Translation/GqrInterproceduralAnalysis.cs @@ -338,7 +338,7 @@ private static void EvaluateCompactCaller( for (var i = 0; i < blockPredecessors.Length; i++) { var predecessor = blockPredecessors[i]; - if (predecessor != blockIndex && outputInitialized[predecessor]) + if (outputInitialized[predecessor]) predecessors[predecessorCount++] = predecessor; } // An uninitialized predecessor is lattice top, not an empty @@ -352,6 +352,7 @@ private static void EvaluateCompactCaller( for (var slot = 0; slot < GqrCount; slot++) if ((entryMask & (1 << slot)) != 0) state[slot] = entryValues[entryBase + slot]; + CompactIntersectWith(output, predecessors, predecessorCount, state); } else { @@ -699,6 +700,26 @@ private static void CompactIntersectInto( } } + private static void CompactIntersectWith( + Dictionary[] output, + int[] predecessors, + int predecessorCount, + Dictionary state) + { + List? removed = null; + foreach (var pair in state) + { + for (var i = 0; i < predecessorCount; i++) + { + if (output[predecessors[i]].TryGetValue(pair.Key, out var value) && value == pair.Value) continue; + (removed ??= new List()).Add(pair.Key); + break; + } + } + if (removed is null) return; + foreach (var key in removed) state.Remove(key); + } + private static bool CompactSame( Dictionary left, Dictionary right) diff --git a/translator/tests/Translator.Tests/FusedFloatLoadCodeGenTests.cs b/translator/tests/Translator.Tests/FusedFloatLoadCodeGenTests.cs new file mode 100644 index 000000000..be3c1d537 --- /dev/null +++ b/translator/tests/Translator.Tests/FusedFloatLoadCodeGenTests.cs @@ -0,0 +1,69 @@ +using Translator.Core.Analysis.Representation; +using Translator.Core.Analysis.Ssa; +using Translator.Core.CodeGen; +using Translator.Core.Ir; +using Translator.Core.Representation; + +namespace Translator.Tests; + +public sealed class FusedFloatLoadCodeGenTests +{ + [Theory] + [InlineData(false, true, true)] + [InlineData(true, true, true)] + [InlineData(false, true, false)] + [InlineData(true, true, false)] + [InlineData(false, false, true)] + [InlineData(true, false, true)] + public void FusedFloatLoadsWidenBitsWithoutHostFloatConversionAndKeepFallbacks( + bool descending, bool firstFloat, bool secondFloat) + { + var code = Emit(descending, firstFloat, secondFloat, 4); + Assert.Contains("MemoryInline::ReadResolvedPair32(range, 0u)", code, StringComparison.Ordinal); + Assert.Contains("if (resolved_pair.valid)", code, StringComparison.Ordinal); + Assert.DoesNotContain("PpcBitCastToFloatInline(resolved_pair.", code, StringComparison.Ordinal); + + CheckMember(firstFloat, descending ? "second" : "first", descending ? 4 : 0); + CheckMember(secondFloat, descending ? "first" : "second", descending ? 0 : 4); + + void CheckMember(bool isFloat, string member, int offset) + { + var value = isFloat + ? $"PpcBitCastToDoubleInline(PpcConvertToDoubleBitsInline(resolved_pair.{member}))" + : $"resolved_pair.{member}"; + Assert.Contains($"= {value};", code, StringComparison.Ordinal); + var helper = isFloat ? "ReadResolvedFloat32" : "ReadResolved32"; + var fallback = code.IndexOf("} else {", code.IndexOf("if (resolved_pair.valid)", StringComparison.Ordinal), StringComparison.Ordinal); + Assert.True(fallback > 0, code); + Assert.Contains($"MemoryInline::{helper}(range, {offset}u,", code[fallback..], StringComparison.Ordinal); + } + } + + [Theory] + [InlineData(false, 2)] + [InlineData(true, 2)] + [InlineData(false, 4)] + [InlineData(true, 4)] + public void FusedIntegerLoadsKeepRawMembers(bool descending, int width) + { + var code = Emit(descending, false, false, width); + Assert.Contains($"MemoryInline::ReadResolvedPair{width * 8}(range, 0u)", code, StringComparison.Ordinal); + Assert.Contains("= resolved_pair.first;", code, StringComparison.Ordinal); + Assert.Contains("= resolved_pair.second;", code, StringComparison.Ordinal); + Assert.DoesNotContain("PpcConvertToDoubleBitsInline(resolved_pair.", code, StringComparison.Ordinal); + } + + private static string Emit(bool descending, bool firstFloat, bool secondFloat, int width) + { + var function = new IrFunction("fused_load", "entry", [new IrBasicBlock("entry", + [ + new IrResolveGuestMemoryRange("range", IrValue.Register("r3"), 8, width * 2, true, false), + new IrResolvedLoadPair(firstFloat ? "f1" : "r4", secondFloat ? "f2" : "r5", "range", + new IrAddress("r3", descending ? 8 + width : 8), + new IrAddress("r3", descending ? 8 : 8 + width), 0, width, descending), + new IrReturn(null) + ])]); + return new CxxLinearCodeGenerator().Emit(0x80001000, new SsaTransformer().Convert(function), + new FunctionAbiClassification(function.Name, ValueRepresentation.Void), new RepresentationEnvironment()); + } +} diff --git a/translator/tests/Translator.Tests/GqrLoopDataflowTests.cs b/translator/tests/Translator.Tests/GqrLoopDataflowTests.cs new file mode 100644 index 000000000..80dd0bee6 --- /dev/null +++ b/translator/tests/Translator.Tests/GqrLoopDataflowTests.cs @@ -0,0 +1,135 @@ +using Translator.Core.Ir; +using Translator.Core.Translation; + +namespace Translator.Tests; + +public sealed class GqrLoopDataflowTests +{ + private const uint Initial = 0x00070007; + private const uint Different = 0x00050005; + private const uint Preserved = 0x3D043D04; + private const uint Root = 0x80001000; + private const uint Loop = 0x80002000; + private const uint Sink = 0x80003000; + + public static IEnumerable LoopCases() + { + foreach (var shape in new[] { "self", "entry-self", "entry-backedge", "multiple-backedges" }) + foreach (var write in new[] { "same", "different", "unknown" }) + foreach (var reverse in new[] { false, true }) + yield return [shape, write, reverse]; + } + + [Theory] + [MemberData(nameof(LoopCases))] + public void OrdinaryFlowMergesEveryLoopEdge(string shape, string write, bool reverse) + { + var function = LoopFunction(shape, write, reverse); + var seed = new Dictionary { ["gqr5"] = Initial, ["gqr6"] = Preserved }; + var masks = new Dictionary { [Sink] = 0 }; + var flow = GqrConstantPropagation.Analyze(function, seed, masks); + AssertFacts(flow.In["loop"], write); + if (write != "unknown") + Assert.Equal(write == "same" ? Initial : Different, flow.In["exit"]["gqr5"]); + else + Assert.False(flow.In["exit"].ContainsKey("gqr5")); + + var specialized = GqrConstantPropagation.Specialize(function, seed, masks); + var header = specialized.Blocks.Single(block => block.Label == "loop"); + var load = Assert.Single(header.Instructions.OfType()); + var store = Assert.Single(header.Instructions.OfType()); + Assert.Equal(write == "same" ? (uint?)Initial : null, load.KnownGqr); + Assert.Equal(Preserved, store.KnownGqr); + } + + [Theory] + [MemberData(nameof(LoopCases))] + public void CompactFlowMergesEveryLoopEdgeAtTheCallSite(string shape, string write, bool reverse) + { + var functions = new Dictionary + { + [Root] = new("root", "entry", [new IrBasicBlock("entry", + [ + new IrAssign("gqr5", IrValue.Imm(Initial)), + new IrAssign("gqr6", IrValue.Imm(Preserved)), + new IrCall(string.Empty, $"0x{Loop:X8}", []), + new IrReturn(null) + ])]), + [Loop] = LoopFunction(shape, write, reverse), + [Sink] = new("sink", "entry", [new IrBasicBlock("entry", [new IrReturn(null)])]) + }; + var summaries = functions.ToDictionary(pair => pair.Key, pair => GqrFunctionSummary.Create(pair.Value)); + var result = GqrInterproceduralAnalysis.Analyze(summaries, [Root]); + Assert.Equal(Initial, result.EntryConstants[Loop]["gqr5"]); + AssertFacts(result.EntryConstants[Sink], write); + Assert.Contains(Sink, result.ReachableFunctions); + } + + [Fact] + public void EntryBackedgeCannotInventAConstantMissingFromTheEntrySeed() + { + var function = LoopFunction("entry-backedge", "same", false); + var flow = GqrConstantPropagation.Analyze(function, calleeWriteMasks: new Dictionary { [Sink] = 0 }); + Assert.Empty(flow.In["loop"]); + } + + [Fact] + public void CompactEntryBackedgeCannotInventAConstantMissingFromTheEntrySeed() + { + var functions = new Dictionary + { + [Loop] = LoopFunction("entry-backedge", "same", false), + [Sink] = new("sink", "entry", [new IrBasicBlock("entry", [new IrReturn(null)])]) + }; + var result = GqrInterproceduralAnalysis.Analyze( + functions.ToDictionary(pair => pair.Key, pair => GqrFunctionSummary.Create(pair.Value)), [Loop]); + Assert.Empty(result.EntryConstants[Sink]); + } + + private static void AssertFacts(IReadOnlyDictionary facts, string write) + { + if (write == "same") Assert.Equal(Initial, facts["gqr5"]); + else Assert.False(facts.ContainsKey("gqr5")); + Assert.Equal(Preserved, facts["gqr6"]); + } + + private static IrFunction LoopFunction(string shape, string write, bool reverse) + { + var blocks = new List(); + var header = new List + { + new IrResolvedPsqLoad("f1", "range", IrValue.Register("r4"), 0, 0, 5, null), + new IrResolvedPsqStore("range", IrValue.Register("r4"), 0, IrValue.Register("f1"), 1, 6, null), + new IrCall(string.Empty, $"0x{Sink:X8}", []) + }; + var rewritten = write switch + { + "same" => IrValue.Imm(Initial), + "different" => IrValue.Imm(Different), + _ => IrValue.Register("r3") + }; + if (shape is "self" or "entry-self") + { + header.Add(new IrAssign("gqr5", rewritten)); + header.Add(new IrBranch("beq", "loop", "exit")); + } + else if (shape == "entry-backedge") + { + header.Add(new IrJump("back")); + blocks.Add(new IrBasicBlock("back", + [new IrAssign("gqr5", rewritten), new IrBranch("beq", "loop", "exit")])); + } + else + { + header.Add(new IrBranch("beq", "left", "right")); + blocks.Add(new IrBasicBlock("left", [new IrAssign("gqr5", IrValue.Imm(Initial)), new IrJump("loop")])); + blocks.Add(new IrBasicBlock("right", [new IrAssign("gqr5", rewritten), new IrBranch("beq", "loop", "exit")])); + } + blocks.Add(new IrBasicBlock("loop", header)); + blocks.Add(new IrBasicBlock("exit", [new IrReturn(null)])); + var entry = shape == "self" ? "entry" : "loop"; + if (entry == "entry") blocks.Insert(0, new IrBasicBlock("entry", [new IrJump("loop")])); + if (reverse) blocks.Reverse(); + return new IrFunction("loop", entry, blocks); + } +} diff --git a/translator/tests/Translator.Tests/PpcDecoderPrimary31Tests.cs b/translator/tests/Translator.Tests/PpcDecoderPrimary31Tests.cs index a80049405..63d63be84 100644 --- a/translator/tests/Translator.Tests/PpcDecoderPrimary31Tests.cs +++ b/translator/tests/Translator.Tests/PpcDecoderPrimary31Tests.cs @@ -187,7 +187,7 @@ public void DecodesIndexedMemoryAndByteReverseForms() Assert.Equal("sthx", Decode(EncodeXForm(27, 28, 29, 407)).Mnemonic); Assert.Equal("sthux", Decode(EncodeXForm(27, 28, 29, 439)).Mnemonic); - var stwcx = Decode(EncodeXoForm(3, 4, 5, 498)); + var stwcx = Decode(EncodeXForm(3, 4, 5, 150, rc: true)); Assert.Equal("stwcx.", stwcx.Mnemonic); Assert.True(stwcx.IsConditionalBranch); diff --git a/translator/tests/Translator.Tests/PpcLogicalCorrectnessTests.cs b/translator/tests/Translator.Tests/PpcLogicalCorrectnessTests.cs new file mode 100644 index 000000000..58b464164 --- /dev/null +++ b/translator/tests/Translator.Tests/PpcLogicalCorrectnessTests.cs @@ -0,0 +1,95 @@ +using Translator.Core.Analysis.Representation; +using Translator.Core.Analysis.Ssa; +using Translator.Core.CodeGen; +using Translator.Core.Disassembly; +using Translator.Core.Ir; +using Translator.Core.Lifting; +using Translator.Core.Representation; + +namespace Translator.Tests; + +public sealed class PpcLogicalCorrectnessTests +{ + [Fact] + public void OnlyTheFullStoreConditionalOpcodeWithRecordBitIsAccepted() + { + for (uint xo = 0; xo < 1024; ++xo) + foreach (var rc in new[] { false, true }) + { + var word = (31u << 26) | (3u << 21) | (4u << 16) | (5u << 11) | + (xo << 1) | (rc ? 1u : 0u); + var decoded = PpcDecoder.Decode(0x80001000, word); + Assert.Equal(xo == 150 && rc, decoded.Mnemonic == "stwcx."); + } + + var instruction = PpcDecoder.Decode(0x80001000, 0x7C64292Du); + Assert.Equal("stwcx.", instruction.Mnemonic); + Assert.Collection(instruction.Operands, + op => Assert.Equal("r3", Assert.IsType(op).Name), + op => Assert.Equal("r4", Assert.IsType(op).Name), + op => Assert.Equal("r5", Assert.IsType(op).Name)); + var lifted = Assert.Single(new PpcLifter().Lift([instruction])).Ir; + var store = Assert.Single(lifted.OfType()); + Assert.Equal("PPC_Stwcx", store.Target); + Assert.Equal(IrValue.Register("r3"), store.Arguments[1]); + } + + [Theory] + [InlineData(false, 3u)] + [InlineData(false, 4u)] + [InlineData(false, 5u)] + [InlineData(true, 3u)] + [InlineData(true, 4u)] + [InlineData(true, 5u)] + public void NorRecordsItsResultAsASignedCr0ComparisonOnlyForRc(bool rc, uint destination) + { + var word = (31u << 26) | (3u << 21) | (destination << 16) | (5u << 11) | + (124u << 1) | (rc ? 1u : 0u); + var instruction = PpcDecoder.Decode(0x80001000, word); + Assert.Equal(rc ? "nor." : "nor", instruction.Mnemonic); + var lifted = Assert.Single(new PpcLifter().Lift([instruction])).Ir; + var register = $"r{destination}"; + Assert.Equal(new IrBinary(register, IrValue.Register("r3"), IrValue.Register("r5"), "nor"), lifted[0]); + if (rc) + Assert.Equal(new IrSetCrField(0, IrValue.Register(register), IrValue.Imm(0), false), lifted[1]); + else + Assert.Single(lifted); + + var function = new IrFunction("nor_record", "entry", + [ + new IrBasicBlock("entry", lifted.Append(new IrBranch("bso", "taken", "exit")).ToArray()), + new IrBasicBlock("taken", [new IrReturn(null)]), + new IrBasicBlock("exit", [new IrReturn(null)]) + ]); + var code = Emit(function); + Assert.Contains("~(r3 | r5)", code, StringComparison.Ordinal); + if (rc) + Assert.Contains($"SetCRResident(cr, xer, 0, static_cast({register}), static_cast(0))", code, StringComparison.Ordinal); + else + Assert.DoesNotContain("SetCRResident(", code, StringComparison.Ordinal); + } + + [Fact] + public void CrorcPreservesOperandOrderThroughLiftingAndBranchEmission() + { + var instruction = PpcDecoder.Decode(0x80001000, + (19u << 26) | (2u << 21) | (3u << 16) | (4u << 11) | (417u << 1)); + Assert.Equal("crorc", instruction.Mnemonic); + var call = Assert.IsType(Assert.Single(Assert.Single(new PpcLifter().Lift([instruction])).Ir)); + Assert.Equal(new[] { IrValue.Imm(6), IrValue.Imm(2), IrValue.Imm(3), IrValue.Imm(4) }, call.Arguments); + var function = new IrFunction("crorc_branch", "entry", + [ + new IrBasicBlock("entry", [call, new IrBranch("beq", "taken", "exit")]), + new IrBasicBlock("taken", [new IrReturn(null)]), + new IrBasicBlock("exit", [new IrReturn(null)]) + ]); + var code = Emit(function); + Assert.Contains("PpcCrLogicalResident(cr, static_cast(6), static_cast(2), static_cast(3), static_cast(4))", code, StringComparison.Ordinal); + Assert.Contains("& 0x2", code, StringComparison.Ordinal); + } + + private static string Emit(IrFunction function) => new CxxLinearCodeGenerator().Emit( + 0x80001000, new SsaTransformer().Convert(function), + new FunctionAbiClassification(function.Name, ValueRepresentation.Void), + new RepresentationEnvironment()); +} diff --git a/translator/tests/Translator.Tests/StateFreePsqGqrCodeGenTests.cs b/translator/tests/Translator.Tests/StateFreePsqGqrCodeGenTests.cs new file mode 100644 index 000000000..ef1686208 --- /dev/null +++ b/translator/tests/Translator.Tests/StateFreePsqGqrCodeGenTests.cs @@ -0,0 +1,80 @@ +using Translator.Core.Analysis.Representation; +using Translator.Core.Analysis.Ssa; +using Translator.Core.CodeGen; +using Translator.Core.Ir; +using Translator.Core.Representation; + +namespace Translator.Tests; + +public sealed class StateFreePsqGqrCodeGenTests +{ + public static IEnumerable AccessCases() + { + for (uint w = 0; w < 2; ++w) + for (uint i = 0; i < 8; ++i) + foreach (var route in new[] { "generic", "stack", "resolved" }) + foreach (var update in new[] { false, true }) + yield return [w, i, route, update]; + } + + [Theory] + [MemberData(nameof(AccessCases))] + public void StateFreePsqLoadsAndStoresKeepTheirGqrAndTemplateArguments( + uint w, uint i, string route, bool update) + { + var instructions = new List(); + if (route == "stack") + instructions.Add(new IrBinary("r4", IrValue.Register("r1"), IrValue.Imm(32), "add")); + if (route == "resolved") + { + instructions.Add(new IrResolveGuestMemoryRange("range", IrValue.Register("r4"), 0, 16, true, true)); + instructions.Add(new IrResolvedPsqLoad("f1", "range", IrValue.Register("r4"), 0, w, i, null)); + } + else + instructions.Add(new IrCall("f1", "PPC_PsqL", [IrValue.Register("r4"), IrValue.Imm(w), IrValue.Imm(i)])); + if (update) + instructions.Add(new IrBinary("r4", IrValue.Register("r4"), IrValue.Imm(8), "add")); + if (route == "resolved") + instructions.Add(new IrResolvedPsqStore("range", IrValue.Register("r4"), update ? 8 : 0, + IrValue.Register("f1"), w, i, null)); + else + instructions.Add(new IrCall(string.Empty, "PPC_PsqSt", + [IrValue.Register("r4"), IrValue.Register("f1"), IrValue.Imm(w), IrValue.Imm(i)])); + instructions.Add(new IrReturn(null)); + var function = new IrFunction("psq_gqr", "entry", [new IrBasicBlock("entry", instructions)]); + + var code = new CxxLinearCodeGenerator().Emit(0x80001000, + new SsaTransformer().Convert(function), + new FunctionAbiClassification(function.Name, ValueRepresentation.Void), + new RepresentationEnvironment(), + emitStateFreeLeafVariant: true, + stateFreeCallSymbols: new Dictionary { [0x80001000] = "psq_gqr_native" }); + + var start = code.IndexOf("psq_gqr_native(", StringComparison.Ordinal); + Assert.True(start >= 0, code); + var end = code.IndexOf("RECOMP_STATE_FREE_ABI", start, StringComparison.Ordinal); + Assert.True(end > start, code); + var variant = code[start..end]; + Assert.DoesNotContain("ctx", variant, StringComparison.Ordinal); + Assert.Contains($"native_gqr{i}", variant, StringComparison.Ordinal); + if (route == "generic") + { + Assert.Contains($"PPC_PsqLStateInline<{w}u, {i}u, false>(native_gqr{i},", variant, StringComparison.Ordinal); + Assert.Contains($"PPC_PsqStStateInline<{w}u, {i}u, false>(native_gqr{i},", variant, StringComparison.Ordinal); + Assert.DoesNotContain("GqrInline<", variant, StringComparison.Ordinal); + Assert.DoesNotContain("mkw_gqr", code, StringComparison.Ordinal); + Assert.Contains($"PPC_PsqLInline<{w}u, {i}u>(ctx,", code, StringComparison.Ordinal); + } + else if (route == "stack") + { + Assert.Contains($"PPC_PsqLStateInline<{w}u, {i}u, true>(native_gqr{i},", variant, StringComparison.Ordinal); + Assert.Contains($"PPC_PsqStStateInline<{w}u, {i}u, true>(native_gqr{i},", variant, StringComparison.Ordinal); + } + else + { + Assert.Contains($"PPC_PsqLResolvedStateInline<{w}u, {i}u>(native_gqr{i},", variant, StringComparison.Ordinal); + Assert.Contains($"PPC_PsqStResolvedStateInline<{w}u, {i}u>(native_gqr{i},", variant, StringComparison.Ordinal); + } + if (update) Assert.Contains("+ 8", variant, StringComparison.Ordinal); + } +} From f3c8feaf041bfb25061b6aee970920fa31dd1f48 Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:26:51 +0100 Subject: [PATCH 02/17] limit memory fault test to supported hosts --- runtime/tests/memory_access_tests.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtime/tests/memory_access_tests.cpp b/runtime/tests/memory_access_tests.cpp index 3d7735ad2..62b3b4ec6 100644 --- a/runtime/tests/memory_access_tests.cpp +++ b/runtime/tests/memory_access_tests.cpp @@ -214,6 +214,8 @@ void TestDeferredRanges() { } void TestDeferredEndpoint() { +#if !defined(__APPLE__) + // The macOS mapping backend has no deferred-read fault handler. if (GuestFlat::RequiresCheckedAccess()) return; unsigned calls = 0; Require(Memory::RegisterDeferredRead(UINT32_MAX, 1, CountRead, &calls) != 0, @@ -224,6 +226,7 @@ void TestDeferredEndpoint() { Require(MemoryInline::ResolveDeferredReads(UINT32_MAX, 1) && calls == 1, "last-byte fault left a pending callback"); Memory::ClearDeferredReads(); +#endif } struct SingleCase { uint32_t single; uint64_t widened; }; From 2f96f91b9e2acfa4c8462af2d8d84e7246391045 Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:29:20 +0100 Subject: [PATCH 03/17] build all registered macos tests --- .github/workflows/build.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 69d16423c..97312aab6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -96,10 +96,19 @@ jobs: shell: bash run: | cmake --build build-macos --target \ + mkw_ppc_cr_logical_tests \ mkw_platform_paths_tests \ mkw_runtime_config_tests \ + mkw_memory_access_tests \ mkw_nand_save_tests \ mkw_nand_guest_path_tests \ + mkw_host_file_tests \ + mkw_u8_archive_tests \ + mkw_runtime_config_io_tests \ + mkw_pad_trigger_tests \ + mkw_fiber_manager_tests \ + mkw_audio_backend_ownership_tests \ + mkw_network_receive_tests \ mkw_nand_settings_tests \ mkw_sc_serial_tests \ mkw_input_expr_tests \ From 73bada98a04e0c9563363cd4e042e265d98d6ed5 Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:04:16 +0100 Subject: [PATCH 04/17] fix config recovery and constructor exceptions --- .github/workflows/build.yml | 1 + runtime/cmake/PlatformTests.cmake | 18 +++ runtime/include/host_file.h | 23 ++- runtime/src/system_bridge.cpp | 18 ++- runtime/tests/host_file_recovery_tests.cpp | 141 ++++++++++++++++++ runtime/tests/host_file_tests.cpp | 72 ++++++++- .../tests/system_bridge_support/abi_bridge.h | 18 +++ runtime/tests/system_bridge_tests.cpp | 129 ++++++++++++++++ 8 files changed, 412 insertions(+), 8 deletions(-) create mode 100644 runtime/tests/host_file_recovery_tests.cpp create mode 100644 runtime/tests/system_bridge_support/abi_bridge.h create mode 100644 runtime/tests/system_bridge_tests.cpp diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 97312aab6..d1ae32d3d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -103,6 +103,7 @@ jobs: mkw_nand_save_tests \ mkw_nand_guest_path_tests \ mkw_host_file_tests \ + mkw_system_bridge_tests \ mkw_u8_archive_tests \ mkw_runtime_config_io_tests \ mkw_pad_trigger_tests \ diff --git a/runtime/cmake/PlatformTests.cmake b/runtime/cmake/PlatformTests.cmake index 53349d8b1..6f0dc65eb 100644 --- a/runtime/cmake/PlatformTests.cmake +++ b/runtime/cmake/PlatformTests.cmake @@ -105,6 +105,24 @@ target_compile_features(mkw_host_file_tests PRIVATE cxx_std_17) target_link_libraries(mkw_host_file_tests PRIVATE Threads::Threads) add_test(NAME mkw_host_file_tests COMMAND mkw_host_file_tests) +add_executable(mkw_system_bridge_tests + "${MKW_TEST_RUNTIME_DIR}/tests/system_bridge_tests.cpp" + "${MKW_TEST_RUNTIME_DIR}/src/system_bridge.cpp") +target_include_directories(mkw_system_bridge_tests BEFORE PRIVATE + "${MKW_TEST_RUNTIME_DIR}/tests/system_bridge_support" + "${MKW_TEST_RUNTIME_DIR}/include" + "${MKW_TEST_RUNTIME_DIR}/third_party/toml11") +target_compile_features(mkw_system_bridge_tests PRIVATE cxx_std_20) +set_target_properties(mkw_system_bridge_tests PROPERTIES UNITY_BUILD OFF) +add_test(NAME mkw_system_bridge_tests COMMAND mkw_system_bridge_tests) +if(MKW_PLATFORM_WINDOWS) + add_executable(mkw_host_file_recovery_tests "${MKW_TEST_RUNTIME_DIR}/tests/host_file_recovery_tests.cpp") + target_include_directories(mkw_host_file_recovery_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/include") + target_compile_features(mkw_host_file_recovery_tests PRIVATE cxx_std_17) + target_link_libraries(mkw_host_file_recovery_tests PRIVATE advapi32) + add_test(NAME mkw_host_file_recovery_tests COMMAND mkw_host_file_recovery_tests) +endif() + add_executable(mkw_u8_archive_tests "${MKW_TEST_RUNTIME_DIR}/tests/u8_archive_tests.cpp") target_include_directories(mkw_u8_archive_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/include") target_compile_features(mkw_u8_archive_tests PRIVATE cxx_std_17) diff --git a/runtime/include/host_file.h b/runtime/include/host_file.h index 924b23c54..4bb11c15a 100644 --- a/runtime/include/host_file.h +++ b/runtime/include/host_file.h @@ -48,19 +48,36 @@ struct FileOperations { #ifdef _WIN32 return _commit(_fileno(file)) == 0; #else +#ifdef __APPLE__ + if (::fcntl(fileno(file), F_FULLFSYNC) == 0) return true; + if (errno != ENOTSUP && errno != EINVAL && errno != ENOTTY) return false; +#endif return ::fsync(fileno(file)) == 0; #endif } static bool Close(FILE* file) { return std::fclose(file) == 0; } - static bool Publish(const std::filesystem::path& temporary, + static bool Publish(std::filesystem::path& temporary, const std::filesystem::path& target, bool replace) { #ifdef _WIN32 if (replace) { + auto backup = temporary; + backup += ".bak"; // ReplaceFile preserves the destination's ACL and other metadata. - if (ReplaceFileW(target.c_str(), temporary.c_str(), nullptr, 0, nullptr, nullptr)) return true; - if (GetLastError() != ERROR_FILE_NOT_FOUND) return false; + if (ReplaceFileW(target.c_str(), temporary.c_str(), backup.c_str(), 0, nullptr, nullptr)) { + return DeleteFileW(backup.c_str()) != 0; + } + const auto error = GetLastError(); + if (error == ERROR_UNABLE_TO_MOVE_REPLACEMENT || error == ERROR_UNABLE_TO_MOVE_REPLACEMENT_2) { + if (error == ERROR_UNABLE_TO_MOVE_REPLACEMENT_2) { + MoveFileExW(backup.c_str(), target.c_str(), MOVEFILE_WRITE_THROUGH); + } + // Keep recovery contents even if restoring the original name fails. + temporary.clear(); + return false; + } + if (error != ERROR_FILE_NOT_FOUND) return false; } return MoveFileExW(temporary.c_str(), target.c_str(), MOVEFILE_WRITE_THROUGH) != 0; #else diff --git a/runtime/src/system_bridge.cpp b/runtime/src/system_bridge.cpp index 99f2abd93..b415c46b8 100644 --- a/runtime/src/system_bridge.cpp +++ b/runtime/src/system_bridge.cpp @@ -353,8 +353,13 @@ void SystemBridge::Initialize() { g_sehJumpTarget = &jumpBuf; if (MKW_SETJMP(jumpBuf) == 0) { cpu.gpr[1] = 0x81700000u; - InvokeIndirectCpu(funcAddr, &cpu); - dolCount++; + try { + InvokeIndirectCpu(funcAddr, &cpu); + dolCount++; + } catch (...) { + RT_LOG(RT_TAG_RUNTIME) << "C++ exception invoking DOL ctor 0x" << std::hex << funcAddr + << std::dec << " - skipping." << std::endl; + } } else { RT_LOG(RT_TAG_RUNTIME) << "SEH Exception caught invoking DOL ctor 0x" << std::hex << funcAddr << " code=0x" << g_sehLastExceptionCode @@ -393,8 +398,13 @@ void SystemBridge::Initialize() { g_sehJumpTarget = &jumpBuf; if (MKW_SETJMP(jumpBuf) == 0) { cpu.gpr[1] = 0x81700000u; - InvokeIndirectCpu(funcAddr, &cpu); - count++; + try { + InvokeIndirectCpu(funcAddr, &cpu); + count++; + } catch (...) { + RT_LOG(RT_TAG_RUNTIME) << "C++ exception invoking ctor 0x" << std::hex << funcAddr + << std::dec << " - skipping." << std::endl; + } } else { RT_LOG(RT_TAG_RUNTIME) << "SEH Exception caught invoking ctor 0x" << std::hex << funcAddr << " code=0x" << g_sehLastExceptionCode diff --git a/runtime/tests/host_file_recovery_tests.cpp b/runtime/tests/host_file_recovery_tests.cpp new file mode 100644 index 000000000..e846c0421 --- /dev/null +++ b/runtime/tests/host_file_recovery_tests.cpp @@ -0,0 +1,141 @@ +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include + +#include +#include +#include +#include +#include + +namespace { +DWORD g_failure = 0; +bool g_failRestore = false; +bool g_restoreCollision = false; +bool g_failBackupCleanup = false; +std::filesystem::path g_replacement; +std::filesystem::path g_backup; + +BOOL TestReplaceFile(LPCWSTR target, LPCWSTR replacement, LPCWSTR backup, + DWORD flags, LPVOID exclude, LPVOID reserved) { + g_replacement = replacement; + g_backup = backup ? backup : L""; + if (g_failure == 0) return ReplaceFileW(target, replacement, backup, flags, exclude, reserved); + if (g_failure == ERROR_UNABLE_TO_MOVE_REPLACEMENT && !backup) { + if (!DeleteFileW(target)) throw std::runtime_error("cannot simulate missing destination"); + } else if (g_failure == ERROR_UNABLE_TO_MOVE_REPLACEMENT_2) { + if (!backup || !MoveFileExW(target, backup, MOVEFILE_WRITE_THROUGH)) { + throw std::runtime_error("cannot simulate moved original"); + } + } + SetLastError(g_failure); + return FALSE; +} + +BOOL TestMoveFile(LPCWSTR source, LPCWSTR target, DWORD flags) { + if (g_restoreCollision && std::filesystem::path(source) == g_backup) { + std::ofstream file{std::filesystem::path(target)}; + file << "concurrent writer"; + } + if (g_failRestore && std::filesystem::path(source) == g_backup) { + SetLastError(ERROR_ACCESS_DENIED); + return FALSE; + } + return MoveFileExW(source, target, flags); +} +BOOL TestDeleteFile(LPCWSTR path) { + if (g_failBackupCleanup && std::filesystem::path(path) == g_backup) { + SetLastError(ERROR_ACCESS_DENIED); + return FALSE; + } + return DeleteFileW(path); +} +} + +#define ReplaceFileW TestReplaceFile +#define MoveFileExW TestMoveFile +#define DeleteFileW TestDeleteFile +#include "host_file.h" +#undef DeleteFileW +#undef MoveFileExW +#undef ReplaceFileW + +namespace { +void Require(bool value, const char* message) { + if (!value) throw std::runtime_error(message); +} +std::string Read(const std::filesystem::path& path) { + std::ifstream file(path, std::ios::binary); + return {std::istreambuf_iterator(file), std::istreambuf_iterator()}; +} + +void CheckPartialFailure(const std::filesystem::path& directory, DWORD error, bool failRestore, + bool restoreCollision = false) { + std::filesystem::create_directory(directory); + const auto target = directory / "Config.toml"; + { std::ofstream file(target); file << "original contents"; } + g_failure = error; + g_failRestore = failRestore; + g_restoreCollision = restoreCollision; + g_replacement.clear(); + g_backup.clear(); + Require(!RuntimeHostFile::WriteAtomically(target, "replacement contents"), "partial failure reported success"); + Require(Read(g_replacement) == "replacement contents", "partial failure discarded replacement contents"); + Require(!g_backup.empty(), "replacement did not provide a backup path"); + if (failRestore || restoreCollision) { + if (restoreCollision) Require(Read(target) == "concurrent writer", "restoration overwrote another writer"); + else Require(!std::filesystem::exists(target), "failed restoration unexpectedly published a file"); + Require(Read(g_backup) == "original contents", "failed restoration discarded original contents"); + } else { + Require(Read(target) == "original contents", "original contents were not retained or restored"); + Require(!std::filesystem::exists(g_backup), "restored file left a duplicate backup"); + } + std::filesystem::remove_all(directory); +} +} + +int main() { + const auto root = std::filesystem::temp_directory_path() / ("wiicomp-recovery-" + + std::to_string(GetCurrentProcessId()) + "-" + std::to_string(GetTickCount64())); + bool claimed = false; + try { + claimed = std::filesystem::create_directory(root); + Require(claimed, "cannot create test directory"); + CheckPartialFailure(root / "before-move", ERROR_UNABLE_TO_MOVE_REPLACEMENT, false); + CheckPartialFailure(root / "after-move", ERROR_UNABLE_TO_MOVE_REPLACEMENT_2, false); + CheckPartialFailure(root / "restore-denied", ERROR_UNABLE_TO_MOVE_REPLACEMENT_2, true); + CheckPartialFailure(root / "restore-collision", ERROR_UNABLE_TO_MOVE_REPLACEMENT_2, false, true); + g_restoreCollision = false; + const auto target = root / "Config.toml"; + { std::ofstream file(target); file << "original contents"; } + for (DWORD error : {DWORD{ERROR_UNABLE_TO_REMOVE_REPLACED}, DWORD{ERROR_ACCESS_DENIED}}) { + g_failure = error; + g_failRestore = false; + Require(!RuntimeHostFile::WriteAtomically(target, "replacement contents"), "ordinary failure reported success"); + Require(Read(target) == "original contents", "ordinary failure changed original contents"); + Require(!std::filesystem::exists(g_replacement) && !std::filesystem::exists(g_backup), + "ordinary failure retained unnecessary recovery files"); + } + g_failure = 0; + Require(RuntimeHostFile::WriteAtomically(target, "published contents"), "normal replacement failed"); + Require(Read(target) == "published contents", "normal replacement did not publish"); + for (const auto& entry : std::filesystem::directory_iterator(root)) { + Require(entry.path() == target, "successful replacement left recovery files"); + } + g_failBackupCleanup = true; + Require(!RuntimeHostFile::WriteAtomically(target, "new contents"), "backup cleanup failure reported success"); + Require(Read(target) == "new contents" && Read(g_backup) == "published contents", + "backup cleanup failure lost published or original contents"); + g_failBackupCleanup = false; + std::filesystem::remove_all(root); + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + std::error_code ignored; + if (claimed) std::filesystem::remove_all(root, ignored); + return 1; + } +} diff --git a/runtime/tests/host_file_tests.cpp b/runtime/tests/host_file_tests.cpp index 47b5209ab..18abe6938 100644 --- a/runtime/tests/host_file_tests.cpp +++ b/runtime/tests/host_file_tests.cpp @@ -1,5 +1,49 @@ +#ifdef __APPLE__ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { +bool g_injectSync = false; +int g_fullSyncError = 0; +int g_syncError = 0; +unsigned g_fullSyncCalls = 0; +unsigned g_syncCalls = 0; + +int TestFcntl(int descriptor, int command) { + if (!g_injectSync) return ::fcntl(descriptor, command); + if (command != F_FULLFSYNC) { errno = EINVAL; return -1; } + ++g_fullSyncCalls; + errno = g_fullSyncError; + return g_fullSyncError ? -1 : 0; +} +int TestFsync(int descriptor) { + if (!g_injectSync) return ::fsync(descriptor); + ++g_syncCalls; + errno = g_syncError; + return g_syncError ? -1 : 0; +} +} +#define fcntl TestFcntl +#define fsync TestFsync +#endif + #include "host_file.h" +#ifdef __APPLE__ +#undef fsync +#undef fcntl +#endif + #include #include #include @@ -19,6 +63,29 @@ std::string Read(const std::filesystem::path& path) { std::ifstream input(path, std::ios::binary); return {std::istreambuf_iterator(input), std::istreambuf_iterator()}; } +#ifdef __APPLE__ +void CheckFullSync(const std::filesystem::path& path) { + FILE* file = RuntimeHostFile::Open(path, "wb"); + Require(file != nullptr, "cannot create flush fixture"); + struct Case { int fullError; int fallbackError; bool succeeds; unsigned fallbackCalls; }; + bool passed = true; + g_injectSync = true; + for (const auto& test : {Case{0, EIO, true, 0}, Case{ENOTSUP, 0, true, 1}, + Case{EINVAL, 0, true, 1}, Case{ENOTSUP, EIO, false, 1}, + Case{ENOTTY, 0, true, 1}, Case{ENOTTY, EIO, false, 1}, + Case{EIO, 0, false, 0}, Case{EINTR, 0, false, 0}}) { + g_fullSyncError = test.fullError; + g_syncError = test.fallbackError; + g_fullSyncCalls = g_syncCalls = 0; + const bool result = RuntimeHostFile::FileOperations::Flush(file); + passed &= result == test.succeeds && g_fullSyncCalls == 1 && g_syncCalls == test.fallbackCalls; + } + g_injectSync = false; + std::fclose(file); + std::filesystem::remove(path); + Require(passed, "full sync did not preserve success, unsupported, or I/O failure semantics"); +} +#endif struct ShortWrites : RuntimeHostFile::FileOperations { static size_t Write(FILE* file, const char* data, size_t size) { return FileOperations::Write(file, data, std::min(size, size_t{3})); @@ -40,7 +107,7 @@ struct FailedPublish : RuntimeHostFile::FileOperations { static bool Publish(const std::filesystem::path&, const std::filesystem::path&, bool) { return false; } }; struct PublishedFailure : RuntimeHostFile::FileOperations { - static bool Publish(const std::filesystem::path& temporary, const std::filesystem::path& target, bool replace) { + static bool Publish(std::filesystem::path& temporary, const std::filesystem::path& target, bool replace) { FileOperations::Publish(temporary, target, replace); return false; } @@ -108,6 +175,9 @@ int main() { try { claimed = std::filesystem::create_directory(root); Require(claimed, "cannot claim test directory"); +#ifdef __APPLE__ + CheckFullSync(root / "flush-fixture"); +#endif const auto path = root / "Config.toml"; Require(RuntimeHostFile::WriteAtomically(path, "original contents"), "initial publication failed"); CheckFailure(path); diff --git a/runtime/tests/system_bridge_support/abi_bridge.h b/runtime/tests/system_bridge_support/abi_bridge.h new file mode 100644 index 000000000..19221843c --- /dev/null +++ b/runtime/tests/system_bridge_support/abi_bridge.h @@ -0,0 +1,18 @@ +#pragma once + +#include "ppc_runtime.h" +#include "guest_abi/guest_region.h" +#include + +struct TranslatedFunctionInfo { + uint32_t address; + const char* name; +}; +class TranslatedFunctionRegistry { +public: + static const TranslatedFunctionInfo* FindByAddressPtr(uint32_t address); +}; +std::optional FindNearestTranslatedFunction(uint32_t address); +void InitializePersistentCpuContext(); +CpuContext& GetPersistentCpuContext(); +void InvokeIndirectCpu(uint32_t address, CpuContext* cpu); diff --git a/runtime/tests/system_bridge_tests.cpp b/runtime/tests/system_bridge_tests.cpp new file mode 100644 index 000000000..a98e0c2af --- /dev/null +++ b/runtime/tests/system_bridge_tests.cpp @@ -0,0 +1,129 @@ +#include "abi_bridge.h" +#include "system_bridge.h" +#include "memory_slow_test_support.h" +#include "recomp_mod_loader.h" +#include "runtime_product.h" + +#include +#include +#include +#include +#include + +namespace { +CpuContext g_cpu{}; +std::unordered_map g_words; +std::vector g_calls; +unsigned g_unwound = 0; +unsigned g_caught = 0; +bool g_invalidContext = false; +constexpr uint32_t kCtorBase = 0x80100000; + +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} +struct UnwindCounter { + ~UnwindCounter() { ++g_unwound; } +}; +} + +Memory::Config Memory::Config::WiiDefaults() { return {}; } +void Memory::Init(const Config&) { g_words.clear(); } +Memory::AccessViolation::AccessViolation(uint32_t address, size_t length, std::string_view reason) + : std::runtime_error(std::string(reason)), address_(address), length_(length), reason_(reason) {} +bool Memory::Contains(uint32_t, size_t) { return false; } +uint8_t* Memory::GetPointer(uint32_t address, size_t length) { + throw AccessViolation(address, length, "unmapped test memory"); +} +uint32_t MemoryInline::Read32Slow(uint32_t address) { + const auto found = g_words.find(address); + if (found == g_words.end()) throw Memory::AccessViolation(address, 4, "unmapped test word"); + return found->second; +} +void MemoryInline::Write32Slow(uint32_t address, uint32_t value) { g_words[address] = value; } + +namespace RecompMod { +const std::vector& MemoryReservations() { + static const std::vector empty; + return empty; +} +uint32_t CurrentTranslatedExecutionAddress() noexcept { return 0; } +} +namespace RuntimeProduct { +const Descriptor& Active() noexcept { + static const Descriptor descriptor{Kind::BaseGame, "test"}; + return descriptor; +} +} +extern "C" { +extern const uint8_t kMkwGuestRegion = 0; +extern const uint32_t kGuestMapSymbolCount = 0; +extern const uint32_t kGuestMapSymbolAddresses[] = {0}; +extern const char* const kGuestMapSymbolNames[] = {""}; +void InitializeDataSections() { + for (const uint32_t start : {MKW_GADDR(80244DE0), MKW_GADDR(8088f400)}) { + for (uint32_t i = 0; i < 6; ++i) g_words[start + 4 * i] = kCtorBase + 4 * i; + g_words[start + 24] = 0; + g_words[start + 28] = 0xffffffff; + g_words[start + 32] = 0xdeadbeef; + } +} +} +void InitializePersistentCpuContext() { g_cpu = {}; } +CpuContext& GetPersistentCpuContext() { return g_cpu; } +std::optional FindNearestTranslatedFunction(uint32_t) { return std::nullopt; } +const TranslatedFunctionInfo* TranslatedFunctionRegistry::FindByAddressPtr(uint32_t address) { + static const TranslatedFunctionInfo info{kCtorBase, "test ctor"}; + return address >= kCtorBase && address < kCtorBase + 24 ? &info : nullptr; +} +void InvokeIndirectCpu(uint32_t address, CpuContext* cpu) { + g_invalidContext |= cpu != &g_cpu || g_currentCpuContext != cpu || + cpu->gpr[1] != 0x81700000 || !g_suppressSehReporting || g_sehJumpTarget == nullptr; + g_calls.push_back(address); + cpu->gpr[1] = 0; + const uint32_t index = (address - kCtorBase) / 4; + if (index == 4) { +#ifdef _WIN32 + longjmp(*g_sehJumpTarget, 1); +#else + siglongjmp(*g_sehJumpTarget, 1); +#endif + } + UnwindCounter counter; + switch (index) { + case 0: throw std::runtime_error("escaped constructor"); + case 1: throw Memory::AccessViolation(0xdeadbeef, 4, "escaped memory fault"); + case 2: throw 42; + case 3: + try { throw std::runtime_error("handled inside constructor"); } + catch (const std::runtime_error&) { ++g_caught; } + break; + default: break; + } +} + +int main() { + std::ostringstream log; + auto* saved = std::cerr.rdbuf(log.rdbuf()); + try { + CpuContext caller{}; + g_currentCpuContext = &caller; + SystemBridge::Initialize(); + Require(!g_invalidContext, "constructor context or recovery target was not prepared"); + Require(g_calls.size() == 12, "a constructor failure skipped later constructors"); + for (size_t i = 0; i < g_calls.size(); ++i) + Require(g_calls[i] == kCtorBase + 4 * (i % 6), "constructor ordering changed"); + Require(g_unwound == 10 && g_caught == 2, "C++ exceptions did not unwind or remain locally catchable"); + Require(!g_suppressSehReporting && g_sehJumpTarget == nullptr, "constructor recovery state leaked"); + Require(g_currentCpuContext == &caller, "constructor scope did not restore the caller context"); + Require(log.str().find("Executed 2 main DOL static constructors.") != std::string::npos && + log.str().find("Executed 2 static constructors.") != std::string::npos, + "failed constructors were counted as successful"); + g_currentCpuContext = nullptr; + } catch (const std::exception& error) { + std::cerr.rdbuf(saved); + std::cerr << error.what() << '\n' << log.str(); + return 1; + } + std::cerr.rdbuf(saved); +} From 2b9a6e6c7a8b90c79c9ce0469604b32926139d41 Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:19:53 +0100 Subject: [PATCH 05/17] draft performance improvements --- .github/workflows/build.yml | 24 +- aurora-main | 2 +- runtime/cmake/PlatformTests.cmake | 60 ++++ runtime/include/runtime_config.h | 14 + runtime/src/hle/audio/ax_mix_kernels.h | 130 ++++++- runtime/src/hle/gx/gx_copy.cpp | 14 +- runtime/src/hle/gx/gx_dl.cpp | 37 +- runtime/src/hle/gx/gx_dl_cache.h | 121 +++++++ runtime/src/hle/gx/gx_fifo.cpp | 107 +++++- runtime/src/hle/gx/gx_init.cpp | 1 + runtime/src/hle/gx/gx_internal.h | 10 + runtime/src/hle/gx/gx_objects.cpp | 34 ++ runtime/src/hle/gx/gx_stream_common.h | 3 + runtime/src/hle/gx/gx_texture.cpp | 59 ++- .../src/hle/gx/gx_texture_binding_contract.h | 22 ++ runtime/src/hle/gx/gx_vertex.cpp | 6 +- runtime/src/hle/gx/gx_worker.cpp | 40 ++- runtime/src/main.cpp | 2 + runtime/src/settings_overlay.cpp | 6 + runtime/tests/ax_mix_kernels_tests.cpp | 317 +++++++++++++++++ runtime/tests/gx_copy_tests.cpp | 245 +++++++++++++ runtime/tests/gx_direct_draw_tests.cpp | 280 +++++++++++++++ runtime/tests/gx_dl_cache_tests.cpp | 301 ++++++++++++++++ .../tests/gx_sync_support/copy_observation.h | 22 ++ .../runtime_seams.h | 100 ++++++ runtime/tests/gx_texture_binding_tests.cpp | 335 ++++++++++++++++++ runtime/tests/gx_worker_tests.cpp | 283 +++++++++++++++ runtime/tests/runtime_config_tests.cpp | 20 ++ translator/src/Translator.Cli/Program.cs | 17 +- .../GuestAbiInterproceduralAnalyzer.cs | 26 +- .../Translation/CanonicalIrDictionary.cs | 58 +++ .../CanonicalIrDictionaryTests.cs | 285 +++++++++++++++ .../TranslateRecursiveRuntimeConfigTests.cs | 85 +++++ 33 files changed, 2982 insertions(+), 84 deletions(-) create mode 100644 runtime/src/hle/gx/gx_dl_cache.h create mode 100644 runtime/tests/ax_mix_kernels_tests.cpp create mode 100644 runtime/tests/gx_copy_tests.cpp create mode 100644 runtime/tests/gx_direct_draw_tests.cpp create mode 100644 runtime/tests/gx_dl_cache_tests.cpp create mode 100644 runtime/tests/gx_sync_support/copy_observation.h create mode 100644 runtime/tests/gx_texture_binding_support/runtime_seams.h create mode 100644 runtime/tests/gx_texture_binding_tests.cpp create mode 100644 runtime/tests/gx_worker_tests.cpp create mode 100644 translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs create mode 100644 translator/tests/Translator.Tests/CanonicalIrDictionaryTests.cs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d1ae32d3d..e7c7ac7e8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -119,8 +119,30 @@ jobs: mkw_macos_context_abi_tests \ mkw_macos_host_context_tests \ mkw_macos_guest_flat_memory_tests \ - mkw_macos_external_audio_tests + mkw_macos_external_audio_tests \ + mkw_ax_mix_kernels_tests - name: Test execution substrate shell: bash run: ctest --test-dir build-macos --output-on-failure + + - name: Check ARM audio kernels with sanitizers + shell: bash + run: | + clang++ -std=c++17 -O2 -fno-fast-math \ + -fsanitize=address,undefined -fno-sanitize-recover=all \ + -DMKW_AX_MIX_REQUIRE_NEON=1 \ + -Iruntime/include -Iruntime/src/hle/audio \ + runtime/tests/ax_mix_kernels_tests.cpp -o build-macos/ax-mix-sanitized + build-macos/ax-mix-sanitized + build-macos/mkw_ax_mix_kernels_tests --benchmark 20000 + + - name: Build and test GX performance paths + shell: bash + run: | + cmake -S runtime -B build-macos -DMKW_BUILD_AURORA_TESTS=ON + cmake --build build-macos --parallel 3 --target \ + gx_fifo_tests shader_module_cache_tests gx_shader_layout_tests \ + mkw_gx_texture_binding_tests mkw_gx_sync_tests mkw_gx_dl_cache_tests + ctest --test-dir build-macos --output-on-failure \ + -R '^(GXFifoTest|RuntimeDirectDrawTest|ShaderModuleCacheTest|GxShader(Layout|Key|Arena|Mode|State)|mkw_gx_(texture_binding|sync|dl_cache)_tests)' diff --git a/aurora-main b/aurora-main index a222e54a4..cf312581c 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit a222e54a4e53d55415de1de1e4fe9dece2705288 +Subproject commit cf312581c4c7cdcdf03f7d6f5e24875d9898c082 diff --git a/runtime/cmake/PlatformTests.cmake b/runtime/cmake/PlatformTests.cmake index 6f0dc65eb..5e634d23f 100644 --- a/runtime/cmake/PlatformTests.cmake +++ b/runtime/cmake/PlatformTests.cmake @@ -155,6 +155,7 @@ endif() if(TARGET gx_fifo_tests) target_sources(gx_fifo_tests PRIVATE "${MKW_TEST_RUNTIME_DIR}/tests/gx_fifo_invalidation_tests.cpp" + "${MKW_TEST_RUNTIME_DIR}/tests/gx_direct_draw_tests.cpp" "${MKW_TEST_RUNTIME_DIR}/src/hle/gx/gx_fifo.cpp") target_include_directories(gx_fifo_tests PRIVATE "${MKW_AURORA_DIR}/tests" "${MKW_TEST_RUNTIME_DIR}/include" @@ -164,7 +165,66 @@ if(TARGET gx_fifo_tests) if(MKW_PLATFORM_WINDOWS) target_link_libraries(gx_fifo_tests PRIVATE advapi32) endif() + + get_target_property(_mkw_gx_sources gx_fifo_tests SOURCES) + get_target_property(_mkw_gx_source_dir gx_fifo_tests SOURCE_DIR) + get_target_property(_mkw_gx_includes gx_fifo_tests INCLUDE_DIRECTORIES) + get_target_property(_mkw_gx_links gx_fifo_tests LINK_LIBRARIES) + set(_mkw_gx_encoder_sources) + foreach(_mkw_gx_source IN LISTS _mkw_gx_sources) + if(_mkw_gx_source MATCHES "(^|/)lib/") + get_filename_component(_mkw_gx_absolute "${_mkw_gx_source}" ABSOLUTE + BASE_DIR "${_mkw_gx_source_dir}") + list(APPEND _mkw_gx_encoder_sources "${_mkw_gx_absolute}") + endif() + endforeach() + + add_executable(mkw_gx_texture_binding_tests + "${MKW_TEST_RUNTIME_DIR}/tests/gx_texture_binding_tests.cpp" + "${MKW_AURORA_DIR}/tests/gx_test_stubs.cpp" + ${_mkw_gx_encoder_sources}) + add_executable(mkw_gx_sync_tests + "${MKW_TEST_RUNTIME_DIR}/tests/gx_worker_tests.cpp" + "${MKW_TEST_RUNTIME_DIR}/tests/gx_copy_tests.cpp" + "${MKW_AURORA_DIR}/tests/gx_test_stubs.cpp" + ${_mkw_gx_encoder_sources}) + target_compile_definitions(mkw_gx_sync_tests PRIVATE MKW_GX_COPY_TESTING=1) + foreach(_mkw_gx_test IN ITEMS mkw_gx_texture_binding_tests mkw_gx_sync_tests) + target_include_directories(${_mkw_gx_test} PRIVATE + ${_mkw_gx_includes} "${MKW_TEST_RUNTIME_DIR}/tests") + target_compile_definitions(${_mkw_gx_test} PRIVATE AURORA TARGET_PC=1) + target_compile_features(${_mkw_gx_test} PRIVATE cxx_std_20) + target_link_libraries(${_mkw_gx_test} PRIVATE ${_mkw_gx_links} Threads::Threads) + set_target_properties(${_mkw_gx_test} PROPERTIES UNITY_BUILD OFF) + aurora_copy_runtime_dlls(${_mkw_gx_test}) + add_test(NAME ${_mkw_gx_test} COMMAND ${_mkw_gx_test}) + set_tests_properties(${_mkw_gx_test} PROPERTIES TIMEOUT 120) + endforeach() + + add_executable(mkw_gx_dl_cache_tests "${MKW_TEST_RUNTIME_DIR}/tests/gx_dl_cache_tests.cpp") + target_include_directories(mkw_gx_dl_cache_tests PRIVATE + "${MKW_TEST_RUNTIME_DIR}/include" + "${MKW_TEST_RUNTIME_DIR}/third_party/toml11" + "${MKW_AURORA_DIR}/include") + target_compile_features(mkw_gx_dl_cache_tests PRIVATE cxx_std_20) + target_compile_definitions(mkw_gx_dl_cache_tests PRIVATE AURORA TARGET_PC=1 NOMINMAX) + target_link_libraries(mkw_gx_dl_cache_tests PRIVATE xxHash::xxhash ${AURORA_SDL3_TARGET}) + set_target_properties(mkw_gx_dl_cache_tests PROPERTIES UNITY_BUILD OFF) + add_test(NAME mkw_gx_dl_cache_tests COMMAND mkw_gx_dl_cache_tests) +endif() + +add_executable(mkw_ax_mix_kernels_tests "${MKW_TEST_RUNTIME_DIR}/tests/ax_mix_kernels_tests.cpp") +target_include_directories(mkw_ax_mix_kernels_tests PRIVATE + "${MKW_TEST_RUNTIME_DIR}/include" + "${MKW_TEST_RUNTIME_DIR}/src/hle/audio") +target_compile_features(mkw_ax_mix_kernels_tests PRIVATE cxx_std_17) +if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|ARM64)$") + target_compile_definitions(mkw_ax_mix_kernels_tests PRIVATE MKW_AX_MIX_REQUIRE_NEON=1) +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|amd64|x86_64|X86_64)$") + target_compile_options(mkw_ax_mix_kernels_tests PRIVATE -march=x86-64-${MKW_X86_CPU_PROFILE}) endif() +set_target_properties(mkw_ax_mix_kernels_tests PROPERTIES UNITY_BUILD OFF) +add_test(NAME mkw_ax_mix_kernels_tests COMMAND mkw_ax_mix_kernels_tests) add_executable(mkw_fiber_manager_tests "${MKW_TEST_RUNTIME_DIR}/tests/fiber_manager_tests.cpp" diff --git a/runtime/include/runtime_config.h b/runtime/include/runtime_config.h index d88ac372e..101c50f59 100644 --- a/runtime/include/runtime_config.h +++ b/runtime/include/runtime_config.h @@ -47,6 +47,7 @@ struct RuntimeUserConfig { std::optional displayMode; std::optional frameInterpolationFps; std::optional metalFxSpatialUpscaling; + std::optional shaderMode; std::optional skipUnreadyPipelines; std::optional vsync; std::optional gxWorker; @@ -294,6 +295,7 @@ inline void EnsureConfigFile() { "frame_interpolation_fps = 0\n" "display_mode = \"windowed\"\n" "graphics_api = \"auto\"\n" + "shader_mode = 'specialized'\n" "skip_unready_pipelines = true\n" "vsync = false\n" "# Runs GX command processing on its own thread. Read once, at startup.\n" @@ -485,6 +487,10 @@ inline RuntimeUserConfig ParseConfigDocument(const toml::value& document) { } config.metalFxSpatialUpscaling = FindConfigValue(document, "video", "metalfx_spatial_upscaling"); + if (auto value = FindConfigValue(document, "video", "shader_mode"); + value && (*value == "specialized" || *value == "hybrid")) { + config.shaderMode = *value; + } config.skipUnreadyPipelines = FindConfigValue(document, "video", "skip_unready_pipelines"); config.vsync = FindConfigValue(document, "video", "vsync"); config.gxWorker = FindConfigValue(document, "video", "gx_worker"); @@ -702,6 +708,12 @@ inline bool SetDisplayMode(std::string value) { return WriteSetting("video", "display_mode", FormatString(value)); } +inline bool SetShaderMode(std::string value) { + if (value != "specialized" && value != "hybrid") return false; + Mutable().shaderMode = value; + return WriteSetting("video", "shader_mode", FormatString(value)); +} + inline bool SetSkipUnreadyPipelines(bool value) { Mutable().skipUnreadyPipelines = value; return WriteSetting("video", "skip_unready_pipelines", value ? "true" : "false"); @@ -1036,6 +1048,8 @@ inline uint32_t FrameInterpolationFps(uint32_t fallback = 0) { return Get().frameInterpolationFps.value_or(fallback); } +inline std::string ShaderMode() { return Get().shaderMode.value_or("specialized"); } + // Whether to skip draws whose graphics pipeline has not finished compiling yet. inline bool SkipUnreadyPipelines(bool fallback = true) { return Get().skipUnreadyPipelines.value_or(fallback); diff --git a/runtime/src/hle/audio/ax_mix_kernels.h b/runtime/src/hle/audio/ax_mix_kernels.h index 687d6bab4..d51a68972 100644 --- a/runtime/src/hle/audio/ax_mix_kernels.h +++ b/runtime/src/hle/audio/ax_mix_kernels.h @@ -3,7 +3,7 @@ #include "isa/big_endian.h" // AX/DSP fixed-point mix kernels: the inner loops of the AXWii command-list mix in ax_mix.cpp. -// Each exists as a scalar reference (a literal transcription of the loop it replaced) and an AVX2 form. +// Each has a scalar reference and architecture-specific SIMD forms. #include #include @@ -16,8 +16,23 @@ #define MKW_AX_MIX_AVX2 0 #endif +#if !MKW_AX_MIX_AVX2 && defined(__aarch64__) && defined(__ARM_NEON) && !defined(__AARCH64EB__) +#include +#define MKW_AX_MIX_NEON 1 +#else +#define MKW_AX_MIX_NEON 0 +#endif + namespace AxMixKernels { +// Mix buses retain the low 32 bits, matching the SIMD adds. +inline int32_t AddWrap32(int32_t value, uint32_t addend) { + const uint32_t bits = static_cast(value) + addend; + int32_t result; + std::memcpy(&result, &bits, sizeof(result)); + return result; +} + inline int16_t ClampToS16(int32_t value) { if (value < -0x8000) { return static_cast(-0x8000); @@ -39,7 +54,7 @@ inline uint16_t MixAddRampScalar(int32_t* out, const int16_t* input, uint32_t co const int32_t scaled = (static_cast(input[i]) * static_cast(volume)) >> 15; const int16_t sample = ClampToS16(scaled); - out[i] += sample; + out[i] = AddWrap32(out[i], static_cast(sample)); volume = static_cast(volume + delta); last = sample; } @@ -90,7 +105,7 @@ inline uint16_t MixAddRampAvx2(int32_t* out, const int16_t* input, uint32_t coun const int32_t scaled = (static_cast(input[i]) * static_cast(volume)) >> 15; const int16_t sample = ClampToS16(scaled); - out[i] += sample; + out[i] = AddWrap32(out[i], static_cast(sample)); volume = static_cast(volume + delta); last = sample; } @@ -99,10 +114,46 @@ inline uint16_t MixAddRampAvx2(int32_t* out, const int16_t* input, uint32_t coun } #endif +#if MKW_AX_MIX_NEON +inline uint16x4_t Ramp4(uint16_t volume, uint16_t delta) { + static constexpr uint16_t lanes[] = {0, 1, 2, 3}; + return vmla_n_u16(vdup_n_u16(volume), vld1_u16(lanes), delta); +} + +inline int16x4_t ScaleBlock4(int16x4_t samples, uint16x4_t ramp) { + const int32x4_t gain = vreinterpretq_s32_u32(vmovl_u16(ramp)); + const int32x4_t product = vmulq_s32(vmovl_s16(samples), gain); + return vqmovn_s32(vshrq_n_s32(product, 15)); +} + +inline uint16_t MixAddRampNeon(int32_t* out, const int16_t* input, uint32_t count, + uint16_t volume, uint16_t delta, int16_t& dpop) { + uint32_t i = 0; + int16_t last = dpop; + if (count >= 4) { + uint16x4_t ramp = Ramp4(volume, delta); + const uint16x4_t step = vdup_n_u16(static_cast(delta * 4u)); + int16x4_t block = vdup_n_s16(0); + for (; count - i >= 4; i += 4) { + block = ScaleBlock4(vld1_s16(input + i), ramp); + vst1q_s32(out + i, vaddw_s16(vld1q_s32(out + i), block)); + ramp = vadd_u16(ramp, step); + } + last = vget_lane_s16(block, 3); + volume = vget_lane_u16(ramp, 0); + } + volume = MixAddRampScalar(out + i, input + i, count - i, volume, delta, last); + dpop = last; + return volume; +} +#endif + inline uint16_t MixAddRamp(int32_t* out, const int16_t* input, uint32_t count, uint16_t volume, uint16_t delta, int16_t& dpop) { #if MKW_AX_MIX_AVX2 return MixAddRampAvx2(out, input, count, volume, delta, dpop); +#elif MKW_AX_MIX_NEON + return MixAddRampNeon(out, input, count, volume, delta, dpop); #else return MixAddRampScalar(out, input, count, volume, delta, dpop); #endif @@ -162,10 +213,29 @@ inline uint16_t ScaleRampAvx2(int16_t* samples, uint32_t count, uint16_t volume, } #endif +#if MKW_AX_MIX_NEON +inline uint16_t ScaleRampNeon(int16_t* samples, uint32_t count, uint16_t volume, + uint16_t delta) { + uint32_t i = 0; + if (count >= 4) { + uint16x4_t ramp = Ramp4(volume, delta); + const uint16x4_t step = vdup_n_u16(static_cast(delta * 4u)); + for (; count - i >= 4; i += 4) { + vst1_s16(samples + i, ScaleBlock4(vld1_s16(samples + i), ramp)); + ramp = vadd_u16(ramp, step); + } + volume = vget_lane_u16(ramp, 0); + } + return ScaleRampScalar(samples + i, count - i, volume, delta); +} +#endif + inline uint16_t ScaleRamp(int16_t* samples, uint32_t count, uint16_t volume, uint16_t delta) { #if MKW_AX_MIX_AVX2 return ScaleRampAvx2(samples, count, volume, delta); +#elif MKW_AX_MIX_NEON + return ScaleRampNeon(samples, count, volume, delta); #else return ScaleRampScalar(samples, count, volume, delta); #endif @@ -179,8 +249,9 @@ inline uint16_t ScaleRamp(int16_t* samples, uint32_t count, uint16_t volume, inline void MixAccumRamp32Scalar(int32_t* dst, const int32_t* src, const uint16_t* ramp, uint32_t count) { for (uint32_t i = 0; i < count; ++i) { - dst[i] += static_cast( - (static_cast(src[i]) * static_cast(ramp[i])) >> 15); + const int64_t product = static_cast(src[i]) * ramp[i]; + const uint32_t scaled = static_cast(static_cast(product) >> 15); + dst[i] = AddWrap32(dst[i], scaled); } } @@ -208,16 +279,37 @@ inline void MixAccumRamp32Avx2(int32_t* dst, const int32_t* src, const uint16_t* _mm256_add_epi32(acc, result)); } for (; i < count; ++i) { - dst[i] += static_cast( - (static_cast(src[i]) * static_cast(ramp[i])) >> 15); + const int64_t product = static_cast(src[i]) * ramp[i]; + const uint32_t scaled = static_cast(static_cast(product) >> 15); + dst[i] = AddWrap32(dst[i], scaled); } } #endif +#if MKW_AX_MIX_NEON +inline void MixAccumRamp32Neon(int32_t* dst, const int32_t* src, const uint16_t* ramp, + uint32_t count) { + uint32_t i = 0; + for (; count - i >= 4; i += 4) { + const int32x4_t source = vld1q_s32(src + i); + const int32x4_t gain = vreinterpretq_s32_u32(vmovl_u16(vld1_u16(ramp + i))); + const int64x2_t lowProduct = vmull_s32(vget_low_s32(source), vget_low_s32(gain)); + const int64x2_t highProduct = vmull_s32(vget_high_s32(source), vget_high_s32(gain)); + // Keep bits 15..46, including products whose shifted value exceeds int32. + const int32x4_t scaled = vcombine_s32(vshrn_n_s64(lowProduct, 15), + vshrn_n_s64(highProduct, 15)); + vst1q_s32(dst + i, vaddq_s32(vld1q_s32(dst + i), scaled)); + } + MixAccumRamp32Scalar(dst + i, src + i, ramp + i, count - i); +} +#endif + inline void MixAccumRamp32(int32_t* dst, const int32_t* src, const uint16_t* ramp, uint32_t count) { #if MKW_AX_MIX_AVX2 MixAccumRamp32Avx2(dst, src, ramp, count); +#elif MKW_AX_MIX_NEON + MixAccumRamp32Neon(dst, src, ramp, count); #else MixAccumRamp32Scalar(dst, src, ramp, count); #endif @@ -270,9 +362,31 @@ inline void LoadBigEndian32Avx2(int32_t* dst, const uint8_t* src, size_t count) } #endif +#if MKW_AX_MIX_NEON +inline void StoreBigEndian32Neon(uint8_t* dst, const int32_t* src, size_t count) { + size_t i = 0; + for (; count - i >= 4; i += 4) { + const uint8x16_t bytes = vreinterpretq_u8_s32(vld1q_s32(src + i)); + vst1q_u8(dst + i * sizeof(uint32_t), vrev32q_u8(bytes)); + } + StoreBigEndian32Scalar(dst + i * sizeof(uint32_t), src + i, count - i); +} + +inline void LoadBigEndian32Neon(int32_t* dst, const uint8_t* src, size_t count) { + size_t i = 0; + for (; count - i >= 4; i += 4) { + const uint8x16_t bytes = vld1q_u8(src + i * sizeof(uint32_t)); + vst1q_s32(dst + i, vreinterpretq_s32_u8(vrev32q_u8(bytes))); + } + LoadBigEndian32Scalar(dst + i, src + i * sizeof(uint32_t), count - i); +} +#endif + inline void StoreBigEndian32(uint8_t* dst, const int32_t* src, size_t count) { #if MKW_AX_MIX_AVX2 StoreBigEndian32Avx2(dst, src, count); +#elif MKW_AX_MIX_NEON + StoreBigEndian32Neon(dst, src, count); #else StoreBigEndian32Scalar(dst, src, count); #endif @@ -281,6 +395,8 @@ inline void StoreBigEndian32(uint8_t* dst, const int32_t* src, size_t count) { inline void LoadBigEndian32(int32_t* dst, const uint8_t* src, size_t count) { #if MKW_AX_MIX_AVX2 LoadBigEndian32Avx2(dst, src, count); +#elif MKW_AX_MIX_NEON + LoadBigEndian32Neon(dst, src, count); #else LoadBigEndian32Scalar(dst, src, count); #endif diff --git a/runtime/src/hle/gx/gx_copy.cpp b/runtime/src/hle/gx/gx_copy.cpp index 2f13b3b50..56640605f 100644 --- a/runtime/src/hle/gx/gx_copy.cpp +++ b/runtime/src/hle/gx/gx_copy.cpp @@ -165,19 +165,13 @@ PPC_NATIVE_OVERRIDE_VOID(8016fc24, GX__SetDispCopyGamma_8016fc24, (uint32_t g), extern "C" void GX__CopyDisp_8016fc38(uint32_t da, uint32_t c) { EnsureAuroraFrameActive(); if (GxWorker::ShouldDefer()) { - // The worker drains like the inline path; EndAuroraFrame joins it before the next frame. + // The copy is ordered with draws in Aurora's FIFO; frame end drains it. GxWorker::Submit([da, c] { - GXDrawDone(); GXCopyDisp(GuestToHostPtr(da), (GXBool)c); }); } else { - // GX copies are FIFO-ordered on hardware. Drain submitted draws before - // resolving the EFB so high-level copies see the same contents. - GXDrawDone(); GXCopyDisp(GuestToHostPtr(da), (GXBool)c); - // Present immediately so post-copy draws don't leak into this frame. Join at the DONE phase - // (not the cheaper SEALED phase GXDrawDone waits for) because ImGui's draw lists, owned by - // Aurora's render worker, replay during encode; aurora_end_frame would join here anyway. + // ImGui draw lists remain owned by the render worker until encoding completes. aurora_wait_for_frame_worker(); } ++g_gxFrameCount; @@ -195,8 +189,8 @@ extern "C" void GX__CopyTex_8016fd74(uint32_t da, uint32_t c) { // Begin the frame before going inline so a deferred begin reaches the worker first. EnsureAuroraFrameActive(); GX_SYNC_INLINE(); - // Match GX FIFO ordering: texture copies observe all prior draws. - GXDrawDone(); + // Flush dirty state without inserting a completion token before the ordered copy. + GXFlush(); const uint16_t rawSrcLeft = g_texCopyState.srcLeft; const uint16_t rawSrcTop = g_texCopyState.srcTop; const uint16_t rawSrcWidth = g_texCopyState.srcWidth; diff --git a/runtime/src/hle/gx/gx_dl.cpp b/runtime/src/hle/gx/gx_dl.cpp index 71e18fbcd..255b19f94 100644 --- a/runtime/src/hle/gx/gx_dl.cpp +++ b/runtime/src/hle/gx/gx_dl.cpp @@ -1,10 +1,10 @@ #include "gx_internal.h" #include "gx_stream_common.h" #include "gx_cp_decode.h" +#include "gx_dl_cache.h" #include "isa/big_endian.h" #include -#include // The display-list scan cache validates a cached scan against the live guest // bytes with a 64-bit XXH3 digest (see GxDisplayListScanCache::CanReuse). @@ -502,10 +502,7 @@ struct DlScanCacheRecord { std::vector flattened{}; }; -struct DlScanCacheState { - std::unordered_map entries{}; - size_t storedCommandBytes = 0; -}; +using DlScanCacheState = GxDisplayListScanCache::BoundedCache; static uint64_t HashScanLayoutState() { if (!g_hleGxState.vtxLayoutHashDirty) { @@ -537,7 +534,7 @@ static uint64_t DlScanCacheKey(uint32_t listAddr, uint32_t nbytes, uint64_t layo } static DlScanCacheState& DlScanCache() { - static thread_local DlScanCacheState s_cache; + static thread_local DlScanCacheState s_cache{kDlScanCacheMaxEntries, kDlScanCacheMaxStoredBytes}; return s_cache; } @@ -560,9 +557,8 @@ static DlScanCacheProbe ProbeDlScanCache(const uint8_t* list, uint32_t listAddr, auto& s_cache = DlScanCache(); const uint64_t key = DlScanCacheKey(listAddr, nbytes, layoutHash); - const auto it = s_cache.entries.find(key); - if (it != s_cache.entries.end()) { - auto& record = it->second; + if (auto* found = s_cache.Find(key)) { + auto& record = *found; const auto& entry = record.result; const bool identityMatches = entry.listAddr == CanonicalizeGxMainRamAddress(listAddr) && entry.nbytes == nbytes && entry.layoutHash == layoutHash; @@ -586,6 +582,9 @@ static DlScanCacheProbe ProbeDlScanCache(const uint8_t* list, uint32_t listAddr, } } } + if (probe.record != nullptr) { + s_cache.Touch(key); + } return probe; } @@ -600,18 +599,6 @@ static void StoreDlScanCache(uint32_t listAddr, uint32_t nbytes, uint64_t layout uint32_t flattenedBytes) { auto& s_cache = DlScanCache(); const uint64_t key = DlScanCacheKey(listAddr, nbytes, layoutHash); - auto old = s_cache.entries.find(key); - bool replacing = old != s_cache.entries.end(); - size_t replacedBytes = replacing ? DlScanCacheRecordBytes(old->second) : 0; - const size_t incomingBytes = static_cast(flattenedBytes) + - cpWrites.size() * sizeof(DlCpWrite); - if ((!replacing && s_cache.entries.size() >= kDlScanCacheMaxEntries) || - s_cache.storedCommandBytes - replacedBytes + incomingBytes > kDlScanCacheMaxStoredBytes) { - s_cache.entries.clear(); - s_cache.storedCommandBytes = 0; - replacing = false; - replacedBytes = 0; - } DlScanCacheRecord record{}; record.result = entry; record.mayContainDraw = mayContainDraw; @@ -626,12 +613,8 @@ static void StoreDlScanCache(uint32_t listAddr, uint32_t nbytes, uint64_t layout if (flattened != nullptr && flattenedBytes != 0) { record.flattened.assign(flattened, flattened + flattenedBytes); } - auto [it, inserted] = s_cache.entries.insert_or_assign(key, std::move(record)); - (void)inserted; - s_cache.storedCommandBytes += DlScanCacheRecordBytes(it->second); - if (replacing) { - s_cache.storedCommandBytes -= replacedBytes; - } + const size_t incomingBytes = DlScanCacheRecordBytes(record); + s_cache.Insert(key, std::move(record), incomingBytes); } static void BuildScanVertexLayout(GXVtxFmt vtxfmt, std::array& indexedSteps, diff --git a/runtime/src/hle/gx/gx_dl_cache.h b/runtime/src/hle/gx/gx_dl_cache.h new file mode 100644 index 000000000..927534214 --- /dev/null +++ b/runtime/src/hle/gx/gx_dl_cache.h @@ -0,0 +1,121 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace GxDisplayListScanCache { + +template +class BoundedCache { + struct Entry { + uint64_t key; + Value value; + size_t bytes; + }; + using Entries = std::list; + using Iterator = typename Entries::iterator; + +public: + static constexpr size_t kMaxEvictionsPerStore = 64; + + BoundedCache(size_t maxEntries, size_t maxBytes) + : m_maxEntries(maxEntries), m_maxBytes(maxBytes) {} + BoundedCache(const BoundedCache&) = delete; + BoundedCache& operator=(const BoundedCache&) = delete; + + Value* Find(uint64_t key) { + const auto it = m_index.find(key); + return it == m_index.end() ? nullptr : &it->second->value; + } + + // Only validated hits refresh recency. + void Touch(uint64_t key) { + const auto it = m_index.find(key); + if (it != m_index.end()) { + m_entries.splice(m_entries.begin(), m_entries, it->second); + } + } + + bool Insert(uint64_t key, Value&& value, size_t bytes) { + static_assert(std::is_nothrow_move_assignable_v); + if (m_maxEntries == 0 || bytes > m_maxBytes) { + return false; + } + const auto found = m_index.find(key); + const auto old = found == m_index.end() ? m_entries.end() : found->second; + size_t retainedEntries = m_entries.size() - (old != m_entries.end() ? 1u : 0u); + size_t retainedBytes = m_storedBytes - (old != m_entries.end() ? old->bytes : 0u); + const auto fits = [&] { + return retainedEntries < m_maxEntries && retainedBytes <= m_maxBytes - bytes; + }; + + std::array victims{}; + size_t victimCount = 0; + auto next = m_entries.end(); + while (!fits() && next != m_entries.begin() && victimCount < victims.size()) { + const auto victim = --next; + if (victim == old) { + continue; + } + victims[victimCount++] = victim; + retainedBytes -= victim->bytes; + --retainedEntries; + } + // Retire at most one batch so later attempts can make progress too. + if (!fits()) { + for (size_t i = 0; i < victimCount; ++i) { + Erase(victims[i]); + } + return false; + } + + if (old == m_entries.end()) { + m_entries.push_front({key, std::move(value), bytes}); + try { + m_index.emplace(key, m_entries.begin()); + } catch (...) { + m_entries.pop_front(); + throw; + } + } else { + old->value = std::move(value); + m_storedBytes -= old->bytes; + old->bytes = bytes; + m_entries.splice(m_entries.begin(), m_entries, old); + } + for (size_t i = 0; i < victimCount; ++i) { + Erase(victims[i]); + } + m_storedBytes += bytes; + return true; + } + + void Clear() { + m_index.clear(); + m_entries.clear(); + m_storedBytes = 0; + } + + size_t Size() const { return m_entries.size(); } + size_t StoredBytes() const { return m_storedBytes; } + +private: + void Erase(Iterator entry) { + m_storedBytes -= entry->bytes; + m_index.erase(entry->key); + m_entries.erase(entry); + } + + size_t m_maxEntries; + size_t m_maxBytes; + Entries m_entries; + std::unordered_map m_index; + size_t m_storedBytes = 0; +}; + +} // namespace GxDisplayListScanCache diff --git a/runtime/src/hle/gx/gx_fifo.cpp b/runtime/src/hle/gx/gx_fifo.cpp index 139b86dba..4bacfc726 100644 --- a/runtime/src/hle/gx/gx_fifo.cpp +++ b/runtime/src/hle/gx/gx_fifo.cpp @@ -13,6 +13,7 @@ HleGxState g_hleGxState; namespace aurora::gx::fifo { bool submit_raw_draw(GXPrimitive prim, GXVtxFmt fmt, const uint8_t* vertices, uint16_t vtxCount, uint32_t vertexBytes); +void write_raw_draw_data(const uint8_t* data, uint32_t length); } void HleGxState::ResetVertex() { @@ -83,6 +84,73 @@ static bool TryGetRawDirectFifoVertexSize(GXVtxFmt fmt, uint32_t& vertexSize) { return vertexSize != 0; } +void GxStream::PrepareImmediateDirectDraw() { + auto& g = g_hleGxState; + g.fifoDirectBytesRemaining = 0; + g.fifoDirectVertexSize = 0; + g.fifoPendingDrawGeneration = g.vtxStateGeneration; + if (!g.inBegin || g.vertsRemaining > 0xffffu || g.currentVtxFmt >= GX_MAX_VTXFMT) { + return; + } + uint32_t vertexSize = 0; + for (int attr = 0; attr < 26; ++attr) { + if (g.vtxDesc[attr] == GX_NONE) { + continue; + } + if (g.vtxDesc[attr] != GX_DIRECT || attr == GX_VA_NRM || attr > GX_VA_TEX7) { + return; + } + const auto& fmt = g.vtxAttrFmt[g.currentVtxFmt][attr]; + if (attr == GX_VA_CLR0 || attr == GX_VA_CLR1) { + // Packed colors and RGBX padding are re-encoded by the attribute writer. + if (!((fmt.type == GX_RGB8 && fmt.cnt == GX_CLR_RGB) || + (fmt.type == GX_RGBA8 && fmt.cnt == GX_CLR_RGBA))) { + return; + } + } else if (!IsMatrixIndexAttr(static_cast(attr))) { + if (static_cast(fmt.type) > GX_F32 || fmt.frac > 31 || + static_cast(fmt.cnt) > 1u) { + return; + } + } + vertexSize += GetDirectAttrByteSizeForFifo(static_cast(attr), fmt); + } + g.fifoDirectVertexSize = vertexSize; + g.fifoDirectBytesRemaining = vertexSize * g.vertsRemaining; +} + +static uint32_t WriteImmediateDirectBytes(const uint8_t* bytes, uint32_t size) { + auto& g = g_hleGxState; + if (!g.inBegin || g.fifoDirectBytesRemaining == 0) { + return 0; + } + if (!g.auroraBeginCalled) { + if (g.fifoPendingDrawGeneration != g.vtxStateGeneration) { + GxStream::PrepareImmediateDirectDraw(); + if (g.fifoDirectBytesRemaining == 0) { + return 0; + } + } + EnsureAuroraFrameActive(); + ApplyAuroraVtxStateForRawBegin(g.currentVtxFmt); + EnsureDefaultGxAlphaCompare(); + GXBegin(g.currentPrim, g.currentVtxFmt, static_cast(g.vertsRemaining)); + g.auroraBeginCalled = true; + GXMarkFrameWork(); + } + const uint32_t count = std::min(size, g.fifoDirectBytesRemaining); + aurora::gx::fifo::write_raw_draw_data(bytes, count); + g.fifoDirectBytesRemaining -= count; + g.vertsRemaining = (g.fifoDirectBytesRemaining + g.fifoDirectVertexSize - 1u) / g.fifoDirectVertexSize; + if (g.fifoDirectBytesRemaining == 0) { + g.inBegin = false; + g.auroraBeginCalled = false; + g.ResetVertex(); + GXEnd(); + } + return count; +} + static bool TrySubmitRawDirectFifoDraw(const uint8_t* packet, uint32_t packetBytes, GXPrimitive prim, GXVtxFmt vtxFmt, uint16_t vtxCount) { if (packet == nullptr || packetBytes == 0 || vtxCount == 0) { @@ -369,6 +437,19 @@ static MKW_PPC_NO_INLINE void HleFifoWriteGeneral(u32 val, uint32_t sizeBytes); // `val` is a raw big-endian bit pattern: the FIFO stream is type-agnostic, and // the float entry point converts before it gets here. void HleFifoWrite(u32 val, uint32_t sizeBytes) { + if (!IsDisplayListActive() && g_hleGxState.fifoDirectBytesRemaining != 0) { + const uint32_t count = sizeBytes == 0 || sizeBytes > 4 ? 4u : sizeBytes; + uint8_t bytes[4]; + BigEndian::Write32(bytes, val); + const uint8_t* data = bytes + 4u - count; + const uint32_t consumed = WriteImmediateDirectBytes(data, count); + if (consumed != 0) { + for (uint32_t i = consumed; i < count; ++i) { + HleFifoWriteGeneral(data[i], 1); + } + return; + } + } if ((sizeBytes == 0 || sizeBytes == 4) && !IsDisplayListActive() && TryAppendIncompleteXfWord(val)) { return; } @@ -385,6 +466,7 @@ static void HleFifoWriteGeneral(u32 val, uint32_t sizeBytes) { auto resetFifoBuffer = [&]() { g_hleGxState.fifoReadOffset = 0; g_hleGxState.fifoByteCount = 0; + g_hleGxState.fifoPendingDrawBytes = 0; }; auto compactFifoBuffer = [&]() { @@ -440,6 +522,7 @@ static void HleFifoWriteGeneral(u32 val, uint32_t sizeBytes) { } g_hleGxState.fifoReadOffset += count; g_hleGxState.fifoByteCount -= count; + g_hleGxState.fifoPendingDrawBytes = 0; if (g_hleGxState.fifoByteCount == 0) { g_hleGxState.fifoReadOffset = 0; } @@ -448,9 +531,17 @@ static void HleFifoWriteGeneral(u32 val, uint32_t sizeBytes) { }; pushBytes(val, sizeBytes == 0 ? 4 : sizeBytes); + if (g_hleGxState.fifoPendingDrawBytes != 0) { + if (g_hleGxState.fifoPendingDrawGeneration == g_hleGxState.vtxStateGeneration && + g_hleGxState.fifoByteCount < g_hleGxState.fifoPendingDrawBytes) { + return; + } + g_hleGxState.fifoPendingDrawBytes = 0; + } // Parse raw FIFO command packets written directly to the gather pipe // (e.g. NW4R/G3D paths that do not call the GXBegin wrapper function). +parseBufferedPackets: while (!g_hleGxState.inBegin) { if (g_hleGxState.fifoByteCount < 1) { break; @@ -535,8 +626,10 @@ static void HleFifoWriteGeneral(u32 val, uint32_t sizeBytes) { ? 3u + static_cast(vtxCount) * rawVertexSize : 0u; // A packet that cannot fit the buffer is streamed per vertex instead. - if (packetBytes != 0 && packetBytes <= g_hleGxState.fifoBytes.size()) { + if (packetBytes != 0 && packetBytes + 3u <= g_hleGxState.fifoBytes.size()) { if (g_hleGxState.fifoByteCount < packetBytes) { + g_hleGxState.fifoPendingDrawBytes = packetBytes; + g_hleGxState.fifoPendingDrawGeneration = g_hleGxState.vtxStateGeneration; break; } if (TrySubmitRawDirectFifoDraw(data, packetBytes, prim, vtxFmt, vtxCount)) { @@ -695,6 +788,9 @@ static void HleFifoWriteGeneral(u32 val, uint32_t sizeBytes) { } break; } + if (!g_hleGxState.inBegin && g_hleGxState.fifoByteCount != 0) { + goto parseBufferedPackets; + } } // Must behave exactly like GX_HLE_FIFO_Write32/16/8 run byte by byte. The ring is not @@ -851,6 +947,15 @@ static void FifoWriteBurstImpl(const uint8_t* data, uint32_t sizeBytes) { return; } + if (!IsDisplayListActive()) { + const uint32_t consumed = WriteImmediateDirectBytes(data, sizeBytes); + data += consumed; + sizeBytes -= consumed; + if (sizeBytes == 0) { + return; + } + } + const uint32_t applied = ApplyFifoPacketsDirect(data, sizeBytes); if (applied < sizeBytes) { HleFifoWriteBurstChunked(data + applied, sizeBytes - applied); diff --git a/runtime/src/hle/gx/gx_init.cpp b/runtime/src/hle/gx/gx_init.cpp index edf6f9c12..8243f8bfc 100644 --- a/runtime/src/hle/gx/gx_init.cpp +++ b/runtime/src/hle/gx/gx_init.cpp @@ -30,6 +30,7 @@ extern "C" uint32_t GX__Init_8016b850(uint32_t fifoBase, uint32_t fifoSize) constexpr uint32_t kGXDataSize = 0x600u; GXInit(GuestToHostPtr(fifoBase, fifoSize), fifoSize); + InvalidateTexObjLoads(); // Initialize GXData structure in guest memory try { diff --git a/runtime/src/hle/gx/gx_internal.h b/runtime/src/hle/gx/gx_internal.h index b8270a9b9..9addd1c07 100644 --- a/runtime/src/hle/gx/gx_internal.h +++ b/runtime/src/hle/gx/gx_internal.h @@ -154,6 +154,8 @@ struct TexObjSlot : TexObjMeta { uint64_t guestShadow[4] = {0, 0, 0, 0}; bool hasGuestShadow = false; + GxTextureBindingContract::ValidatedLoad validatedLoad; + // DCStoreRange interval-index bookkeeping: what this slot currently // contributes to g_texObjIntervals, so a pending update that turns out to // be a no-op costs a compare instead of an erase + insert. @@ -256,6 +258,10 @@ struct HleGxState { std::array fifoBytes{}; size_t fifoReadOffset = 0; size_t fifoByteCount = 0; + uint32_t fifoPendingDrawBytes = 0; + uint64_t fifoPendingDrawGeneration = 0; + uint32_t fifoDirectBytesRemaining = 0; + uint32_t fifoDirectVertexSize = 0; void ResetVertex(); void InvalidateVtxLayoutHash() noexcept { @@ -347,6 +353,10 @@ bool ValidateTlutData(uint32_t objAddr, const TlutObjMeta& meta); TexObjMeta& GetTexObjMeta(uint32_t addr); TexObjMeta ExtractTexObjMetaFromGuest(uint32_t addr); bool TryGetOrExtractTexObjMeta(uint32_t addr, TexObjMeta& outMeta); +// Lookup and publication require g_texObjMutex; reset acquires it itself. +TexObjSlot* TryGetValidatedTexObjSlot(uint32_t addr); +void MarkTexObjLoadValidated(uint32_t addr); +void InvalidateTexObjLoads(); // Guest GXTexObj bytes captured on the game thread; while a scope is active on a thread, texobj // decoding for that address reads the captured bytes instead of live guest memory. diff --git a/runtime/src/hle/gx/gx_objects.cpp b/runtime/src/hle/gx/gx_objects.cpp index 645658dfb..d87665610 100644 --- a/runtime/src/hle/gx/gx_objects.cpp +++ b/runtime/src/hle/gx/gx_objects.cpp @@ -520,6 +520,37 @@ void MarkTexObjSyncedLocked(TexObjSlot& slot) noexcept { } // namespace +TexObjSlot* TryGetValidatedTexObjSlot(uint32_t addr) { + TexObjSlot* slot = FindTexObjSlot(addr); + if (slot == nullptr) { + return nullptr; + } + if (slot->validatedLoad.CanReuse(TexObjSnapshotFor(addr), slot->needsUpload, + slot->host && slot->host->storageLive && slot->host->constructed, + IsPaletteTexFormat(slot->format))) { + return slot; + } + slot->validatedLoad.Invalidate(); + return nullptr; +} + +void MarkTexObjLoadValidated(uint32_t addr) { + TexObjSlot* slot = FindTexObjSlot(addr); + if (slot != nullptr && !slot->needsUpload && slot->host && slot->host->constructed && + !IsPaletteTexFormat(slot->format)) { + // Only call-time bytes may identify a completed load, never newer producer memory. + slot->validatedLoad.Record(TexObjSnapshotFor(addr)); + } +} + +void InvalidateTexObjLoads() { + std::lock_guard guard(g_texObjMutex); + for (auto& [addr, slot] : g_TexObjMeta) { + slot.validatedLoad.Invalidate(); + } + g_boundTexMaps = {}; +} + bool TryGetOrExtractTexObjMeta(uint32_t addr, TexObjMeta& outMeta) { TexObjSlot* slot = FindTexObjSlot(addr); if (slot != nullptr) { @@ -553,6 +584,7 @@ bool TryGetOrExtractTexObjMeta(uint32_t addr, TexObjMeta& outMeta) { const bool extractedUsable = IsUsableTexObjMeta(extracted); if (extractedUsable) { QueueTexObjIntervalUpdate(*slot); + slot->validatedLoad.Invalidate(); static_cast(*slot) = extracted; MarkTexObjSyncedLocked(*slot); outMeta = extracted; @@ -575,6 +607,7 @@ bool TryGetOrExtractTexObjMeta(uint32_t addr, TexObjMeta& outMeta) { // queues the interval-index patch and re-captures the guest shadow before that happens. TexObjMeta& GetTexObjMeta(uint32_t addr) { TexObjSlot& slot = FindOrCreateTexObjSlot(addr); + slot.validatedLoad.Invalidate(); QueueTexObjIntervalUpdate(slot); MarkTexObjSyncedLocked(slot); return slot; @@ -604,6 +637,7 @@ GXTexObj* GetHostTexObj(uint32_t addr) { GXTexObj* CreateHostTexObj(uint32_t addr) { TexObjSlot& slot = FindOrCreateTexObjSlot(addr); + slot.validatedLoad.Invalidate(); // GXInitTexObj/GXInitTexObjCI land here and rewrite the entire guest struct, // resetting the LOD/filter words the HLE does not mirror into this cache. // The shadow captured before that rewrite mismatches afterwards, so the diff --git a/runtime/src/hle/gx/gx_stream_common.h b/runtime/src/hle/gx/gx_stream_common.h index 006ab4711..8167f37de 100644 --- a/runtime/src/hle/gx/gx_stream_common.h +++ b/runtime/src/hle/gx/gx_stream_common.h @@ -36,6 +36,9 @@ constexpr uint8_t GX_DRAW_POINTS_CMD = 0xB8; namespace GxStream { +// Called after the immediate begin state has been initialized. +void PrepareImmediateDirectDraw(); + // --- Big-endian scalar reads out of a command stream --------------------- inline uint16_t ReadBE16(const uint8_t* ptr) { return BigEndian::Read16(ptr); } diff --git a/runtime/src/hle/gx/gx_texture.cpp b/runtime/src/hle/gx/gx_texture.cpp index 0f7953334..f643d9daf 100644 --- a/runtime/src/hle/gx/gx_texture.cpp +++ b/runtime/src/hle/gx/gx_texture.cpp @@ -511,6 +511,34 @@ static void LoadTexObjImpl(uint32_t oa, uint32_t tid) { return; } + if (TexObjSlot* slot = TryGetValidatedTexObjSlot(oa)) { + try { + BoundTexInfo newBound{}; + static_cast(newBound) = *slot; + newBound.objAddr = oa; + newBound.dataAddr = CanonicalizeGxMainRamAddress(slot->dataAddr); + if (!GxTextureBindingContract::CanSkipHostLoad( + g_boundTexMaps[tid], newBound, false, false, false)) { + GXLoadTexObj(slot->host->PublicPtr(), (GXTexMapID)tid); + } + g_boundTexMaps[tid] = newBound; + } catch (const std::exception& ex) { + slot->validatedLoad.Invalidate(); + if (s_hostExceptionLogCount++ < 64) { + RT_LOGF(RT_TAG_GX, "GXLoadTexObj rejected reason=host-exception (%s) oa=0x%08X tid=%u\n", + ex.what(), oa, tid); + } + BindUnloadableTexturePlaceholder(tid); + } catch (...) { + slot->validatedLoad.Invalidate(); + if (s_hostExceptionLogCount++ < 64) { + RT_LOGF(RT_TAG_GX, "GXLoadTexObj rejected reason=host-exception-unknown oa=0x%08X tid=%u\n", oa, tid); + } + BindUnloadableTexturePlaceholder(tid); + } + return; + } + // Check if we have metadata for this texture, or try to extract it from guest memory TexObjMeta meta; if (!TryGetOrExtractTexObjMeta(oa, meta)) { @@ -661,6 +689,7 @@ static void LoadTexObjImpl(uint32_t oa, uint32_t tid) { if (!canSkipHostLoad) { GXLoadTexObj(obj, (GXTexMapID)tid); } + MarkTexObjLoadValidated(oa); } catch (const std::exception& ex) { if (s_hostExceptionLogCount++ < 64) { RT_LOGF(RT_TAG_GX, @@ -681,24 +710,20 @@ static void LoadTexObjImpl(uint32_t oa, uint32_t tid) { } extern "C" void GX__LoadTexObj_80170f2c(uint32_t oa, uint32_t tid) { - if (GxWorker::ShouldDefer()) { - // The CPU-side SDK reads the GXTexObj at call time; the texture data itself is read later. - std::optional bytes; - try { - bytes = CaptureTexObjBytes(oa); - } catch (...) { - } - GxWorker::Submit([oa, tid, bytes] { - if (bytes) { - TexObjSnapshotScope snapshot(oa, *bytes); - LoadTexObjImpl(oa, tid); - } else { - LoadTexObjImpl(oa, tid); - } - }); - } else { - LoadTexObjImpl(oa, tid); + // Inline and deferred loads use the same complete call-time snapshot. + std::optional bytes; + try { + bytes = CaptureTexObjBytes(oa); + } catch (...) { } + GxWorker::Run([oa, tid, bytes] { + if (bytes) { + TexObjSnapshotScope snapshot(oa, *bytes); + LoadTexObjImpl(oa, tid); + } else { + LoadTexObjImpl(oa, tid); + } + }); try { uint32_t gd = Memory::Read32(kGXDataPtrAddr); if (gd) { Memory::Write32(gd + 0x5FCu, Memory::Read32(gd + 0x5FCu) | 1u); Memory::Write16(gd + 2, 0); } } catch (...) {} } PPC_NATIVE_OVERRIDE_VOID(80170f2c, GX__LoadTexObj_80170f2c, (uint32_t oa, uint32_t tid), (oa, tid)); diff --git a/runtime/src/hle/gx/gx_texture_binding_contract.h b/runtime/src/hle/gx/gx_texture_binding_contract.h index f8ca7b233..0ede802b6 100644 --- a/runtime/src/hle/gx/gx_texture_binding_contract.h +++ b/runtime/src/hle/gx/gx_texture_binding_contract.h @@ -1,9 +1,31 @@ #pragma once #include +#include namespace GxTextureBindingContract { +// Independent of the metadata shadow, which getters and setters may refresh. +struct ValidatedLoad { + uint8_t bytes[32]{}; + bool valid = false; + + void Invalidate() noexcept { valid = false; } + + void Record(const uint8_t* snapshot) noexcept { + valid = snapshot != nullptr; + if (valid) { + std::memcpy(bytes, snapshot, sizeof(bytes)); + } + } + + bool CanReuse(const uint8_t* snapshot, bool needsUpload, bool constructed, + bool isPalette) const noexcept { + return valid && snapshot != nullptr && !needsUpload && constructed && !isPalette && + std::memcmp(bytes, snapshot, sizeof(bytes)) == 0; + } +}; + // Every piece of GX texture state that can change the bound view/sampler. Shared by `State` // (this plus the guest GXTexObj address) and HLE's TexObjMeta (this plus the `userData`/ // `needsUpload` bookkeeping fields), so a new sampler field here reaches both. diff --git a/runtime/src/hle/gx/gx_vertex.cpp b/runtime/src/hle/gx/gx_vertex.cpp index 3a8c43b89..e0e90bced 100644 --- a/runtime/src/hle/gx/gx_vertex.cpp +++ b/runtime/src/hle/gx/gx_vertex.cpp @@ -329,11 +329,15 @@ static void GxBeginImpl(uint32_t t, uint32_t vf, uint32_t nv) { g_hleGxState.vertsRemaining=nv; g_hleGxState.inBegin=nv!=0; g_hleGxState.auroraBeginCalled=false; g_hleGxState.fifoReadOffset = 0; g_hleGxState.fifoByteCount=0; g_hleGxState.ResetVertex(); + g_hleGxState.fifoPendingDrawBytes = 0; + GxStream::PrepareImmediateDirectDraw(); } PPC_NATIVE_OVERRIDE_VOID(8016f0f0, GX__Begin_8016f0f0, (uint32_t t, uint32_t vf, uint32_t nv), (t, vf, nv)); extern "C" void GX__End_80044b30() { - GX_DEFER_CALL0(GX__End_80044b30); g_hleGxState.inBegin=false; GXEnd(); } + GX_DEFER_CALL0(GX__End_80044b30); + g_hleGxState.fifoDirectBytesRemaining = 0; + g_hleGxState.inBegin=false; GXEnd(); } PPC_NATIVE_OVERRIDE_VOID(80044b30, GX__End_80044b30, (), ()); extern "C" void GX__End_80048c30() { GX__End_80044b30(); } diff --git a/runtime/src/hle/gx/gx_worker.cpp b/runtime/src/hle/gx/gx_worker.cpp index ac8983e05..32279eb94 100644 --- a/runtime/src/hle/gx/gx_worker.cpp +++ b/runtime/src/hle/gx/gx_worker.cpp @@ -54,7 +54,9 @@ uint64_t g_write = 0; std::atomic g_head{0}; std::atomic g_tail{0}; std::atomic g_consumerWaiting{false}; -std::atomic g_producerWaiting{false}; +constexpr uint64_t kNotWaiting = ~uint64_t{0}; +// Publish the required tail before rechecking it under g_tailMutex. +std::atomic g_waitTarget{kNotWaiting}; std::mutex g_headMutex; std::condition_variable g_headCv; std::mutex g_tailMutex; @@ -66,6 +68,14 @@ std::atomic g_exited{false}; std::thread* g_thread = nullptr; std::vector g_fifoBytes; +#if defined(MKW_GX_WORKER_TESTING) +bool g_testEnabled = true; +bool g_testWakeEachRecord = false; +std::atomic g_testNotifications{0}; +std::atomic g_testWaits{0}; +void (*g_testBeforeTailRecheck)() = nullptr; +#endif + constexpr size_t AlignUp(size_t value) { return (value + detail::kAlign - 1) & ~(detail::kAlign - 1); } inline void CpuRelax() { @@ -117,11 +127,22 @@ void WaitForTail(uint64_t minTail, bool service) { { PerfStats::WaitScope wait; std::unique_lock lock{g_tailMutex}; - g_producerWaiting.store(true, std::memory_order_seq_cst); + g_waitTarget.store(minTail, std::memory_order_seq_cst); +#if defined(MKW_GX_WORKER_TESTING) + if (g_testWakeEachRecord) { + g_waitTarget.store(0, std::memory_order_seq_cst); + } + if (g_testBeforeTailRecheck != nullptr) { + g_testBeforeTailRecheck(); + } +#endif if (g_tail.load(std::memory_order_seq_cst) < minTail) { +#if defined(MKW_GX_WORKER_TESTING) + g_testWaits.fetch_add(1, std::memory_order_relaxed); +#endif WaitTailBriefly(lock); } - g_producerWaiting.store(false, std::memory_order_relaxed); + g_waitTarget.store(kNotWaiting, std::memory_order_relaxed); } if (service && g_waitCallback != nullptr && g_tail.load(std::memory_order_acquire) < minTail) { g_waitCallback(); @@ -234,8 +255,11 @@ void WorkerMain() { } tail += header->size; g_tail.store(tail, std::memory_order_seq_cst); - if (g_producerWaiting.load(std::memory_order_seq_cst)) { + if (tail >= g_waitTarget.load(std::memory_order_seq_cst)) { std::lock_guard lock{g_tailMutex}; +#if defined(MKW_GX_WORKER_TESTING) + g_testNotifications.fetch_add(1, std::memory_order_relaxed); +#endif g_tailCv.notify_all(); } } @@ -342,7 +366,12 @@ void Start() { if (Active()) { return; } - if (!RuntimeConfigFile::GxWorkerEnabled(true)) { +#if defined(MKW_GX_WORKER_TESTING) + const bool enabled = g_testEnabled; +#else + const bool enabled = RuntimeConfigFile::GxWorkerEnabled(true); +#endif + if (!enabled) { RT_LOGF(RT_TAG_GX, "GX worker disabled\n"); return; } @@ -350,6 +379,7 @@ void Start() { g_ring = static_cast(::operator new(kCapacity, std::align_val_t{64})); } g_fifoBytes.reserve(kFifoFlushBytes + 4096); + g_waitTarget.store(kNotWaiting, std::memory_order_relaxed); g_stop.store(false, std::memory_order_relaxed); g_abandon.store(false, std::memory_order_relaxed); g_exited.store(false, std::memory_order_relaxed); diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 3026abd6e..5266e25d0 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -1464,6 +1464,8 @@ int RuntimeMain(int argc, char** argv) { // aurora_initialize does. Wiimote::ConfigureSdlHints(RuntimeConfigFile::WiiRemotesEnabled()); + aurora_set_shader_mode(RuntimeConfigFile::ShaderMode() == "hybrid" + ? AURORA_SHADER_HYBRID : AURORA_SHADER_SPECIALIZED); const AuroraInfo auroraInfo = aurora_initialize(0, nullptr, &auroraConfig); if (auroraInfo.initializationStatus != AURORA_INITIALIZATION_SUCCESS) { throw std::runtime_error(auroraInfo.initializationError != nullptr diff --git a/runtime/src/settings_overlay.cpp b/runtime/src/settings_overlay.cpp index 6efe7e31b..0e32c1334 100644 --- a/runtime/src/settings_overlay.cpp +++ b/runtime/src/settings_overlay.cpp @@ -110,6 +110,7 @@ int g_displayMode = [] { } return static_cast(AURORA_DISPLAY_MODE_WINDOWED); }(); +int g_shaderMode = RuntimeConfigFile::ShaderMode() == "hybrid" ? 1 : 0; bool g_skipUnreadyPipelines = RuntimeConfigFile::SkipUnreadyPipelines(true); bool g_vsync = RuntimeConfigFile::Vsync(); bool g_disableCopyFilter = RuntimeConfigFile::DisableCopyFilter(true); @@ -1278,11 +1279,16 @@ void DrawGraphicsSettings() { aurora_set_disable_copy_filter(g_disableCopyFilter); RuntimeConfigFile::SetDisableCopyFilter(g_disableCopyFilter); } + if (ImGui::Combo("Shaders (restart required)", &g_shaderMode, "Specialized\0Hybrid\0")) { + RuntimeConfigFile::SetShaderMode(g_shaderMode == 1 ? "hybrid" : "specialized"); + } + ImGui::BeginDisabled(g_shaderMode == 1); if (ImGui::Checkbox("Skip draws while shaders compile", &g_skipUnreadyPipelines)) { GX_SYNC_INLINE(); aurora_set_skip_unready_pipelines(g_skipUnreadyPipelines); RuntimeConfigFile::SetSkipUnreadyPipelines(g_skipUnreadyPipelines); } + ImGui::EndDisabled(); if (ImGui::Checkbox("VSync", &g_vsync)) { aurora_enable_vsync(g_vsync); RuntimeConfigFile::SetVsync(g_vsync); diff --git a/runtime/tests/ax_mix_kernels_tests.cpp b/runtime/tests/ax_mix_kernels_tests.cpp new file mode 100644 index 000000000..a9e6f398e --- /dev/null +++ b/runtime/tests/ax_mix_kernels_tests.cpp @@ -0,0 +1,317 @@ +#include "ax_mix_kernels.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(MKW_AX_MIX_REQUIRE_NEON) && !MKW_AX_MIX_NEON +#error The ARM kernel fixture must select NEON +#endif + +namespace { + +struct Backend { + const char* name; + decltype(&AxMixKernels::MixAddRampScalar) mix; + decltype(&AxMixKernels::ScaleRampScalar) scale; + decltype(&AxMixKernels::MixAccumRamp32Scalar) accum; + decltype(&AxMixKernels::StoreBigEndian32Scalar) store; + decltype(&AxMixKernels::LoadBigEndian32Scalar) load; +}; + +constexpr Backend kScalar = {"scalar", AxMixKernels::MixAddRampScalar, + AxMixKernels::ScaleRampScalar, AxMixKernels::MixAccumRamp32Scalar, + AxMixKernels::StoreBigEndian32Scalar, AxMixKernels::LoadBigEndian32Scalar}; +#if MKW_AX_MIX_NEON +constexpr const char* kSelectedName = "NEON"; +#elif MKW_AX_MIX_AVX2 +constexpr const char* kSelectedName = "AVX2"; +#else +constexpr const char* kSelectedName = "selected scalar"; +#endif +constexpr Backend kSelected = {kSelectedName, AxMixKernels::MixAddRamp, + AxMixKernels::ScaleRamp, AxMixKernels::MixAccumRamp32, + AxMixKernels::StoreBigEndian32, AxMixKernels::LoadBigEndian32}; + +uint32_t g_random = 0x12345678u; + +uint32_t NextRandom() { + g_random ^= g_random << 13; + g_random ^= g_random >> 17; + g_random ^= g_random << 5; + return g_random; +} + +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +int32_t Wrap32(int64_t value) { + const uint32_t bits = static_cast(value); + return bits <= 0x7fffffffu ? static_cast(bits) + : static_cast(static_cast(bits) - 0x100000000ll); +} + +int64_t FloorQ15(int64_t product) { + return product >= 0 ? product / 32768 : -((-product + 32767) / 32768); +} + +int16_t ScaleSample(int16_t sample, uint16_t volume) { + const int64_t scaled = FloorQ15(static_cast(sample) * volume); + return static_cast(std::clamp(scaled, -32768, 32767)); +} + +constexpr int16_t kSamples[] = {-32768, 32767, -32767, 16384, -16385, 1, -1, 0}; +constexpr int32_t kWords[] = {std::numeric_limits::min(), + std::numeric_limits::max(), -2147483647, 2147450880, + -65537, 65537, -1, 0, 1}; +constexpr uint16_t kGains[] = {0, 1, 0x3fff, 0x4000, 0x7fff, 0x8000, 0xfffe, 0xffff}; + +std::vector Counts() { + std::vector counts; + for (uint32_t count = 0; count <= 40; ++count) counts.push_back(count); + for (uint32_t count : {63u, 64u, 65u, 95u, 96u, 97u, 159u, 160u, 161u}) { + counts.push_back(count); + } + return counts; +} + +void TestRamps(const Backend& backend, uint32_t count, size_t offset, + uint16_t volume, uint16_t delta, unsigned pattern) { + // Source ends at the allocation boundary so ASan also catches tail overreads. + std::vector input(offset + count, 12345); + std::vector initial(offset + count + 1, 0x13579bdf); + for (uint32_t i = 0; i < count; ++i) { + input[offset + i] = pattern == 0 ? kSamples[i % std::size(kSamples)] + : static_cast(static_cast(NextRandom() & 0xffffu) - 32768); + initial[offset + i] = pattern == 0 ? kWords[i % std::size(kWords)] + : Wrap32(NextRandom()); + } + const auto original = input; + auto expectedMix = initial; + auto expectedScale = input; + int16_t expectedDpop = -23456; + uint16_t expectedVolume = volume; + for (uint32_t i = 0; i < count; ++i) { + const int16_t scaled = ScaleSample(input[offset + i], expectedVolume); + expectedMix[offset + i] = Wrap32(static_cast(initial[offset + i]) + scaled); + expectedScale[offset + i] = scaled; + expectedDpop = scaled; + expectedVolume = static_cast(expectedVolume + delta); + } + auto mixed = initial; + int16_t dpop = -23456; + Require(backend.mix(mixed.data() + offset, input.data() + offset, count, + volume, delta, dpop) == expectedVolume, "mix final volume"); + Require(mixed == expectedMix, "mix output or sentinel"); + Require(dpop == expectedDpop, "mix de-pop sample"); + Require(input == original, "mix modified its source"); + + auto scaled = input; + Require(backend.scale(scaled.data() + offset, count, volume, delta) == expectedVolume, + "scale final volume"); + Require(scaled == expectedScale, "scale output or prefix"); +} + +void TestAccum(const Backend& backend, uint32_t count, size_t offset, unsigned pattern) { + std::vector source(offset + count, 0x2468ace); + std::vector ramp(offset + count, 12345); + std::vector initial(offset + count + 1, 0x13579bdf); + for (uint32_t i = 0; i < count; ++i) { + source[offset + i] = pattern == 0 ? kWords[i % std::size(kWords)] : Wrap32(NextRandom()); + initial[offset + i] = pattern == 0 ? kWords[(i + 1) % std::size(kWords)] + : Wrap32(NextRandom()); + ramp[offset + i] = pattern == 0 ? kGains[i % std::size(kGains)] + : static_cast(NextRandom()); + } + const auto originalSource = source; + const auto originalRamp = ramp; + auto expected = initial; + for (uint32_t i = 0; i < count; ++i) { + expected[offset + i] = Wrap32(static_cast(initial[offset + i]) + + FloorQ15(static_cast(source[offset + i]) * ramp[offset + i])); + } + auto actual = initial; + backend.accum(actual.data() + offset, source.data() + offset, ramp.data() + offset, count); + Require(actual == expected, "aux accumulation or sentinel"); + Require(source == originalSource && ramp == originalRamp, "aux modified its source"); + + auto aliased = initial; + expected = initial; + for (uint32_t i = 0; i < count; ++i) { + expected[offset + i] = Wrap32(static_cast(initial[offset + i]) + + FloorQ15(static_cast(initial[offset + i]) * ramp[offset + i])); + } + backend.accum(aliased.data() + offset, aliased.data() + offset, ramp.data() + offset, count); + Require(aliased == expected, "aux in-place accumulation"); +} + +void TestAccumEdges(const Backend& backend) { + for (int32_t source : kWords) for (int32_t initial : kWords) for (uint16_t gain : kGains) { + std::array src, dst; + std::array ramp; + src.fill(source); + dst.fill(initial); + ramp.fill(gain); + const int32_t expected = Wrap32(static_cast(initial) + + FloorQ15(static_cast(source) * gain)); + backend.accum(dst.data(), src.data(), ramp.data(), 17); + Require(std::all_of(dst.begin(), dst.end(), [expected](int32_t v) { return v == expected; }), + "aux full-width edge"); + } +} + +void TestEndian(const Backend& backend, size_t count, size_t offset) { + std::vector source(offset + count, 0x13579bdf); + for (size_t i = 0; i < count; ++i) { + source[offset + i] = i % 2 == 0 ? kWords[(i / 2) % std::size(kWords)] : Wrap32(NextRandom()); + } + const auto original = source; + std::vector expected(offset + count * 4 + 1, 0xa5); + for (size_t i = 0; i < count; ++i) { + const uint32_t word = static_cast(source[offset + i]); + for (unsigned byte = 0; byte < 4; ++byte) { + expected[offset + i * 4 + byte] = static_cast(word >> (24 - byte * 8)); + } + } + auto bytes = std::vector(expected.size(), 0xa5); + backend.store(bytes.data() + offset, source.data() + offset, count); + Require(bytes == expected, "big-endian store or sentinel"); + Require(source == original, "store modified its source"); + + // Drop the trailing sentinel before loading to expose overreads to ASan. + std::vector loadBytes(expected.begin(), expected.end() - 1); + std::vector loaded(offset + count + 1, 0x13579bdf); + auto expectedWords = loaded; + std::copy(source.begin() + offset, source.end(), expectedWords.begin() + offset); + backend.load(loaded.data() + offset, loadBytes.data() + offset, count); + Require(loaded == expectedWords, "big-endian load or sentinel"); + Require(std::equal(loadBytes.begin(), loadBytes.end(), expected.begin()), "load modified source"); +} + +void TestBackend(const Backend& backend) { + for (uint32_t count : Counts()) for (size_t offset : {1u, 2u, 3u}) { + for (uint16_t volume : kGains) for (uint16_t delta : kGains) { + TestRamps(backend, count, offset, volume, delta, 0); + } + for (unsigned pattern = 0; pattern < 8; ++pattern) TestAccum(backend, count, offset, pattern); + } + for (unsigned trial = 0; trial < 512; ++trial) { + TestRamps(backend, NextRandom() % 162, 1 + NextRandom() % 3, + static_cast(NextRandom()), static_cast(NextRandom()), 1); + } + TestRamps(backend, 65539, 1, 0xffff, 0xffff, 0); + TestAccumEdges(backend); + for (size_t count = 0; count <= 161; ++count) for (size_t offset = 1; offset <= 15; ++offset) { + TestEndian(backend, count, offset); + } +} + +uint64_t Checksum(uint64_t hash, uint32_t value) { + return (hash ^ value) * 1099511628211ull; +} + +#if defined(_MSC_VER) +#define AX_TEST_NOINLINE __declspec(noinline) +#else +#define AX_TEST_NOINLINE __attribute__((noinline)) +#endif + +AX_TEST_NOINLINE uint64_t Benchmark(const Backend& backend, const std::string& kernel, + uint32_t count, uint32_t iterations) { + std::vector samples(count); + std::vector source(count), bus(count); + std::vector ramp(count); + std::vector bytes(count * 4 + 1); + for (uint32_t i = 0; i < count; ++i) { + samples[i] = kSamples[i % std::size(kSamples)]; + source[i] = kWords[i % std::size(kWords)]; + bus[i] = source[i]; + ramp[i] = kGains[i % std::size(kGains)]; + } + AxMixKernels::StoreBigEndian32Scalar(bytes.data() + 1, source.data(), count); + uint16_t volume = 0xfffe; + int16_t dpop = -12345; + const auto start = std::chrono::steady_clock::now(); + if (kernel == "mix") { + for (uint32_t i = 0; i < iterations; ++i) { + volume = backend.mix(bus.data(), samples.data(), count, volume, 0xffff, dpop); + } + } else if (kernel == "scale") { + for (uint32_t i = 0; i < iterations; ++i) { + samples[i % count] = kSamples[i % std::size(kSamples)]; + volume = backend.scale(samples.data(), count, volume, 0xffff); + } + } else if (kernel == "accum") { + for (uint32_t i = 0; i < iterations; ++i) { + backend.accum(bus.data(), source.data(), ramp.data(), count); + } + } else if (kernel == "store") { + for (uint32_t i = 0; i < iterations; ++i) { + source[i % count] = Wrap32(i); + backend.store(bytes.data() + 1, source.data(), count); + } + } else if (kernel == "load") { + for (uint32_t i = 0; i < iterations; ++i) { + bytes[1 + i % (count * 4)] = static_cast(i); + backend.load(bus.data(), bytes.data() + 1, count); + } + } + const auto elapsed = std::chrono::duration( + std::chrono::steady_clock::now() - start).count(); + uint64_t hash = Checksum(Checksum(14695981039346656037ull, volume), static_cast(dpop)); + for (int32_t value : bus) hash = Checksum(hash, static_cast(value)); + for (int16_t value : samples) hash = Checksum(hash, static_cast(value)); + for (uint8_t value : bytes) hash = Checksum(hash, value); + std::printf("%s,%s,%u,%u,%.3f,%.3f,%016llx\n", backend.name, kernel.c_str(), count, + iterations, elapsed / iterations, elapsed / (static_cast(iterations) * count), + static_cast(hash)); + return hash; +} + +void RunBenchmarks(uint32_t iterations) { + std::puts("backend,kernel,samples,iterations,ns_per_block,ns_per_sample,checksum"); + for (uint32_t count : {6u, 18u, 32u, 96u, 160u}) { + for (const char* kernel : {"mix", "scale", "accum", "store", "load"}) { + const uint64_t scalar = Benchmark(kScalar, kernel, count, iterations); + const uint64_t selected = Benchmark(kSelected, kernel, count, iterations); + Require(scalar == selected, "benchmark checksum differs"); + } + } +} + +} // namespace + +int main(int argc, char** argv) { + try { + if (argc >= 2 && std::string(argv[1]) == "--benchmark") { + uint32_t iterations = 200000; + if (argc == 3) { + const std::string argument = argv[2]; + const auto result = std::from_chars(argument.data(), argument.data() + argument.size(), iterations); + Require(result.ec == std::errc{} && result.ptr == argument.data() + argument.size() && + iterations > 0 && iterations <= 100000000, "invalid benchmark iterations"); + } + Require(argc <= 3, "usage: ax_mix_kernels_tests [--benchmark [iterations]]"); + RunBenchmarks(iterations); + } else { + Require(argc == 1, "usage: ax_mix_kernels_tests [--benchmark [iterations]]"); + g_random = 0x12345678u; + TestBackend(kScalar); + g_random = 0x12345678u; + TestBackend(kSelected); + std::printf("AX mix kernels (%s): passed\n", kSelected.name); + } + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; + } +} diff --git a/runtime/tests/gx_copy_tests.cpp b/runtime/tests/gx_copy_tests.cpp new file mode 100644 index 000000000..8a8e04ab9 --- /dev/null +++ b/runtime/tests/gx_copy_tests.cpp @@ -0,0 +1,245 @@ +#include "gx_test_common.hpp" +#include "../src/hle/gx/gx_internal.h" +#include "gx_sync_support/copy_observation.h" +#include "gx/readback.hpp" +#include "__gx.h" + +// Exercise the wrappers without registering guest ABI entry points in the host fixture. +#undef REGISTER_NATIVE_FUNCTION +#define REGISTER_NATIVE_FUNCTION(address, fn) +#include "../src/hle/gx/gx_copy.cpp" + +extern "C" void AuroraResetCopyProbesForTesting(); + +namespace aurora::gfx { +extern uint32_t g_testDrawCount; +namespace testing { +extern std::atomic resolvePassCount; +extern std::vector> readbackJobs; +extern std::vector readbackJobCopies; +std::vector copyObservations; + +void observe_copy(TextureHandle target, ClipRect rect, bool clearColor, bool clearAlpha, bool clearDepth, + GXTexFmt format, const ResolveOptions& options) { + copyObservations.push_back({std::move(target), rect, clearColor, clearAlpha, clearDepth, format, options, + g_testDrawCount}); +} +} // namespace testing +} // namespace aurora::gfx + +extern "C" { +const uint8_t kMkwGuestRegion = 0; +int g_gxFrameCount = 0; +} +TexCopyState g_texCopyState; + +namespace { +constexpr uint32_t kGuestBase = 0x80001000; +std::array g_guestBytes{}; +std::atomic g_drawDoneCallbacks{0}; +uint32_t g_frameJoins = 0; +uint32_t g_readyXfb = 0; +uint32_t g_presents = 0; +uint32_t g_overlayDraws = 0; + +void DrawDoneCallback() { ++g_drawDoneCallbacks; } +} // namespace + +void* GuestToHostPtr(uint32_t address, size_t length) { + const uint32_t canonical = CanonicalizeGxMainRamAddress(address); + const uint32_t base = CanonicalizeGxMainRamAddress(kGuestBase); + if (canonical < base || canonical - base >= g_guestBytes.size() || + length > g_guestBytes.size() - (canonical - base)) { + return nullptr; + } + return g_guestBytes.data() + canonical - base; +} +void EnsureAuroraFrameActive() {} +void VI_HLE_SetXfbReady(uint32_t address) { g_readyXfb = address; } +void VI_HLE_PresentFrame(bool presentedXfb, bool paceToRetrace) { + EXPECT_TRUE(presentedXfb); + EXPECT_TRUE(paceToRetrace); + GxWorker::Sync(); + aurora::gx::fifo::drain(); + ++g_presents; +} +extern "C" void aurora_wait_for_frame_worker() { ++g_frameJoins; } +namespace settings_overlay { +void Draw() noexcept { ++g_overlayDraws; } +} // namespace settings_overlay + +namespace { +namespace stub = aurora::gfx::testing; + +class GxCopyTest : public GXFifoTest, public ::testing::WithParamInterface { +protected: + void SetUp() override { + aurora::gx::fifo::init(); + GXFifoTest::SetUp(); + AuroraResetCopyProbesForTesting(); + AuroraSetGxProducerThread(); + g_texCopyState = {}; + g_gxFrameCount = 0; + g_drawDoneCallbacks = 0; + g_frameJoins = g_readyXfb = g_presents = g_overlayDraws = 0; + g_efbCopyDestinations.clear(); + g_efbCopyBuckets.clear(); + g_largestEfbCopyDestination = 0; + stub::copyObservations.clear(); + stub::resolvePassCount = 0; + stub::readbackJobs.clear(); + stub::readbackJobCopies.clear(); + aurora::gfx::g_testDrawCount = 0; + aurora::gfx::readback::set_supported(true); + GXSetDrawDoneCallback(DrawDoneCallback); + aurora::gx::fifo::begin_frame(); + if (GetParam()) { + GxWorker::Start(); + } + GxWorker::Run([] { + GXSetColorUpdate(GX_TRUE); + GXSetAlphaUpdate(GX_TRUE); + GXSetZMode(GX_TRUE, GX_ALWAYS, GX_TRUE); + GXSetPixelFmt(GX_PF_RGBA6_Z24, GX_ZC_LINEAR); + }); + } + void TearDown() override { + GxWorker::Stop(); + aurora::gx::fifo::drain(); + aurora::gx::fifo::end_frame(); + GXSetDrawDoneCallback(nullptr); + AuroraResetCopyProbesForTesting(); + aurora::gfx::readback::shutdown(); + aurora::gfx::readback::set_supported(false); + GXFifoTest::TearDown(); + } + void Drain() { + GxWorker::Sync(); + AuroraGXSync(); + } + void Draw() { + GxWorker::Run([] { + GXClearVtxDesc(); + GXSetVtxDesc(GX_VA_POS, GX_DIRECT); + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_U8, 0); + GXBegin(GX_TRIANGLES, GX_VTXFMT0, 3); + GXPosition3u8(0, 1, 2); + GXPosition3u8(3, 4, 5); + GXPosition3u8(6, 7, 8); + GXEnd(); + }); + } + void Copy(uint32_t address, GXBool clear, GXTexFmt format = GX_TF_RGBA8, u16 width = 8, u16 height = 8) { + GX__SetTexCopySrc_8016f478(2, 4, width, height); + GX__SetTexCopyDst_8016f4dc(width, height, format, GX_FALSE); + GX__CopyTex_8016fd74(address, clear); + } +}; + +TEST_P(GxCopyTest, InterleavedDrawsClearsAndReusedDestinationsNeedNoCompletionTokens) { + Draw(); + Copy(kGuestBase, GX_TRUE); + Draw(); + Copy(kGuestBase + 512, GX_FALSE); + Copy(kGuestBase, GX_FALSE); + Drain(); + ASSERT_EQ(stub::copyObservations.size(), 3u); + EXPECT_EQ(stub::copyObservations[0].draws, 1u); + EXPECT_EQ(stub::copyObservations[1].draws, 2u); + EXPECT_EQ(stub::copyObservations[2].draws, 2u); + const auto& first = stub::copyObservations[0]; + EXPECT_TRUE(first.clearColor); + EXPECT_TRUE(first.clearAlpha); + EXPECT_TRUE(first.clearDepth); + EXPECT_EQ(first.rect.x, 2); + EXPECT_EQ(first.rect.y, 4); + EXPECT_EQ(first.rect.width, 8); + EXPECT_EQ(first.rect.height, 8); + EXPECT_FALSE(stub::copyObservations[1].clearColor); + EXPECT_EQ(first.target, stub::copyObservations[2].target); + EXPECT_EQ(g_drawDoneCallbacks.load(), 0u); + EXPECT_EQ(g_frameJoins, 0u); + EXPECT_EQ(g_efbCopyDestinations.size(), 2u); + GXDrawDone(); + EXPECT_EQ(g_drawDoneCallbacks.load(), 1u); +} + +TEST_P(GxCopyTest, ReadbackIsRecordedAfterItsCopyWithoutAnImplicitFence) { + Draw(); + Copy(kGuestBase, GX_FALSE); + const auto stream = AuroraReadbackCreateStream(); + ASSERT_NE(stream, 0u); + const AuroraReadbackCopySrc source{.key = 7, .src = GuestToHostPtr(kGuestBase)}; + ASSERT_TRUE(AuroraReadbackCaptureCopies(stream, &source, 1)); + Drain(); + ASSERT_EQ(stub::readbackJobs.size(), 1u); + ASSERT_EQ(stub::readbackJobCopies.size(), 1u); + ASSERT_EQ(stub::copyObservations.size(), 1u); + EXPECT_EQ(stub::readbackJobCopies[0], 1u); + ASSERT_EQ(stub::readbackJobs[0]->copies.size(), 1u); + EXPECT_EQ(stub::readbackJobs[0]->copies[0].texture, stub::copyObservations[0].target); + EXPECT_EQ(g_drawDoneCallbacks.load(), 0u); + AuroraReadbackDestroyStream(stream); +} + +TEST_P(GxCopyTest, CopyStillFlushesUnpublishedDirtyState) { + GxWorker::Run([] { + GXClearVtxDesc(); + GXSetVtxDesc(GX_VA_POS, GX_DIRECT); + }); + GxWorker::Sync(); + ASSERT_NE(__gx->dirtyState, 0u); + Copy(kGuestBase, GX_FALSE); + EXPECT_EQ(__gx->dirtyState, 0u); + Drain(); + EXPECT_EQ(g_drawDoneCallbacks.load(), 0u); +} + +TEST_P(GxCopyTest, InvalidationRetiresTheQueuedCopyBeforeLaterConsumers) { + Copy(kGuestBase, GX_FALSE); + Copy(kGuestBase + 512, GX_FALSE); + InvalidateEfbCopyDestinationsForRange(kGuestBase, 256); + Drain(); + EXPECT_FALSE(aurora::gx::g_gxState.copyTextures.contains(GuestToHostPtr(kGuestBase))); + EXPECT_TRUE(aurora::gx::g_gxState.copyTextures.contains(GuestToHostPtr(kGuestBase + 512))); + EXPECT_EQ(g_efbCopyDestinations.size(), 1u); + EXPECT_EQ(g_drawDoneCallbacks.load(), 0u); +} + +TEST_P(GxCopyTest, PersistentCopyPolicyAndDepthProbesSurviveWithoutTheWait) { + Copy(kGuestBase, GX_FALSE); + Drain(); + ASSERT_EQ(stub::copyObservations.size(), 1u); + EXPECT_TRUE(stub::copyObservations.back().options.persistent); + // The renderer stub retains this field, allowing the real copy policy to see a warm destination. + aurora::gx::g_gxState.copyTextures[GuestToHostPtr(kGuestBase)].consecutiveFrames = 8; + Copy(kGuestBase, GX_FALSE, GX_TF_RGBA8, 640, 480); + Drain(); + EXPECT_FALSE(stub::copyObservations.back().options.persistent); + const auto probe = kGuestBase + (GetParam() ? 2048u : 3072u); + Copy(probe, GX_FALSE, GX_TF_Z8, 4, 4); + Drain(); + EXPECT_FALSE(stub::copyObservations.back().options.persistent); + EXPECT_FALSE(stub::readbackJobs.empty()); + EXPECT_EQ(g_guestBytes[probe - kGuestBase], 0xff); + EXPECT_EQ(g_drawDoneCallbacks.load(), 0u); +} + +TEST_P(GxCopyTest, DisplayCopyRetainsPresentationAndInlineRenderWorkerJoin) { + Draw(); + GxWorker::Run([] { + GXSetDispCopySrc(0, 0, 640, 480); + GXSetDispCopyDst(640, 480); + }); + GX__CopyDisp_8016fc38(kGuestBase, GX_TRUE); + EXPECT_EQ(stub::copyObservations.size(), 1u); + EXPECT_EQ(g_readyXfb, kGuestBase); + EXPECT_EQ(g_presents, 1u); + EXPECT_EQ(g_overlayDraws, 1u); + EXPECT_EQ(g_gxFrameCount, 1); + EXPECT_EQ(g_frameJoins, GetParam() ? 0u : 1u); + EXPECT_EQ(g_drawDoneCallbacks.load(), 0u); +} + +INSTANTIATE_TEST_SUITE_P(WorkerDisabledAndEnabled, GxCopyTest, ::testing::Bool()); +} // namespace diff --git a/runtime/tests/gx_direct_draw_tests.cpp b/runtime/tests/gx_direct_draw_tests.cpp new file mode 100644 index 000000000..6ddd64f41 --- /dev/null +++ b/runtime/tests/gx_direct_draw_tests.cpp @@ -0,0 +1,280 @@ +#include "gx_test_common.hpp" +#include "../src/hle/gx/gx_internal.h" +#include "../src/hle/gx/gx_stream_common.h" +#include "isa/big_endian.h" + +#include +#include + +namespace { +class RuntimeDirectDrawTest : public GXFifoTest { +protected: + void SetUp() override { + GXFifoTest::SetUp(); + reset_runtime(); + } + + void reset_runtime() { + GXInit(nullptr, 0); + aurora::gx::fifo::clear_buffer(); + g_hleGxState = {}; + g_dlRecordState = {}; + g_alphaCompareValid = false; + GxWorker::detail::g_active = false; + GxWorker::detail::t_onWorker = false; + } + + void position(GXCompCnt cnt = GX_POS_XYZ, GXCompType type = GX_U8, u8 frac = 0) { + g_hleGxState.vtxDesc[GX_VA_POS] = GX_DIRECT; + g_hleGxState.vtxAttrFmt[GX_VTXFMT0][GX_VA_POS] = {cnt, type, frac}; + g_hleGxState.InvalidateVtxLayoutHash(); + } + + void begin(u16 count, bool direct = true) { + auto& g = g_hleGxState; + GxStream::PublishAuroraVtxState(GX_VTXFMT0, {false, GX_VA_POS, GX_VA_TEX7}); + g.currentVtxFmt = GX_VTXFMT0; + g.currentPrim = GX_TRIANGLES; + g.vertsRemaining = count; + g.inBegin = count != 0; + g.auroraBeginCalled = false; + g.fifoReadOffset = 0; + g.fifoByteCount = 0; + g.fifoPendingDrawBytes = 0; + g.ResetVertex(); + GxStream::PrepareImmediateDirectDraw(); + if (!direct) { + g.fifoDirectBytesRemaining = 0; + } + } + + void write(const std::vector& bytes, u32 width) { + size_t pos = 0; + for (; pos + width <= bytes.size(); pos += width) { + const u32 value = width == 4 ? BigEndian::Read32(bytes.data() + pos) + : width == 2 ? BigEndian::Read16(bytes.data() + pos) : bytes[pos]; + HleFifoWrite(value, width); + } + for (; pos < bytes.size(); ++pos) { + HleFifoWrite(bytes[pos], 1); + } + } +}; + +TEST_F(RuntimeDirectDrawTest, DirectNumericPayloadMatchesIncrementalForEveryWriteWidth) { + for (GXCompType type : {GX_U8, GX_S8, GX_U16, GX_S16, GX_F32}) { + for (GXCompCnt cnt : {GX_POS_XY, GX_POS_XYZ}) { + for (u32 width : {1u, 2u, 4u}) { + const u32 bytesPerVertex = (cnt == GX_POS_XY ? 2u : 3u) * GetCompSizeBytes(type); + std::vector payload(bytesPerVertex * 3); + for (size_t i = 0; i < payload.size(); ++i) { + payload[i] = static_cast(i * 17u + 11u); + } + reset_runtime(); + position(cnt, type, 7); + begin(3, false); + write(payload, width); + const auto reference = capture_fifo(); + reset_runtime(); + position(cnt, type, 7); + begin(3); + ASSERT_EQ(g_hleGxState.fifoDirectBytesRemaining, payload.size()); + write(payload, width); + EXPECT_EQ(capture_fifo(), reference) << "type=" << type << " count=" << cnt << " width=" << width; + EXPECT_FALSE(g_hleGxState.inBegin); + EXPECT_EQ(g_hleGxState.vertsRemaining, 0u); + } + } + } +} + +TEST_F(RuntimeDirectDrawTest, MixedMatrixColorsAndAllTextureCoordinatesMatchIncremental) { + std::vector reference; + for (bool direct : {false, true}) { + reset_runtime(); + position(GX_POS_XY, GX_S16, 11); + auto& g = g_hleGxState; + g.vtxDesc[GX_VA_PNMTXIDX] = GX_DIRECT; + g.vtxDesc[GX_VA_TEX7MTXIDX] = GX_DIRECT; + g.vtxDesc[GX_VA_CLR0] = GX_DIRECT; + g.vtxAttrFmt[0][GX_VA_CLR0] = {GX_CLR_RGB, GX_RGB8, 0}; + g.vtxDesc[GX_VA_CLR1] = GX_DIRECT; + g.vtxAttrFmt[0][GX_VA_CLR1] = {GX_CLR_RGBA, GX_RGBA8, 0}; + for (int tex = GX_VA_TEX0; tex <= GX_VA_TEX7; ++tex) { + g.vtxDesc[tex] = GX_DIRECT; + g.vtxAttrFmt[0][tex] = {GX_TEX_ST, GX_S16, static_cast(tex)}; + } + begin(3, direct); + ASSERT_EQ(g.fifoDirectBytesRemaining, direct ? 45u * 3u : 0u); + const std::vector payload(45u * 3u, 0x12); + GX_HLE_FIFO_WriteBurst(payload.data(), static_cast(payload.size())); + if (!direct) { + reference = capture_fifo(); + } else { + EXPECT_EQ(capture_fifo(), reference); + } + } +} + +TEST_F(RuntimeDirectDrawTest, NormalAliasesIndexedAndReencodedColorsKeepFallback) { + for (GXCompCnt cnt : {GX_NRM_XYZ, GX_NRM_NBT, GX_NRM_NBT3}) { + reset_runtime(); + position(); + g_hleGxState.vtxDesc[GX_VA_NRM] = GX_DIRECT; + g_hleGxState.vtxAttrFmt[0][GX_VA_NRM] = {cnt, GX_S16, 14}; + begin(3); + EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, 0u); + } + for (GXCompType type : {GX_RGB565, GX_RGBX8, GX_RGBA4, GX_RGBA6}) { + reset_runtime(); + position(); + g_hleGxState.vtxDesc[GX_VA_CLR0] = GX_DIRECT; + g_hleGxState.vtxAttrFmt[0][GX_VA_CLR0] = {GX_CLR_RGBA, type, 0}; + begin(3); + EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, 0u); + } + for (GXAttrType type : {GX_INDEX8, GX_INDEX16}) { + reset_runtime(); + position(); + g_hleGxState.vtxDesc[GX_VA_POS] = type; + begin(3); + EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, 0u); + } +} + +TEST_F(RuntimeDirectDrawTest, CompletingWordAndBurstTailAreParsedImmediately) { + for (bool burst : {false, true}) { + reset_runtime(); + position(); + begin(1); + const std::vector stream{0x11, 0x22, 0x33, 0x48}; + if (burst) { + GX_HLE_FIFO_WriteBurst(stream.data(), static_cast(stream.size())); + } else { + HleFifoWrite(0x11223348, 4); + } + const auto bytes = capture_fifo(); + ASSERT_FALSE(bytes.empty()); + EXPECT_EQ(bytes.back(), 0x48); + EXPECT_EQ(g_hleGxState.fifoByteCount, 0u); + EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, 0u); + } +} + +TEST_F(RuntimeDirectDrawTest, RawPacketsReserveCompletingWordTailAtBufferBoundary) { + for (u16 packetBytes : {4093, 4094, 4095, 4096}) { + reset_runtime(); + g_hleGxState.vtxDesc[GX_VA_PNMTXIDX] = GX_DIRECT; + const u16 count = packetBytes - 3; + HleFifoWrite(0x90, 1); + HleFifoWrite(count, 2); + EXPECT_EQ(g_hleGxState.fifoPendingDrawBytes, packetBytes == 4093 ? packetBytes : 0u); + for (u32 i = 0; i < count - 1u; ++i) { + HleFifoWrite(0x12, 1); + } + HleFifoWrite(0x12480000, 4); + const auto bytes = capture_fifo(); + ASSERT_GE(bytes.size(), count + 4u); + const auto drawStart = bytes.size() - count - 4u; + EXPECT_EQ(bytes[drawStart], 0x90); + EXPECT_EQ(BigEndian::Read16(bytes.data() + drawStart + 1u), count); + for (size_t i = drawStart + 3u; i < drawStart + count + 3u; ++i) { + ASSERT_EQ(bytes[i], 0x12) << "packet=" << packetBytes; + } + EXPECT_EQ(bytes.back(), 0x48); + EXPECT_EQ(g_hleGxState.fifoByteCount, 0u); + EXPECT_EQ(g_hleGxState.fifoPendingDrawBytes, 0u); + EXPECT_FALSE(g_hleGxState.inBegin); + } +} + +TEST_F(RuntimeDirectDrawTest, PendingRawLengthIsRecomputedAfterLayoutInvalidation) { + position(); + HleFifoWrite(0x90, 1); + HleFifoWrite(1, 2); + ASSERT_EQ(g_hleGxState.fifoPendingDrawBytes, 6u); + position(GX_POS_XY); + HleFifoWrite(0x1122, 2); + EXPECT_EQ(g_hleGxState.fifoPendingDrawBytes, 0u); + EXPECT_EQ(g_hleGxState.fifoByteCount, 0u); + EXPECT_FALSE(g_hleGxState.inBegin); +} + +TEST_F(RuntimeDirectDrawTest, CompletingWordCanStartPartialCpPacket) { + position(GX_POS_XY); + begin(1); + HleFifoWrite(0x11220850, 4); + EXPECT_FALSE(g_hleGxState.inBegin); + EXPECT_EQ(g_hleGxState.fifoByteCount, 2u); + HleFifoWrite(3u << 9, 4); + EXPECT_EQ(g_hleGxState.vtxDesc[GX_VA_POS], GX_INDEX16); + EXPECT_EQ(g_hleGxState.fifoByteCount, 0u); +} + +TEST_F(RuntimeDirectDrawTest, LayoutChangeBeforeFirstPayloadRechecksDirectEligibility) { + position(); + begin(1); + g_hleGxState.vtxDesc[GX_VA_NRM] = GX_DIRECT; + g_hleGxState.vtxAttrFmt[0][GX_VA_NRM] = {GX_NRM_XYZ, GX_S16, 14}; + g_hleGxState.InvalidateVtxLayoutHash(); + HleFifoWrite(0x11, 1); + EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, 0u); + EXPECT_TRUE(g_hleGxState.auroraBeginCalled); +} + +TEST_F(RuntimeDirectDrawTest, DirectPayloadLargerThanQueueStillMatchesIncremental) { + std::vector reference; + const std::vector payload(4096u * 3u, 0x32); + for (bool direct : {false, true}) { + reset_runtime(); + position(); + begin(4096, direct); + GX_HLE_FIFO_WriteBurst(payload.data(), static_cast(payload.size())); + EXPECT_FALSE(g_hleGxState.inBegin); + EXPECT_EQ(g_hleGxState.fifoByteCount, 0u); + if (!direct) { + reference = capture_fifo(); + } else { + EXPECT_EQ(capture_fifo(), reference); + } + } +} + +TEST_F(RuntimeDirectDrawTest, RecordingBypassesPreparedDirectPayload) { + position(); + begin(1); + g_dlRecordState = {0x80001000, 32, 0x80001000, 0, true}; + HleFifoWrite(0x11223344, 4); + EXPECT_EQ(g_dlRecordState.count, 4u); + EXPECT_EQ(Memory::Read32(0x80001000), 0x11223344u); + EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, 3u); + EXPECT_TRUE(capture_fifo().empty()); + g_dlRecordState.active = false; +} + +TEST_F(RuntimeDirectDrawTest, ZeroCountDoesNotPrepareDirectPayload) { + position(); + begin(0); + EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, 0u); + EXPECT_FALSE(g_hleGxState.inBegin); +} + +TEST_F(RuntimeDirectDrawTest, DISABLED_SubmissionBenchmark) { + constexpr u32 draws = 20000; + const std::vector payload(3u * 4u, 0x32); + for (bool direct : {false, true, true, false}) { + reset_runtime(); + position(); + const auto start = std::chrono::steady_clock::now(); + for (u32 i = 0; i < draws; ++i) { + begin(4, direct); + write(payload, 4); + } + const auto end = std::chrono::steady_clock::now(); + const auto bytes = capture_fifo(); + std::printf("direct=%d draws=%u elapsed_us=%lld fifo_bytes=%zu\n", direct, draws, + static_cast(std::chrono::duration_cast(end - start).count()), + bytes.size()); + } +} +} // namespace diff --git a/runtime/tests/gx_dl_cache_tests.cpp b/runtime/tests/gx_dl_cache_tests.cpp new file mode 100644 index 000000000..585eb0e90 --- /dev/null +++ b/runtime/tests/gx_dl_cache_tests.cpp @@ -0,0 +1,301 @@ +#include "../src/hle/gx/gx_dl_cache.h" +#include "../src/hle/gx/gx_internal.h" + +#include + +#include +#include +#include +#include +#include + +extern "C" const uint8_t kMkwGuestRegion = 0; + +namespace { + +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +struct Record { + uint32_t listAddr = 0; + uint32_t nbytes = 0; + uint64_t layoutHash = 0; + uint64_t contentDigest = 0; + uint64_t writeGeneration = GxGuestWrite::kUntracked; + std::vector cpWrites; + std::vector flattened; + + size_t Bytes() const { return flattened.size() + cpWrites.size() * sizeof(uint64_t); } +}; + +using Cache = GxDisplayListScanCache::BoundedCache; + +bool Store(Cache& cache, uint64_t key, size_t flattenedBytes, size_t cpWrites = 0) { + Record record{}; + record.cpWrites.resize(cpWrites, key); + record.flattened.resize(flattenedBytes, static_cast(key)); + const size_t bytes = record.Bytes(); + return cache.Insert(key, std::move(record), bytes); +} + +void EntryBudgetAndRecency() { + Cache cache{3, 1024}; + for (uint64_t key = 1; key <= 3; ++key) Require(Store(cache, key, 0), "initial insert failed"); + auto* hot = cache.Find(1); + cache.Touch(1); + Require(Store(cache, 4, 0), "entry-budget insert failed"); + Require(cache.Size() == 3 && cache.StoredBytes() == 0, "entry budget exceeded"); + Require(cache.Find(1) == hot && cache.Find(2) == nullptr && cache.Find(3) && cache.Find(4), + "entry-budget eviction discarded the hot working set"); + cache.Find(3); + cache.Touch(999); + Require(Store(cache, 5, 0), "second entry-budget insert failed"); + Require(!cache.Find(3) && cache.Find(1), "unvalidated probe refreshed recency"); +} + +void ByteBudgetAndReplacement() { + Cache cache{3, 12}; + Require(Store(cache, 1, 4) && Store(cache, 2, 4) && Store(cache, 3, 4), "byte-budget setup failed"); + auto* retained = cache.Find(3); + Require(Store(cache, 1, 8), "protected replacement failed"); + Require(cache.Size() == 2 && cache.StoredBytes() == 12, "replacement byte accounting failed"); + Require(cache.Find(1) && !cache.Find(2) && cache.Find(3) == retained, + "oldest replacement was evicted or unrelated record moved"); + Require(cache.Find(1)->flattened == std::vector(8, 1), "replacement payload was lost"); + Require(Store(cache, 4, 4), "byte-budget eviction failed"); + Require(cache.Size() == 2 && cache.StoredBytes() == 12 && !cache.Find(3) && cache.Find(1), + "replacement was not promoted to most recent"); + Require(Store(cache, 1, 0), "shrinking replacement failed"); + Require(cache.Size() == 2 && cache.StoredBytes() == 4, "shrinking replacement leaked bytes"); + Require(Store(cache, 5, 0, 1), "CP-write insertion failed"); + Require(cache.Size() == 3 && cache.StoredBytes() == 12, "CP-write bytes were not counted"); + Require(Store(cache, 5, 4), "CP-write replacement failed"); + Require(cache.StoredBytes() == 8, "CP-write replacement counted old bytes twice"); +} + +void RejectedInsertsPreserveRecords() { + Cache cache{2, 12}; + Require(Store(cache, 1, 8) && Store(cache, 2, 4), "oversize setup failed"); + const auto* old = cache.Find(1); + Require(!Store(cache, 3, 13) && !Store(cache, 1, 13), "oversized record was admitted"); + Require(!cache.Insert(1, Record{}, std::numeric_limits::max()), "overflowing size was admitted"); + Require(cache.Size() == 2 && cache.StoredBytes() == 12 && cache.Find(1) == old && cache.Find(2), + "rejected insertion changed the working set"); + Require(Store(cache, 3, 4) && !cache.Find(1) && cache.Find(2), "rejected insertion changed recency"); + + Cache disabled{0, 12}; + Require(!Store(disabled, 1, 0) && disabled.Size() == 0, "disabled entry budget accepted a record"); + Cache noBytes{1, 0}; + Require(Store(noBytes, 1, 0) && !Store(noBytes, 1, 1) && noBytes.Find(1), + "zero byte budget mishandled an empty record"); +} + +void EvictionLimitAndProtection() { + Cache cache{256, 128}; + for (uint64_t key = 1; key <= 128; ++key) Require(Store(cache, key, 1), "bounded setup failed"); + const auto* protectedRecord = cache.Find(1); + Require(Store(cache, 1, 65), "replacement needing exactly 64 victims failed"); + Require(cache.Size() == 64 && cache.StoredBytes() == 128 && cache.Find(1) == protectedRecord, + "protected replacement was counted as a victim"); + for (uint64_t key = 2; key <= 65; ++key) Require(!cache.Find(key), "wrong LRU victim selected"); + for (uint64_t key = 66; key <= 128; ++key) Require(cache.Find(key), "too many records evicted"); + + cache.Clear(); + for (uint64_t key = 1; key <= 128; ++key) Require(Store(cache, key, 1), "bounded refill failed"); + Require(Store(cache, 129, 64), "new insertion needing exactly 64 victims failed"); + Require(cache.Size() == 65 && cache.StoredBytes() == 128 && !cache.Find(64) && cache.Find(65), + "new insertion evicted the wrong count"); +} + +void BoundedRejectionsMakeProgress() { + Cache cache{256, 128}; + for (uint64_t key = 1; key <= 128; ++key) Require(Store(cache, key, 1), "rejection setup failed"); + Require(!Store(cache, 129, 65), "new insertion exceeded 64 victims"); + Require(cache.Size() == 64 && cache.StoredBytes() == 64 && !cache.Find(64) && cache.Find(65), + "rejected insertion did not stop at 64 victims"); + Require(Store(cache, 129, 65) && cache.Size() == 64 && cache.StoredBytes() == 128, + "rejected insertion did not make progress for the next attempt"); + + cache.Clear(); + for (uint64_t key = 1; key <= 128; ++key) Require(Store(cache, key, 1), "protected rejection setup failed"); + const auto* protectedRecord = cache.Find(1); + Require(!Store(cache, 1, 66), "replacement exceeded 64 victims"); + Require(cache.Size() == 64 && cache.StoredBytes() == 64 && cache.Find(1) == protectedRecord && + protectedRecord->flattened.size() == 1, "bounded rejection lost the protected replacement"); + Require(Store(cache, 1, 66) && cache.Size() == 63 && cache.StoredBytes() == 128, + "replacement did not make progress for the next attempt"); + + cache.Clear(); + for (uint64_t key = 1; key <= 64; ++key) Require(Store(cache, key, 0), "empty record setup failed"); + Require(Store(cache, 65, 128), "large record setup failed"); + Require(!Store(cache, 66, 1) && cache.Size() == 1 && cache.StoredBytes() == 128, + "zero-byte victims bypassed the eviction work limit"); + Require(Store(cache, 66, 1) && cache.Size() == 1 && cache.StoredBytes() == 1, + "zero-byte victims caused repeated insertion starvation"); +} + +void ClearAndPointerLifetime() { + Cache cache{8192, 8192}; + Require(Store(cache, 0, 1), "pointer setup failed"); + const auto* first = cache.Find(0); + const auto* payload = first->flattened.data(); + for (uint64_t key = 1; key < 8192; ++key) Require(Store(cache, key, 1), "growth insert failed"); + cache.Touch(0); + for (uint64_t key = 8192; key < 16384; ++key) { + Require(Store(cache, key, 1), "churn insert failed"); + Require(cache.Find(0) == first && first->flattened.data() == payload, + "rehash or unrelated eviction invalidated a retained stream"); + cache.Touch(0); + } + Require(cache.Size() == 8192 && cache.StoredBytes() == 8192, "churn exceeded a budget"); + cache.Clear(); + cache.Clear(); + Require(cache.Size() == 0 && cache.StoredBytes() == 0 && !cache.Find(0), "clear left stale accounting"); + Require(Store(cache, 0, 0), "insertion after clear failed"); +} + +void LiveBytesStillInvalidateRetainedRecords() { + constexpr uint32_t address = 0x80010000; + constexpr uint64_t layout = 0x12345678; + std::array bytes{0x08, 0x50, 0, 0, 0, 1, 0, 0}; + const auto digest = [&] { return static_cast(XXH3_64bits(bytes.data(), bytes.size())); }; + const auto key = GxDisplayListScanCache::MakeIdentityKey(address, bytes.size(), layout); + Require(key == GxDisplayListScanCache::MakeIdentityKey(0xC0010000, bytes.size(), layout), + "cached and uncached aliases use different keys"); + Cache cache{2, 32}; + Record record{}; + record.listAddr = CanonicalizeGxMainRamAddress(address); + record.nbytes = bytes.size(); + record.layoutHash = layout; + record.contentDigest = digest(); + record.writeGeneration = GxGuestWrite::GenerationForRange(address, bytes.size()); + Require(cache.Insert(key, std::move(record), 0), "validation setup failed"); + auto* retained = cache.Find(key); + const auto reusable = [&](uint32_t addr, uint32_t count, uint64_t currentLayout, uint64_t currentDigest) { + return GxDisplayListScanCache::CanReuse(retained->listAddr, retained->nbytes, retained->layoutHash, + retained->contentDigest, addr, count, currentLayout, + currentDigest); + }; + Require(reusable(0xC0010000, bytes.size(), layout, digest()), "unchanged alias was rejected"); + Require(!reusable(address, bytes.size() - 1, layout, digest()) && + !reusable(address, bytes.size(), layout + 1, digest()) && + !reusable(address + 32, bytes.size(), layout, digest()), "identity mismatch was reused"); + GxGuestWrite::NotifyWrite(0xC0010000, bytes.size()); + const auto generation = GxGuestWrite::GenerationForRange(address, bytes.size()); + Require(!GxGuestWrite::CanSkipDigest(retained->writeGeneration, generation), "write failed to invalidate digest"); + Require(reusable(address, bytes.size(), layout, digest()), "unchanged bytes after a write were rejected"); + bytes[5] ^= 0x80; + Require(!reusable(address, bytes.size(), layout, digest()), "changed command bytes reused a retained record"); + Require(!GxGuestWrite::CanSkipDigest(GxGuestWrite::kUntracked, GxGuestWrite::kUntracked), + "untracked writes bypassed digest validation"); + Require(Store(cache, 1, 16), "validation churn setup failed"); + cache.Touch(key); + Require(Store(cache, 2, 16) && cache.Find(key) == retained && !cache.Find(1), + "unrelated eviction discarded the retained record"); + Require(!reusable(address, bytes.size(), layout, digest()), "churn made a stale digest reusable"); + Record replacement = *retained; + replacement.contentDigest = digest(); + replacement.writeGeneration = generation; + Require(cache.Insert(key, std::move(replacement), 0) && cache.StoredBytes() == 16, + "invalidated replacement changed unrelated byte accounting"); + Require(reusable(address, bytes.size(), layout, digest()), "replacement did not refresh the digest"); +} + +struct LegacyCache { + std::unordered_map entries; + size_t storedBytes = 0; + size_t maxEntries; + size_t maxBytes; + + Record* Find(uint64_t key) { + const auto it = entries.find(key); + return it == entries.end() ? nullptr : &it->second; + } + void Touch(uint64_t) {} + size_t Size() const { return entries.size(); } + bool Insert(uint64_t key, Record&& value, size_t bytes) { + const auto it = entries.find(key); + const bool replacing = it != entries.end(); + size_t replacedBytes = replacing ? it->second.Bytes() : 0; + if ((!replacing && entries.size() >= maxEntries) || storedBytes - replacedBytes + bytes > maxBytes) { + entries.clear(); + storedBytes = 0; + replacedBytes = 0; + } + entries.insert_or_assign(key, std::move(value)); + storedBytes = storedBytes - replacedBytes + bytes; + return true; + } +}; + +template +void Benchmark(Storage& cache, std::string_view policy, std::string_view scenario, + size_t hotCount, size_t coldCount, size_t bytesPerEntry) { + using Clock = std::chrono::steady_clock; + uint64_t hits = 0, misses = 0, evictions = 0, skips = 0; + std::vector batches; + const auto access = [&](uint64_t key) { + if (cache.Find(key)) { + ++hits; + cache.Touch(key); + } else { + ++misses; + Record record{}; + record.flattened.resize(bytesPerEntry); + const auto before = cache.Size(); + const bool inserted = cache.Insert(key, std::move(record), bytesPerEntry); + skips += !inserted; + evictions += before + (inserted ? 1u : 0u) - cache.Size(); + } + }; + for (uint64_t key = 0; key < hotCount; ++key) access(key); + for (uint64_t round = 0; round < 256; ++round) { + const auto begin = Clock::now(); + for (uint64_t key = 0; key < hotCount; ++key) access(key); + for (uint64_t key = 0; key < coldCount; ++key) access(hotCount + round * coldCount + key); + batches.push_back(std::chrono::duration(Clock::now() - begin).count()); + } + std::sort(batches.begin(), batches.end()); + const auto percentile = [&](size_t percent) { return batches[(batches.size() - 1) * percent / 100]; }; + std::cout << policy << ',' << scenario << ',' << hits << ',' << misses << ',' << evictions << ',' << skips + << ',' << percentile(50) << ',' << percentile(95) << ',' << percentile(99) << '\n'; +} + +void Benchmarks() { + constexpr size_t maxBytes = 8u * 1024u * 1024u; + std::cout << "policy,scenario,hits,misses,evictions,skips,batch_p50_us,batch_p95_us,batch_p99_us\n"; + for (const bool bytePressure : {false, true}) { + const size_t hotCount = bytePressure ? 256 : 4096; + const size_t coldCount = bytePressure ? 64 : 1024; + const size_t bytesPerEntry = bytePressure ? 16384 : 64; + const auto scenario = bytePressure ? "bytes" : "entries"; + Cache cache{8192, maxBytes}; + Benchmark(cache, "incremental", scenario, hotCount, coldCount, bytesPerEntry); + LegacyCache legacy{{}, 0, 8192, maxBytes}; + Benchmark(legacy, "whole-cache", scenario, hotCount, coldCount, bytesPerEntry); + } +} + +} // namespace + +int main(int argc, char** argv) { + try { + if (argc == 2 && std::string_view(argv[1]) == "--benchmark") { + Benchmarks(); + } else { + EntryBudgetAndRecency(); + ByteBudgetAndReplacement(); + RejectedInsertsPreserveRecords(); + EvictionLimitAndProtection(); + BoundedRejectionsMakeProgress(); + ClearAndPointerLifetime(); + LiveBytesStillInvalidateRetainedRecords(); + std::cout << "display-list scan cache tests passed\n"; + } + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/runtime/tests/gx_sync_support/copy_observation.h b/runtime/tests/gx_sync_support/copy_observation.h new file mode 100644 index 000000000..c54d56531 --- /dev/null +++ b/runtime/tests/gx_sync_support/copy_observation.h @@ -0,0 +1,22 @@ +#pragma once + +#include "gfx/recording.hpp" + +#include + +namespace aurora::gfx::testing { +struct CopyObservation { + TextureHandle target; + ClipRect rect; + bool clearColor; + bool clearAlpha; + bool clearDepth; + GXTexFmt format; + ResolveOptions options; + uint32_t draws; +}; + +extern std::vector copyObservations; +void observe_copy(TextureHandle target, ClipRect rect, bool clearColor, bool clearAlpha, bool clearDepth, + GXTexFmt format, const ResolveOptions& options); +} // namespace aurora::gfx::testing diff --git a/runtime/tests/gx_texture_binding_support/runtime_seams.h b/runtime/tests/gx_texture_binding_support/runtime_seams.h new file mode 100644 index 000000000..20a4d5e31 --- /dev/null +++ b/runtime/tests/gx_texture_binding_support/runtime_seams.h @@ -0,0 +1,100 @@ +#pragma once + +#include "../memory_slow_test_support.h" + +#include + +extern "C" const uint8_t kMkwGuestRegion = 0; + +namespace TextureBindingTest { +std::array memory{}; +uint32_t unreadableObject = 0; +uint64_t rangeChecks = 0; +std::vector> retiredRanges; + +uint8_t* Pointer(uint32_t address, size_t length) { + const uint32_t offset = CanonicalizeGxMainRamAddress(address); + if ((unreadableObject != 0 && offset >= unreadableObject && offset < unreadableObject + 32) || + offset > memory.size() || length > memory.size() - offset) { + throw Memory::AccessViolation(address, length, "test unmapped range"); + } + return memory.data() + offset; +} + +struct Command { + std::unique_ptr storage; + void (*run)(void*); +}; +std::deque commands; +} // namespace TextureBindingTest + +Memory::AccessViolation::AccessViolation(uint32_t address, size_t length, std::string_view reason) + : std::runtime_error(std::string(reason)), address_(address), length_(length), reason_(reason) {} + +bool Memory::Contains(uint32_t address, size_t length) { + ++TextureBindingTest::rangeChecks; + try { + TextureBindingTest::Pointer(address, length); + return true; + } catch (const Memory::AccessViolation&) { + return false; + } +} +uint8_t* Memory::GetPointer(uint32_t address) { return TextureBindingTest::Pointer(address, 1); } +uint8_t* Memory::GetPointer(uint32_t address, size_t length) { return TextureBindingTest::Pointer(address, length); } + +uint8_t MemoryInline::Read8Slow(uint32_t address) { return *TextureBindingTest::Pointer(address, 1); } +uint16_t MemoryInline::Read16Slow(uint32_t address) { return BigEndian::Read16(TextureBindingTest::Pointer(address, 2)); } +uint32_t MemoryInline::Read32Slow(uint32_t address) { return BigEndian::Read32(TextureBindingTest::Pointer(address, 4)); } +uint64_t MemoryInline::Read64Slow(uint32_t address) { + return (static_cast(Read32Slow(address)) << 32) | Read32Slow(address + 4); +} +void MemoryInline::Write8Slow(uint32_t address, uint8_t value) { *TextureBindingTest::Pointer(address, 1) = value; } +void MemoryInline::Write16Slow(uint32_t address, uint16_t value) { + BigEndian::Write16(TextureBindingTest::Pointer(address, 2), value); +} +void MemoryInline::Write32Slow(uint32_t address, uint32_t value) { + BigEndian::Write32(TextureBindingTest::Pointer(address, 4), value); +} +void MemoryInline::WriteFloat32Slow(uint32_t address, double value) { + const float narrowed = static_cast(value); + uint32_t bits; + std::memcpy(&bits, &narrowed, sizeof(bits)); + Write32Slow(address, bits); +} + +namespace GxWorker { +namespace detail { +std::atomic g_active{false}; +constinit thread_local bool t_onWorker = false; +constinit thread_local int t_inlineDepth = 0; +bool g_fifoPending = false; + +void* Reserve(size_t size, void (*run)(void*)) { + auto storage = std::make_unique(size); + void* result = storage.get(); + TextureBindingTest::commands.push_back({std::move(storage), run}); + return result; +} +void Commit() {} +void FlushFifo() { g_fifoPending = false; } +} // namespace detail + +void Sync() { + const bool wasWorker = detail::t_onWorker; + detail::t_onWorker = true; + while (!TextureBindingTest::commands.empty()) { + auto command = std::move(TextureBindingTest::commands.front()); + TextureBindingTest::commands.pop_front(); + command.run(command.storage.get()); + } + detail::t_onWorker = wasWorker; +} +void BeginInline() { Sync(); ++detail::t_inlineDepth; } +void EndInline() { --detail::t_inlineDepth; } +} // namespace GxWorker + +void InvalidateEfbCopyDestinationsForRange(uint32_t address, uint32_t size) { + TextureBindingTest::retiredRanges.emplace_back(address, size); +} +extern "C" void GxNotifyDisplayListMemoryWrite(uint32_t, uint32_t) {} diff --git a/runtime/tests/gx_texture_binding_tests.cpp b/runtime/tests/gx_texture_binding_tests.cpp new file mode 100644 index 000000000..1220fdab6 --- /dev/null +++ b/runtime/tests/gx_texture_binding_tests.cpp @@ -0,0 +1,335 @@ +#include "gx_test_common.hpp" +#include "../src/hle/gx/gx_internal.h" +#include "isa/big_endian.h" + +// Keep the production loaders and metadata writers, without a guest dispatch registry. +#undef PPC_NATIVE_OVERRIDE +#undef PPC_NATIVE_OVERRIDE_VOID +#define PPC_NATIVE_OVERRIDE(...) +#define PPC_NATIVE_OVERRIDE_VOID(...) +#include "../src/hle/gx/gx_utils.cpp" +#include "../src/hle/gx/gx_objects.cpp" +#include "../src/hle/gx/gx_texture.cpp" +#include "gx_texture_binding_support/runtime_seams.h" + +namespace { +constexpr uint32_t kObject = 0x80001000; +constexpr uint32_t kOtherObject = kObject + 32; +constexpr uint32_t kPixels = 0x80010000; +constexpr uint32_t kOtherPixels = kPixels + 4096; +constexpr uint32_t kGuestGxData = 0x80003000; + +class RuntimeTextureBindingTest : public GXFifoTest { +protected: + void SetUp() override { + GXFifoTest::SetUp(); + AuroraSetGxProducerThread(); + GxWorker::detail::g_active = false; + GxWorker::detail::t_inlineDepth = 0; + GxWorker::detail::t_onWorker = false; + TextureBindingTest::memory.fill(0); + TextureBindingTest::unreadableObject = 0; + TextureBindingTest::rangeChecks = 0; + TextureBindingTest::retiredRanges.clear(); + InvalidateTexObjLoads(); + // The production table never erases slots; reset its memo before fixture teardown. + g_texObjSlotMemo = nullptr; + g_texObjSlotMemoAddr = 0; + g_texObjIntervals.clear(); + g_texObjIntervalsPending.clear(); + g_texObjIntervalsRebuildAll = true; + g_TexObjMeta.clear(); + g_TlutObjMeta.clear(); + g_HostTlutObjMap.clear(); + g_tlutObjBoundsDirty = true; + Memory::Write32(kGXDataPtrAddr, kGuestGxData); + } + + void TearDown() override { + GxWorker::Sync(); + GxWorker::detail::g_active = false; + g_texObjSlotMemo = nullptr; + g_texObjSlotMemoAddr = 0; + g_texObjIntervals.clear(); + g_texObjIntervalsPending.clear(); + g_TexObjMeta.clear(); + g_HostTlutObjMap.clear(); + GXFifoTest::TearDown(); + } + + void Init(uint32_t object = kObject, uint32_t pixels = kPixels, uint32_t format = GX_TF_RGBA8) { + if (IsPaletteTexFormat(format)) { + GX__InitTexObjCI_80170a04(object, pixels, 8, 8, format, GX_REPEAT, GX_CLAMP, 0, 0); + } else { + GX__InitTexObj_801707f8(object, pixels, 8, 8, format, GX_REPEAT, GX_CLAMP, 0); + } + } + + void Load(uint32_t object = kObject, uint32_t map = 0) { GX__LoadTexObj_80170f2c(object, map); } + + void Warm(uint32_t object = kObject, uint32_t map = 0) { + Load(object, map); + GxWorker::Sync(); + EnsureTexObjIntervalsLocked(); + capture_fifo(); + } + + void ExpectFast(uint32_t object = kObject, uint32_t map = 0) { + const auto checks = TextureBindingTest::rangeChecks; + Load(object, map); + GxWorker::Sync(); + EXPECT_EQ(TextureBindingTest::rangeChecks, checks); + EXPECT_TRUE(g_texObjIntervalsPending.empty()); + } + + void ExpectFull(uint32_t object = kObject, uint32_t map = 0) { + const auto checks = TextureBindingTest::rangeChecks; + Load(object, map); + GxWorker::Sync(); + EXPECT_GT(TextureBindingTest::rangeChecks, checks); + } +}; + +TEST_F(RuntimeTextureBindingTest, RepeatedBindingSkipsMetadataAndExistingHostLoad) { + Init(); + Warm(); + ExpectFast(); + EXPECT_TRUE(capture_fifo().empty()); + EXPECT_EQ(Memory::Read32(kGuestGxData + 0x5FC) & 1u, 1u); + EXPECT_EQ(Memory::Read16(kGuestGxData + 2), 0); +} + +TEST_F(RuntimeTextureBindingTest, AlternatingObjectsAndMapsStillEmitRequiredBindings) { + Init(); + Init(kOtherObject, kOtherPixels); + Warm(); + Warm(kOtherObject, 1); + ExpectFast(kObject, 1); + auto bytes = capture_fifo(); + ASSERT_FALSE(bytes.empty()); + decode_fifo(bytes); + EXPECT_EQ(gxState().loadedTextures[1].data, GuestToHostPtr(kPixels)); + ExpectFast(kOtherObject, 0); + bytes = capture_fifo(); + ASSERT_FALSE(bytes.empty()); + decode_fifo(bytes); + EXPECT_EQ(gxState().loadedTextures[0].data, GuestToHostPtr(kOtherPixels)); +} + +TEST_F(RuntimeTextureBindingTest, BindOnlyAndFullPathsEmitIdenticalPackets) { + for (uint32_t format : {GX_TF_I4, GX_TF_RGBA8, GX_TF_CMPR, GX_TF_Z8, GX_TF_Z16, GX_TF_Z24X8}) { + Init(kObject, kPixels, format); + // Include values the full path has to sanitize before memoizing them. + Memory::Write32(kObject, Memory::Read32(kObject) | 3u | (3u << 19)); + Memory::Write32(kObject + 4, 0xFFFF); + Warm(); + g_boundTexMaps[2] = {}; + ExpectFast(kObject, 2); + const auto fast = capture_fifo(); + ASSERT_FALSE(fast.empty()); + g_boundTexMaps[2] = {}; + g_TexObjMeta.at(kObject).validatedLoad.Invalidate(); + ExpectFull(kObject, 2); + EXPECT_EQ(capture_fifo(), fast) << format; + } +} + +TEST_F(RuntimeTextureBindingTest, EverySnapshotByteParticipatesInValidation) { + Init(); + Warm(); + const auto original = CaptureTexObjBytes(kObject); + for (uint32_t offset = 0; offset < original.size(); ++offset) { + const uint8_t changed = original[offset] ^ 0x80; + Memory::Write8(kObject + offset, changed); + const auto snapshot = CaptureTexObjBytes(kObject); + TexObjSnapshotScope scope(kObject, snapshot); + EXPECT_EQ(TryGetValidatedTexObjSlot(kObject), nullptr) << offset; + Memory::Write8(kObject + offset, original[offset]); + // Re-arm through a real successful load, using the original bytes. + { + TexObjSnapshotScope restored(kObject, original); + LoadTexObjImpl(kObject, 0); + } + } +} + +TEST_F(RuntimeTextureBindingTest, GetterShadowCannotValidateAnotherSnapshot) { + Init(); + Warm(); + auto newer = CaptureTexObjBytes(kObject); + newer[0x1C] ^= 1; + Memory::Write8(kObject + 0x1C, newer[0x1C]); + GX__GetTexObjAll_80170bf8(kObject, 0, 0, 0, 0, 0, 0, 0); + // This getter refreshes the decode shadow without doing a full texture load. + ExpectFull(); + Warm(); + ExpectFast(); +} + +TEST_F(RuntimeTextureBindingTest, ProducerBacklogUsesExecutedBytes) { + Init(); + Warm(); + const auto oldBytes = CaptureTexObjBytes(kObject); + GxWorker::detail::g_active = true; + Load(kObject, 1); + Memory::Write32(kObject + 0x0C, CanonicalizeGxMainRamAddress(kOtherPixels) >> 5); + Load(kObject, 2); + // Producer has already moved on again before either queued load executes. + Memory::Write32(kObject + 0x0C, BigEndian::Read32(oldBytes.data() + 0x0C)); + GxWorker::Sync(); + auto bytes = capture_fifo(); + decode_fifo(bytes); + EXPECT_EQ(gxState().loadedTextures[1].data, GuestToHostPtr(kPixels)); + EXPECT_EQ(gxState().loadedTextures[2].data, GuestToHostPtr(kOtherPixels)); + EXPECT_EQ(g_TexObjMeta.at(kObject).dataAddr, CanonicalizeGxMainRamAddress(kOtherPixels)); + ExpectFull(kObject, 2); + Warm(kObject, 2); + ExpectFast(kObject, 2); +} + +TEST_F(RuntimeTextureBindingTest, MetadataAndHostWritersRequireFullLoad) { + Init(); + Warm(); + GX__InitTexObjLOD_80170a4c(kObject, GX_NEAR, GX_LINEAR, 0, 0, 0.5f, 1, 0, GX_ANISO_2); + ExpectFull(); + Warm(); + GX__InitTexObjWrapMode_80170b50(kObject, GX_MIRROR, GX_REPEAT); + ExpectFull(); + Warm(); + GX__InitTexObjTlut_80170b64(kObject, 3); + ExpectFull(); + Warm(); + GX__InitTexObjFilter_80170b6c(kObject, GX_LINEAR, GX_NEAR); + ExpectFull(); + Warm(); + GX__InitTexObjLODBias_80170b94(kObject, -0.5f); + ExpectFull(); + Warm(); + GX__InitTexObjUserData_80170be8(kObject, kOtherPixels); + ExpectFull(); + Warm(); + // Even an identical descriptor receives a new host texture identity. + Init(); + ExpectFull(); + EXPECT_FALSE(capture_fifo().empty()); + Warm(); + auto& meta = GetTexObjMeta(kObject); + meta.wrapT = GX_MIRROR; + ExpectFull(); +} + +TEST_F(RuntimeTextureBindingTest, PixelDmaBetweenQueuedLoadsCannotBeSkipped) { + Init(); + Warm(); + const auto* host = TryGetHostTexObj(kObject); + const auto oldVersion = reinterpret_cast(host)->texDataVersion; + GxWorker::detail::g_active = true; + Load(kObject, 1); + GxNotifyGuestRamDmaWrite(kPixels, 256); + Load(kObject, 1); + GxWorker::Sync(); + EXPECT_FALSE(g_TexObjMeta.at(kObject).needsUpload); + EXPECT_GT(reinterpret_cast(host)->texDataVersion, oldVersion); + EXPECT_EQ(TextureBindingTest::retiredRanges.size(), 1u); + auto bytes = capture_fifo(); + decode_fifo(bytes); + EXPECT_GT(gxState().loadedTextures[1].texDataVersion, oldVersion); +} + +TEST_F(RuntimeTextureBindingTest, DirtyAliasesAndAddressReuseUploadBeforeRebinding) { + Init(); + Warm(); + const auto oldVersion = reinterpret_cast(TryGetHostTexObj(kObject))->texDataVersion; + MarkTexObjsDirtyForRange(0xC0010000, 256); + EXPECT_TRUE(g_TexObjMeta.at(kObject).needsUpload); + ExpectFull(); + EXPECT_GT(reinterpret_cast(TryGetHostTexObj(kObject))->texDataVersion, oldVersion); + Warm(); + Memory::Write32(kObject + 0x0C, CanonicalizeGxMainRamAddress(kOtherPixels) >> 5); + ExpectFull(); + EXPECT_EQ(g_TexObjMeta.at(kObject).dataAddr, CanonicalizeGxMainRamAddress(kOtherPixels)); + auto bytes = capture_fifo(); + decode_fifo(bytes); + EXPECT_EQ(gxState().loadedTextures[0].data, GuestToHostPtr(kOtherPixels)); +} + +TEST_F(RuntimeTextureBindingTest, PaletteFormatsAlwaysKeepFullPathAcrossTlutChanges) { + for (uint32_t format : {GX_TF_C4, GX_TF_C8, GX_TF_C14X2}) { + Init(kObject, kPixels, format); + Warm(); + ExpectFull(); + GX__InitTlutObj_80170f80(kOtherObject, kOtherPixels, GX_TL_RGB565, 16); + GX__LoadTlut_80170fa8(kOtherObject, 0); + MarkTlutObjsDirtyForRange(kOtherPixels, 32); + GX__LoadTlut_80170fa8(kOtherObject, 0); + ExpectFull(); + } +} + +TEST_F(RuntimeTextureBindingTest, InvalidationAndCopyEvictionPreserveMetadataValidation) { + Init(); + Warm(); + GX__InvalidateTexAll_80171110(); + ExpectFast(); + EXPECT_FALSE(capture_fifo().empty()); + GXDestroyCopyTex(GuestToHostPtr(kPixels)); + ExpectFast(); + EXPECT_FALSE(capture_fifo().empty()); +} + +TEST_F(RuntimeTextureBindingTest, ResetAndHostDestructionCannotReuseCompletedLoad) { + Init(); + Warm(); + InvalidateTexObjLoads(); + ExpectFull(); + EXPECT_FALSE(capture_fifo().empty()); + Warm(); + g_TexObjMeta.at(kObject).host->Destroy(); + ExpectFull(); + EXPECT_TRUE(g_TexObjMeta.at(kObject).host->constructed); +} + +TEST_F(RuntimeTextureBindingTest, UnreadableSnapshotAndBadMapDoNotBecomeValidated) { + Init(); + Warm(); + TextureBindingTest::unreadableObject = CanonicalizeGxMainRamAddress(kObject); + ExpectFull(); + EXPECT_FALSE(g_TexObjMeta.at(kObject).validatedLoad.valid); + TextureBindingTest::unreadableObject = 0; + Warm(); + Load(kObject, 8); + EXPECT_TRUE(capture_fifo().empty()); + ExpectFast(); +} + +TEST_F(RuntimeTextureBindingTest, InvalidGuestDescriptorKeepsPlaceholderFallback) { + WriteGuestTexObj(kObject, kPixels, 8, 8, GX_CTF_R4, GX_CLAMP, GX_CLAMP, false, false, 0); + Load(); + auto bytes = capture_fifo(); + ASSERT_FALSE(bytes.empty()); + EXPECT_EQ(g_boundTexMaps[0].objAddr, 0u); + Init(); + Warm(); + ExpectFast(); +} + +TEST_F(RuntimeTextureBindingTest, DISABLED_BenchmarkRepeatedLoads) { + Init(); + Warm(); + constexpr uint32_t iterations = 250000; + auto& validation = g_TexObjMeta.at(kObject).validatedLoad; + for (bool forceFull : {true, false}) { + const auto checks = TextureBindingTest::rangeChecks; + const auto begin = std::chrono::steady_clock::now(); + for (uint32_t i = 0; i < iterations; ++i) { + if (forceFull) validation.Invalidate(); + Load(); + } + const auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - begin).count(); + std::printf("texture-load full=%d iterations=%u ns=%lld range-checks=%llu\n", forceFull, iterations, + static_cast(elapsed), + static_cast(TextureBindingTest::rangeChecks - checks)); + } +} +} // namespace diff --git a/runtime/tests/gx_worker_tests.cpp b/runtime/tests/gx_worker_tests.cpp new file mode 100644 index 000000000..5f9660238 --- /dev/null +++ b/runtime/tests/gx_worker_tests.cpp @@ -0,0 +1,283 @@ +#define MKW_GX_WORKER_TESTING 1 +#include "../src/hle/gx/gx_worker.cpp" + +#include + +#include +#include +#include + +namespace PerfStats { +bool Enabled() noexcept { return false; } +void AddWait(std::chrono::steady_clock::duration) noexcept {} +} // namespace PerfStats + +namespace { +std::vector> g_fifoReplay; +std::function g_service; +std::function g_beforeRecheck; + +void ServiceWait() { g_service(); } +void BeforeRecheck() { g_beforeRecheck(); } + +template +bool WaitUntil(F&& ready) { + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds{2}; + while (!ready()) { + if (std::chrono::steady_clock::now() >= deadline) { + return false; + } + std::this_thread::yield(); + } + return true; +} + +class Gate { +public: + ~Gate() { Open(); } + void Block() { + std::unique_lock lock{m_mutex}; + m_entered = true; + m_cv.notify_all(); + m_cv.wait(lock, [&] { return m_open; }); + } + bool WaitEntered() { + std::unique_lock lock{m_mutex}; + return m_cv.wait_for(lock, std::chrono::seconds{2}, [&] { return m_entered; }); + } + void Open() { + std::lock_guard lock{m_mutex}; + m_open = true; + m_cv.notify_all(); + } + +private: + std::mutex m_mutex; + std::condition_variable m_cv; + bool m_entered = false; + bool m_open = false; +}; + +class GxWorkerTest : public ::testing::Test { +protected: + void SetUp() override { + GxWorker::g_testEnabled = true; + GxWorker::g_testWakeEachRecord = false; + GxWorker::g_testNotifications = 0; + GxWorker::g_testWaits = 0; + g_fifoReplay.clear(); + GxWorker::Start(); + } + void TearDown() override { + GxWorker::g_testBeforeTailRecheck = nullptr; + GxWorker::SetWaitCallback(nullptr); + GxWorker::Stop(); + g_service = {}; + g_beforeRecheck = {}; + } +}; + +TEST_F(GxWorkerTest, DoesNotNotifyBeforeTheRequestedTail) { + Gate first; + Gate last; + uint32_t completed = 0; + GxWorker::Submit([&] { first.Block(); }); + for (uint32_t i = 0; i < 512; ++i) { + GxWorker::Submit([&, i] { EXPECT_EQ(completed++, i); }); + } + GxWorker::Submit([&] { last.Block(); }); + std::thread release{[&] { + EXPECT_TRUE(WaitUntil([] { return GxWorker::g_waitTarget.load() != GxWorker::kNotWaiting; })); + first.Open(); + EXPECT_TRUE(last.WaitEntered()); + EXPECT_EQ(GxWorker::g_testNotifications.load(), 0u); + last.Open(); + }}; + GxWorker::Sync(); + release.join(); + EXPECT_EQ(completed, 512u); + EXPECT_GE(GxWorker::g_testWaits.load(), 1u); + EXPECT_EQ(GxWorker::g_waitTarget.load(), GxWorker::kNotWaiting); +} + +TEST_F(GxWorkerTest, RechecksCompletionAfterPublishingTheWaitTarget) { + Gate gate; + GxWorker::Submit([&] { gate.Block(); }); + g_beforeRecheck = [&] { + GxWorker::g_testBeforeTailRecheck = nullptr; + gate.Open(); + EXPECT_TRUE(WaitUntil([] { return GxWorker::g_tail.load() >= GxWorker::g_waitTarget.load(); })); + }; + GxWorker::g_testBeforeTailRecheck = BeforeRecheck; + GxWorker::Sync(); + EXPECT_EQ(GxWorker::g_testWaits.load(), 0u); + EXPECT_EQ(GxWorker::g_waitTarget.load(), GxWorker::kNotWaiting); +} + +TEST_F(GxWorkerTest, ServicesCallbacksAndDrainsTheirNestedSyncAndNewWork) { + Gate gate; + uint32_t callbacks = 0; + uint32_t completed = 0; + GxWorker::Submit([&] { gate.Block(); ++completed; }); + g_service = [&] { + if (++callbacks == 1) { + GxWorker::Submit([&] { ++completed; }); + gate.Open(); + GxWorker::Sync(); + } + }; + GxWorker::SetWaitCallback(ServiceWait); + GxWorker::Sync(); + EXPECT_GE(callbacks, 1u); + EXPECT_EQ(completed, 2u); + EXPECT_EQ(GxWorker::g_waitTarget.load(), GxWorker::kNotWaiting); +} + +TEST_F(GxWorkerTest, RingBackpressureAndWrapPreserveOrderWithoutServicingGuestCallbacks) { + Gate gate; + uint32_t callbacks = 0; + uint32_t completed = 0; + GxWorker::Submit([&] { gate.Block(); }); + std::thread release{[&] { + EXPECT_TRUE(WaitUntil([] { return GxWorker::g_waitTarget.load() != GxWorker::kNotWaiting; })); + gate.Open(); + }}; + g_service = [&] { ++callbacks; }; + GxWorker::SetWaitCallback(ServiceWait); + for (uint32_t i = 0; i < 160; ++i) { + std::array bytes{}; + bytes.front() = static_cast(i); + bytes.back() = static_cast(i ^ 0xff); + GxWorker::Submit([&, bytes, i] { + EXPECT_EQ(completed++, i); + EXPECT_EQ(bytes.front(), static_cast(i)); + EXPECT_EQ(bytes.back(), static_cast(i ^ 0xff)); + }); + } + release.join(); + EXPECT_EQ(callbacks, 0u); + GxWorker::SetWaitCallback(nullptr); + GxWorker::Sync(); + EXPECT_EQ(completed, 160u); +} + +TEST_F(GxWorkerTest, FifoFlushInlineAndRestartRetainTheirBoundaries) { + GxWorker::FifoWrite(0x61, 1); + GxWorker::FifoWrite(0x12345678, 4); + const std::array burst{1, 2, 3}; + GxWorker::FifoBurst(burst.data(), burst.size()); + GxWorker::Submit([] { EXPECT_EQ(g_fifoReplay.size(), 5u); }); + { + GxWorker::InlineScope scope; + EXPECT_FALSE(GxWorker::ShouldDefer()); + EXPECT_EQ(g_fifoReplay, (std::vector>{ + {0x61, 1}, {0x12345678, 4}, {1, 1}, {2, 1}, {3, 1}})); + } + EXPECT_TRUE(GxWorker::ShouldDefer()); + auto lifetime = std::make_shared(0); + std::weak_ptr weak = lifetime; + GxWorker::Submit([owned = std::move(lifetime)] {}); + GxWorker::Stop(); + EXPECT_TRUE(weak.expired()); + GxWorker::g_testEnabled = false; + GxWorker::Start(); + EXPECT_FALSE(GxWorker::Active()); + bool ran = false; + GxWorker::Run([&] { ran = true; }); + EXPECT_TRUE(ran); + GxWorker::g_testEnabled = true; + GxWorker::Start(); + GxWorker::Submit([&] { ran = false; }); + GxWorker::Sync(); + EXPECT_FALSE(ran); +} + +TEST_F(GxWorkerTest, ThrowingCommandsStillCompleteTheirRecordAndReleaseTheirPayload) { + auto lifetime = std::make_shared(0); + std::weak_ptr weak = lifetime; + bool completed = false; + GxWorker::Submit([owned = std::move(lifetime)] { throw std::runtime_error("injected failure"); }); + GxWorker::Submit([&] { completed = true; }); + GxWorker::Sync(); + EXPECT_TRUE(completed); + EXPECT_TRUE(weak.expired()); +} + +#if GTEST_HAS_DEATH_TEST +TEST(GxWorkerDeathTest, AbandonWakesTheIdleConsumerAndSignalsExit) { + ASSERT_EXIT({ + GxWorker::Start(); + GxWorker::Abandon(); + std::_Exit(!GxWorker::Active() && GxWorker::g_thread == nullptr && GxWorker::g_exited.load() ? 0 : 1); + }, ::testing::ExitedWithCode(0), ""); +} + +TEST(GxWorkerDeathTest, AbandonDoesNotExecuteTheFollowingRecord) { + ASSERT_EXIT({ + Gate gate; + std::atomic ran{false}; + GxWorker::Start(); + GxWorker::Submit([&] { gate.Block(); }); + GxWorker::Submit([&] { ran = true; }); + GxWorker::Publish(); + if (!gate.WaitEntered()) { + std::_Exit(2); + } + std::thread release{[&] { + WaitUntil([] { return GxWorker::g_abandon.load(); }); + gate.Open(); + }}; + GxWorker::Abandon(); + release.join(); + std::_Exit(!ran.load() && GxWorker::g_exited.load() ? 0 : 1); + }, ::testing::ExitedWithCode(0), ""); +} +#endif + +TEST_F(GxWorkerTest, DISABLED_WakeBenchmark) { + std::array, 2> times; + std::array notifications{}; + std::array waits{}; + for (uint32_t batch = 0; batch < 40; ++batch) { + for (uint32_t order = 0; order < 2; ++order) { + const uint32_t mode = (batch + order) % 2; + GxWorker::g_testWakeEachRecord = mode != 0; + GxWorker::g_testNotifications = 0; + GxWorker::g_testWaits = 0; + uint32_t completed = 0; + Gate gate; + const auto start = std::chrono::steady_clock::now(); + GxWorker::Submit([&] { gate.Block(); }); + for (uint32_t i = 0; i < 20000; ++i) { + GxWorker::Submit([&] { ++completed; }); + } + std::thread release{[&] { + EXPECT_TRUE(WaitUntil([] { return GxWorker::g_waitTarget.load() != GxWorker::kNotWaiting; })); + gate.Open(); + }}; + GxWorker::Sync(); + release.join(); + times[mode].push_back(std::chrono::duration_cast( + std::chrono::steady_clock::now() - start).count()); + notifications[mode] += GxWorker::g_testNotifications.load(); + waits[mode] += GxWorker::g_testWaits.load(); + EXPECT_EQ(completed, 20000u); + } + } + for (uint32_t mode = 0; mode < 2; ++mode) { + std::sort(times[mode].begin(), times[mode].end()); + std::printf("wake_each=%u records=800000 notifications=%llu waits=%llu median_us=%lld p95_us=%lld\n", mode, + static_cast(notifications[mode]), static_cast(waits[mode]), + static_cast((times[mode][19] + times[mode][20]) / 2), + static_cast(times[mode][37])); + } + GxWorker::g_testWakeEachRecord = false; +} +} // namespace + +void GxWorkerReplayFifoWrite(uint32_t value, uint32_t sizeBytes) { g_fifoReplay.emplace_back(value, sizeBytes); } +void GxWorkerReplayFifoBurst(const uint8_t* data, uint32_t sizeBytes) { + for (uint32_t i = 0; i < sizeBytes; ++i) { + GxWorkerReplayFifoWrite(data[i], 1); + } +} diff --git a/runtime/tests/runtime_config_tests.cpp b/runtime/tests/runtime_config_tests.cpp index 32e42f679..98f8fadcd 100644 --- a/runtime/tests/runtime_config_tests.cpp +++ b/runtime/tests/runtime_config_tests.cpp @@ -34,6 +34,26 @@ bool SetsOffset(const std::string& original, const char* message) { } // namespace int main() { + for (const std::string mode : {"specialized", "hybrid"}) { + std::istringstream input("[video]\nshader_mode = '" + mode + "'\n"); + const auto config = RuntimeConfigFile::ParseConfig(input, "shader.toml"); + if (!Require(config.shaderMode == mode, "valid shader mode was not loaded")) return 1; + } + for (const std::string text : {"", "video = 4\n", "[[video]]\nshader_mode = 'hybrid'\n", + "[video]\nshader_mode = true\n", "[video]\nshader_mode = 1\n", + "[video]\nshader_mode = []\n", "[video]\nshader_mode = {}\n", + "[video]\nshader_mode = 'force_generic'\n", "[video]\nshader_mode = 'HYBRID'\n", + "[video]\nshader_mode = 'hybrid'\nshader_mode = 'specialized'\n", + "[video]\nshader_mode = 'hybrid'\n[video]\n"}) { + std::istringstream input(text); + const auto config = RuntimeConfigFile::ParseConfig(input, "invalid-shader.toml"); + if (!Require(config.shaderMode.value_or("specialized") == "specialized", "invalid shader mode did not default")) return 1; + } + { + const auto doc = Update("[video]\nshader_mode = 'specialized'\n", "video", "shader_mode", "'hybrid'"); + if (!Require(doc && doc->at("video").at("shader_mode").as_string() == "hybrid", "next-start shader mode was not saved")) return 1; + } + { std::istringstream input("[video]\nmetalfx_spatial_upscaling = true\n"); const RuntimeUserConfig config = RuntimeConfigFile::ParseConfig(input, "enabled.toml"); diff --git a/translator/src/Translator.Cli/Program.cs b/translator/src/Translator.Cli/Program.cs index e1164dd2a..064db8bca 100644 --- a/translator/src/Translator.Cli/Program.cs +++ b/translator/src/Translator.Cli/Program.cs @@ -461,6 +461,7 @@ int RunTranslateRecursive(string[] argsTail) var outputMetadataPath = OptionValue(argsTail, "--output-metadata"); var productionSourceBundlePath = OptionValue(argsTail, "--production-source-bundle"); var emitFunctionFiles = !HasFlag(argsTail, "--no-function-files"); + var boundedIr = HasFlag(argsTail, "--bounded-ir"); if (!emitFunctionFiles && string.IsNullOrWhiteSpace(productionSourceBundlePath)) throw new ArgumentException("--no-function-files requires --production-source-bundle."); BaseTranslationOutputMetadata? previousOutputMetadata = null; @@ -954,14 +955,17 @@ void LogPhase(string label) var emissionAddresses = baseSummaries.Keys.OrderBy(address => address).ToArray(); var localGuestAbiContracts = new Dictionary(emissionAddresses.Length); - var canonicalGuestFunctions = new Dictionary(emissionAddresses.Length); + var bufferedGuestFunctions = boundedIr ? null : new Dictionary(emissionAddresses.Length); + IReadOnlyDictionary canonicalGuestFunctions = bufferedGuestFunctions is not null + ? bufferedGuestFunctions + : new CanonicalIrDictionary(canonicalIrStore, emissionAddresses); var irInstructionCounts = new Dictionary(emissionAddresses.Length); // Each slot is an independent pure decode plus contract analysis, so // compute them in parallel into position-indexed arrays and fill the // dictionaries afterwards in the original ascending order. Dictionary // insertion order is preserved, so every downstream enumeration of // these maps is byte-for-byte the same as the serial version. - var canonicalByIndex = new IrFunction[emissionAddresses.Length]; + var canonicalByIndex = boundedIr ? null : new IrFunction[emissionAddresses.Length]; var localContractByIndex = new GuestAbiContract[emissionAddresses.Length]; var irInstructionCountByIndex = new int[emissionAddresses.Length]; IndexedParallel.For(emissionAddresses.Length, parallelOptions, i => @@ -969,14 +973,14 @@ void LogPhase(string label) var address = emissionAddresses[i]; if (!canonicalIrStore.TryGet(address, out var canonicalSsa)) throw new InvalidOperationException($"Missing canonical SSA while building ABI contract for 0x{address:X8}."); - canonicalByIndex[i] = canonicalSsa; + if (canonicalByIndex is not null) canonicalByIndex[i] = canonicalSsa; localContractByIndex[i] = GuestAbiContractAnalyzer.Analyze(canonicalSsa); irInstructionCountByIndex[i] = canonicalSsa.Blocks.Sum(block => block.Instructions.Count); }); for (var i = 0; i < emissionAddresses.Length; i++) { var address = emissionAddresses[i]; - canonicalGuestFunctions[address] = canonicalByIndex[i]; + if (bufferedGuestFunctions is not null) bufferedGuestFunctions[address] = canonicalByIndex![i]; localGuestAbiContracts[address] = localContractByIndex[i]; irInstructionCounts[address] = irInstructionCountByIndex[i]; } @@ -988,7 +992,9 @@ void LogPhase(string label) // rather than retaining every descendant dependency forever. var interproceduralGuestAbi = GuestAbiInterproceduralAnalyzer.Analyze( canonicalGuestFunctions, - nativeGuestEffects.Contracts); + nativeGuestEffects.Contracts, + retainBodies: !boundedIr, + maxDegreeOfParallelism: boundedIr ? maxThreads : null); var guestAbiContracts = interproceduralGuestAbi.Contracts .ToDictionary(static pair => pair.Key, static pair => pair.Value); var recursiveComponents = interproceduralGuestAbi.StronglyConnectedComponents @@ -3697,6 +3703,7 @@ static string[] KnownCommands() => new[] new("--production-source-bundle", "path"), new("--threads", "N"), new("--no-function-files"), + new("--bounded-ir"), new("--prune-stale") }), "translate-mod" => (null, new CommandOption[] diff --git a/translator/src/Translator.Core/Analysis/GuestAbiInterproceduralAnalyzer.cs b/translator/src/Translator.Core/Analysis/GuestAbiInterproceduralAnalyzer.cs index f6817d3a1..a5374ac88 100644 --- a/translator/src/Translator.Core/Analysis/GuestAbiInterproceduralAnalyzer.cs +++ b/translator/src/Translator.Core/Analysis/GuestAbiInterproceduralAnalyzer.cs @@ -18,7 +18,9 @@ public static class GuestAbiInterproceduralAnalyzer { public static GuestAbiInterproceduralResult Analyze( IReadOnlyDictionary functions, - IReadOnlyDictionary? externalContracts = null) + IReadOnlyDictionary? externalContracts = null, + bool retainBodies = true, + int? maxDegreeOfParallelism = null) { externalContracts ??= new Dictionary(); var addresses = functions.Keys.OrderBy(static address => address).ToArray(); @@ -26,12 +28,18 @@ public static GuestAbiInterproceduralResult Analyze( // GuestAbiContractAnalyzer.Analyze is pure and the round's contract snapshot is frozen, so // per-function analyses within a round can run on any thread. Determinism comes from // applying results serially in ascending-ordinal order, matching the old single-threaded loop. - var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; + var parallelOptions = new ParallelOptions + { + MaxDegreeOfParallelism = maxDegreeOfParallelism ?? Environment.ProcessorCount + }; - // Resolving the IR by ordinal once keeps the caller-supplied dictionary - // off every parallel path: only this loop and BuildComponents touch it. - var bodies = new IrFunction[addresses.Length]; - for (var index = 0; index < addresses.Length; ++index) bodies[index] = functions[addresses[index]]; + // Lazy callers must supply stable bodies and support concurrent reads. + var bodies = retainBodies ? new IrFunction[addresses.Length] : null; + if (bodies is not null) + { + for (var index = 0; index < addresses.Length; ++index) bodies[index] = functions[addresses[index]]; + } + IrFunction Body(int index) => bodies is not null ? bodies[index] : functions[addresses[index]]; // Mirror of `contracts` keyed by ordinal. The fixpoint compares against // it from the parallel phase, where the dictionary must stay untouched. @@ -40,7 +48,7 @@ public static GuestAbiInterproceduralResult Analyze( 0, addresses.Length, parallelOptions, - index => current[index] = GuestAbiContractAnalyzer.Analyze(bodies[index])); + index => current[index] = GuestAbiContractAnalyzer.Analyze(Body(index))); var contracts = new Dictionary(addresses.Length); for (var index = 0; index < addresses.Length; ++index) contracts.Add(addresses[index], current[index]); @@ -62,7 +70,7 @@ public static GuestAbiInterproceduralResult Analyze( (index, _, scratch) => { scratch.Clear(); - CollectDirectCallees(bodies[index], ordinals, scratch); + CollectDirectCallees(Body(index), ordinals, scratch); directCallees[index] = scratch.ToArray(); return scratch; }, @@ -95,7 +103,7 @@ public static GuestAbiInterproceduralResult Analyze( Parallel.For(0, worklistCount, parallelOptions, position => { var ordinal = worklist[position]; - var next = GuestAbiContractAnalyzer.Analyze(bodies[ordinal], visible); + var next = GuestAbiContractAnalyzer.Analyze(Body(ordinal), visible); roundResults[position] = Equivalent(current[ordinal], next) ? null : next; }); diff --git a/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs b/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs new file mode 100644 index 000000000..b707683e8 --- /dev/null +++ b/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs @@ -0,0 +1,58 @@ +using System.Collections; +using Translator.Core.Ir; + +namespace Translator.Core.Translation; + +/// +/// Fixed keys over canonical SSA decoded on demand without caching bodies. +/// Concurrent readers require a thread-safe provider, such as CanonicalIrStore. +/// +public sealed class CanonicalIrDictionary : IReadOnlyDictionary +{ + private readonly ICanonicalIrProvider _provider; + private readonly IReadOnlyList _addresses; + private readonly HashSet _membership; + + public CanonicalIrDictionary(ICanonicalIrProvider provider, IEnumerable addresses) + { + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(addresses); + _provider = provider; + var snapshot = addresses.ToArray(); + _membership = new HashSet(snapshot); + if (_membership.Count != snapshot.Length) + throw new ArgumentException("Canonical IR addresses must be unique.", nameof(addresses)); + _addresses = Array.AsReadOnly(snapshot); + } + + public int Count => _addresses.Count; + public IEnumerable Keys => _addresses; + public IEnumerable Values + { + get + { + foreach (var address in _addresses) yield return this[address]; + } + } + + public IrFunction this[uint key] => TryGetValue(key, out var value) + ? value + : throw new KeyNotFoundException($"Missing canonical SSA for 0x{key:X8}."); + + public bool ContainsKey(uint key) => _membership.Contains(key); + + public bool TryGetValue(uint key, out IrFunction value) + { + if (_membership.Contains(key) && _provider.TryGet(key, out value)) return true; + value = null!; + return false; + } + + public IEnumerator> GetEnumerator() + { + foreach (var address in _addresses) + yield return new KeyValuePair(address, this[address]); + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); +} diff --git a/translator/tests/Translator.Tests/CanonicalIrDictionaryTests.cs b/translator/tests/Translator.Tests/CanonicalIrDictionaryTests.cs new file mode 100644 index 000000000..7acc5bb5b --- /dev/null +++ b/translator/tests/Translator.Tests/CanonicalIrDictionaryTests.cs @@ -0,0 +1,285 @@ +using System.Buffers.Binary; +using System.Collections; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Translator.Core.Analysis; +using Translator.Core.Ir; +using Translator.Core.Loading; +using Translator.Core.Translation; +using Xunit; + +namespace Translator.Tests; + +public sealed class CanonicalIrDictionaryTests +{ + [Fact] + public void KeysAndMembershipUseAnIndependentSnapshotWithoutDecoding() + { + var store = new CanonicalIrStore(); + store.Put(20, Function("second", new IrReturn(null))); + store.Put(10, Function("first", new IrReturn(null))); + store.Put(30, Function("outside", new IrReturn(null))); + var provider = new CountingProvider(store); + var addresses = new uint[] { 20, 10 }; + var view = new CanonicalIrDictionary(provider, addresses); + addresses[0] = 0; + + Assert.Equal(2, view.Count); + Assert.Equal(new uint[] { 20, 10 }, view.Keys); + Assert.True(view.ContainsKey(10)); + Assert.False(view.ContainsKey(0)); + Assert.False(view.ContainsKey(30)); + Assert.False(view.TryGetValue(30, out var missing)); + Assert.Null(missing); + Assert.Throws(() => view[30]); + Assert.Equal(0, provider.Reads); + + var first = view[10]; + Assert.NotSame(first, view[10]); + Assert.Equal(2, provider.Reads); + Assert.Equal("second", Assert.Single(view.Values.Take(1)).Name); + Assert.Equal(3, provider.Reads); + using var entries = view.GetEnumerator(); + Assert.Equal(3, provider.Reads); + Assert.True(entries.MoveNext()); + Assert.Equal(20u, entries.Current.Key); + Assert.Equal(4, provider.Reads); + Assert.Equal(view.Keys, view.Select(static pair => pair.Key)); + Assert.Equal(new[] { "second", "first" }, view.Values.Select(static body => body.Name)); + Assert.Equal(view.Keys, ((IEnumerable)view).Cast>().Select(static pair => pair.Key)); + } + + [Fact] + public void MissingAndReplacedBodiesDoNotChangeMembershipOrLeaveCachedValues() + { + var store = new CanonicalIrStore(); + store.Put(10, Function("original", new IrReturn(null))); + var view = new CanonicalIrDictionary(store, new uint[] { 10 }); + Assert.Equal("original", view[10].Name); + store.Put(10, Function("replacement", new IrReturn(null))); + Assert.Equal("replacement", view[10].Name); + store.Remove(10); + + Assert.Single(view.Keys); + Assert.True(view.ContainsKey(10)); + Assert.Equal(new uint[] { 10 }, view.Keys); + Assert.False(view.TryGetValue(10, out var missing)); + Assert.Null(missing); + Assert.Throws(() => view[10]); + Assert.Throws(() => view.Values.ToArray()); + Assert.Throws(() => view.ToArray()); + } + + [Fact] + public void ConstructorRejectsNullInputsAndDuplicateKeys() + { + var store = new CanonicalIrStore(); + Assert.Throws(() => new CanonicalIrDictionary(null!, Array.Empty())); + Assert.Throws(() => new CanonicalIrDictionary(store, null!)); + Assert.Throws(() => new CanonicalIrDictionary(store, new uint[] { 10, 20, 10 })); + var empty = new CanonicalIrDictionary(store, Array.Empty()); + Assert.Empty(empty); + Assert.Empty(empty.Keys); + Assert.Empty(empty.Values); + } + + [Fact] + public void ConcurrentReadersEnumerateInSnapshotOrderAndDecodeIndependentBodies() + { + var store = new CanonicalIrStore(); + var addresses = Enumerable.Range(1, 16).Reverse().Select(static index => (uint)index).ToArray(); + foreach (var address in addresses) store.Put(address, Function($"f{address}", new IrReturn(null))); + var provider = new CountingProvider(store); + var view = new CanonicalIrDictionary(provider, addresses); + var bodies = new IrFunction[32]; + + Parallel.For(0, bodies.Length, new ParallelOptions { MaxDegreeOfParallelism = 4 }, index => + { + Assert.Equal(addresses, view.Keys); + Assert.All(addresses, address => Assert.True(view.ContainsKey(address))); + Assert.False(view.TryGetValue(0, out _)); + Assert.Equal(addresses, view.Select(static pair => pair.Key)); + Assert.Equal(addresses.Select(static address => $"f{address}"), view.Values.Select(static body => body.Name)); + bodies[index] = view[1]; + }); + + Assert.Equal(bodies.Length * (2 * addresses.Length + 1), provider.Reads); + for (var index = 1; index < bodies.Length; ++index) Assert.NotSame(bodies[0], bodies[index]); + } + + [Fact] + public void LazyCanonicalBodyPreservesSerializedIrAndLoweredSource() + { + var memory = new byte[8]; + BinaryPrimitives.WriteUInt32BigEndian(memory.AsSpan(0, 4), 0x38630001u); // addi r3,r3,1 + BinaryPrimitives.WriteUInt32BigEndian(memory.AsSpan(4, 4), 0x4E800020u); // blr + var image = new ProgramImage(memory, + AddressRange.FromStartAndSize(MemoryLayout.RamBase, (uint)memory.Length), + AddressRange.FromStartAndSize(MemoryLayout.RamBase, (uint)memory.Length), default, "bounded-ir-test"); + var translator = new FunctionTranslator(image); + var options = TranslationOptions.Default with { PreferredName = "bounded_ir_test" }; + var canonical = translator.Discover(MemoryLayout.RamBase, options).Ssa.Function; + var store = new CanonicalIrStore(); + store.Put(MemoryLayout.RamBase, canonical); + var view = new CanonicalIrDictionary(store, new[] { MemoryLayout.RamBase }); + var buffered = translator.LowerCanonical(MemoryLayout.RamBase, canonical, options); + var lazy = translator.LowerCanonical(MemoryLayout.RamBase, view[MemoryLayout.RamBase], options); + + Assert.Equal(buffered.CxxCode, lazy.CxxCode); + Assert.Equal(JsonSerializer.Serialize(canonical), JsonSerializer.Serialize(view[MemoryLayout.RamBase])); + } + + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(-1)] + public void LazyAnalysisMatchesBufferedContractsAndComponents(int parallelism) + { + var functions = new Dictionary + { + [0x80003000] = Function("leaf", + new IrAssign("r7", IrValue.Register("r6")), + new IrCall("lr", "0x80004000", Array.Empty()), new IrReturn(null)), + [0x80001000] = Function("first", + new IrAssign("r5", IrValue.Register("r3")), + new IrCall("lr", "0x80002000", Array.Empty()), new IrReturn(null)), + [0x80002000] = Function("second", + new IrAssign("r6", IrValue.Register("r4")), + new IrCall("lr", "0x80001000", Array.Empty()), + new IrCall("lr", "0x80003000", Array.Empty()), new IrReturn(null)), + [0x80005000] = Function("self", + new IrCall("lr", "0x80005000", Array.Empty()), new IrReturn(null)), + [0x80006000] = Function("unknown", + new IrCall("lr", "0x80007000", Array.Empty()), + new IrIndirectCall("lr", IrValue.Register("ctr"), Array.Empty()), new IrReturn(null)) + }; + var external = new Dictionary + { + [0x80004000] = GuestAbiContractAnalyzer.Analyze(Function("external", + new IrAssign("r8", IrValue.Register("r9")), new IrReturn(null))) + }; + var store = new CanonicalIrStore(); + foreach (var pair in functions) store.Put(pair.Key, pair.Value); + var view = new CanonicalIrDictionary(store, functions.Keys.Reverse()); + var buffered = GuestAbiInterproceduralAnalyzer.Analyze(functions, external); + var lazy = GuestAbiInterproceduralAnalyzer.Analyze(view, external, + retainBodies: false, maxDegreeOfParallelism: parallelism); + + Assert.Equal(JsonSerializer.Serialize(buffered), JsonSerializer.Serialize(lazy)); + Assert.Equal(functions.Keys.OrderBy(static address => address), lazy.Contracts.Keys); + Assert.Contains(lazy.StronglyConnectedComponents, + component => component.SequenceEqual(new uint[] { 0x80001000, 0x80002000 })); + Assert.Contains(lazy.StronglyConnectedComponents, + component => component.SequenceEqual(new uint[] { 0x80005000 })); + Assert.True(lazy.Contracts[0x80006000].HasFullSynchronizationFence); + Assert.NotEqual(0u, lazy.Contracts[0x80003000].GprReadBeforeWriteMask & (1u << 9)); + } + + [Fact] + public void DefaultAnalysisResolvesEachBodyOnceAndLazyAnalysisResolvesEachPass() + { + var store = new CanonicalIrStore(); + store.Put(10, Function("leaf", new IrReturn(null))); + var provider = new CountingProvider(store); + var view = new CanonicalIrDictionary(provider, new uint[] { 10 }); + + var buffered = GuestAbiInterproceduralAnalyzer.Analyze(view); + Assert.Equal(1, provider.Reads); + var lazy = GuestAbiInterproceduralAnalyzer.Analyze(view, retainBodies: false, maxDegreeOfParallelism: 1); + Assert.Equal(4, provider.Reads); + Assert.Equal(JsonSerializer.Serialize(buffered), JsonSerializer.Serialize(lazy)); + } + + [Fact] + public void LazyAnalysisRevisitsCallersOfInlineThunksAfterTheirCalleesChange() + { + var range = GuestSaveRestoreThunks.Current.SaveGpr; + Assert.NotNull(range); + var thunk = range!.BaseAddress; + var functions = new Dictionary + { + [0x80001000] = Function("caller", + new IrCall("lr", $"0x{thunk:X8}", Array.Empty()), new IrReturn(null)), + [thunk] = Function("thunk", + new IrCall("lr", "0x80002000", Array.Empty()), new IrReturn(null)), + [0x80002000] = Function("middle", + new IrCall("lr", "0x80003000", Array.Empty()), new IrReturn(null)), + [0x80003000] = Function("leaf", + new IrAssign("r8", IrValue.Register("r9")), new IrReturn(null)) + }; + var store = new CanonicalIrStore(); + foreach (var pair in functions) store.Put(pair.Key, pair.Value); + var buffered = GuestAbiInterproceduralAnalyzer.Analyze(functions); + var lazy = GuestAbiInterproceduralAnalyzer.Analyze(new CanonicalIrDictionary(store, functions.Keys), + retainBodies: false, maxDegreeOfParallelism: 2); + + Assert.Equal(JsonSerializer.Serialize(buffered), JsonSerializer.Serialize(lazy)); + Assert.DoesNotContain(thunk, lazy.Contracts[0x80001000].DirectCallTargets); + Assert.Equal(1u << 9, lazy.Contracts[0x80001000].GprReadBeforeWriteMask); + Assert.NotEqual(0u, lazy.Contracts[0x80001000].GprPossibleWriteMask & (1u << 8)); + } + + [Theory] + [InlineData(true, 0)] + [InlineData(false, 0)] + [InlineData(true, -2)] + [InlineData(false, -2)] + public void InvalidParallelismFailsBeforeDecoding(bool retainBodies, int parallelism) + { + var provider = new CountingProvider(new CanonicalIrStore()); + var view = new CanonicalIrDictionary(provider, new uint[] { 10 }); + Assert.Throws(() => GuestAbiInterproceduralAnalyzer.Analyze( + view, retainBodies: retainBodies, maxDegreeOfParallelism: parallelism)); + Assert.Equal(0, provider.Reads); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void EmptyAnalysisPreservesEmptyResults(bool retainBodies) + { + var view = new CanonicalIrDictionary(new CanonicalIrStore(), Array.Empty()); + var result = GuestAbiInterproceduralAnalyzer.Analyze(view, retainBodies: retainBodies, maxDegreeOfParallelism: 1); + Assert.Empty(result.Contracts); + Assert.Empty(result.StronglyConnectedComponents); + } + + [Fact] + public void SingleWorkerLimitAppliesToLazyProviderReadsInEveryAnalysisPass() + { + var store = new CanonicalIrStore(); + var addresses = Enumerable.Range(1, 32).Select(static index => (uint)index).ToArray(); + foreach (var address in addresses) store.Put(address, Function($"f{address}", new IrReturn(null))); + var provider = new CountingProvider(store, requireSerial: true); + var result = GuestAbiInterproceduralAnalyzer.Analyze(new CanonicalIrDictionary(provider, addresses), + retainBodies: false, maxDegreeOfParallelism: 1); + Assert.Equal(addresses.Length, result.Contracts.Count); + Assert.Equal(3 * addresses.Length, provider.Reads); + } + + private static IrFunction Function(string name, params IrInstruction[] instructions) => + new(name, "entry", new[] { new IrBasicBlock("entry", instructions) }); + + private sealed class CountingProvider(ICanonicalIrProvider inner, bool requireSerial = false) : ICanonicalIrProvider + { + private int _reads; + private int _active; + public int Reads => Volatile.Read(ref _reads); + + public bool TryGet(uint entryPoint, out IrFunction function) + { + var active = Interlocked.Increment(ref _active); + try + { + if (requireSerial) Assert.Equal(1, active); + Interlocked.Increment(ref _reads); + return inner.TryGet(entryPoint, out function); + } + finally + { + Interlocked.Decrement(ref _active); + } + } + } +} diff --git a/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs b/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs index 7f9254f25..982495af5 100644 --- a/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs +++ b/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs @@ -8,6 +8,91 @@ namespace Translator.Tests; public sealed class TranslateRecursiveRuntimeConfigTests { + [Fact] + public void BoundedIrMatchesBufferedFilesMetadataAndBundleAcrossThreadCounts() + { + var root = Path.Combine(Path.GetTempPath(), $"translator-bounded-ir-{Guid.NewGuid():N}"); + Directory.CreateDirectory(root); + try + { + const uint entry = 0x80001000u; + File.WriteAllBytes(Path.Combine(root, "main.dol"), SyntheticDolFactory.CreateBytes(entry, + sections: + [ + SyntheticDolFactory.Text(0, entry, + 0x38A30001u, // addi r5,r3,1 + 0x4800001Du, // bl 0x80001020 + 0x38650001u, // addi r3,r5,1 + 0x4E800020u), + SyntheticDolFactory.Text(1, entry + 0x20, + 0x38C40001u, // addi r6,r4,1 + 0x4BFFFFDDu, // bl 0x80001000 + 0x38660001u, // addi r3,r6,1 + 0x4E800020u) + ])); + var projectPath = Path.Combine(root, "recomp.yml"); + File.WriteAllText(projectPath, + """ + schema_version: 1 + project: + id: bounded-ir-test + memory: + sda_base: 0x80002000 + sda2_base: 0x80003000 + inputs: + dol: + path: main.dol + translation: + entry_points: [0x80001000, 0x80001020] + output: + root: generated + """); + + (Dictionary Sources, byte[] Metadata, byte[] Bundle) Translate( + string name, int threads, bool bounded, bool emitFiles = true) + { + var output = Path.Combine(root, name); + var functions = Path.Combine(output, "functions"); + var metadata = Path.Combine(output, "metadata.json"); + var bundle = Path.Combine(output, "sources.bundle"); + var args = new List + { + "translate-recursive", $"0x{entry:X8}", "--project", projectPath, + "--outdir", functions, "--output-metadata", metadata, + "--production-source-bundle", bundle, "--threads", threads.ToString() + }; + if (bounded) args.Add("--bounded-ir"); + if (!emitFiles) args.Add("--no-function-files"); + Assert.Equal(0, InvokeCli(args.ToArray())); + var sources = Directory.Exists(functions) + ? Directory.GetFiles(functions, "*.cpp").ToDictionary( + static path => Path.GetFileName(path), static path => File.ReadAllText(path)) + : new Dictionary(); + return (sources, File.ReadAllBytes(metadata), File.ReadAllBytes(bundle)); + } + + var buffered = Translate("buffered", 1, bounded: false); + Assert.Equal(2, buffered.Sources.Count); + foreach (var threads in new[] { 1, 3 }) + { + var lazy = Translate($"lazy-{threads}", threads, bounded: true); + Assert.Equal(buffered.Sources.Keys.OrderBy(static name => name), + lazy.Sources.Keys.OrderBy(static name => name)); + foreach (var pair in buffered.Sources) Assert.Equal(pair.Value, lazy.Sources[pair.Key]); + Assert.Equal(buffered.Metadata, lazy.Metadata); + Assert.Equal(buffered.Bundle, lazy.Bundle); + } + var bundleOnly = Translate("bundle-only", 3, bounded: true, emitFiles: false); + Assert.Empty(bundleOnly.Sources); + Assert.Equal(buffered.Metadata, bundleOnly.Metadata); + Assert.Equal(buffered.Bundle, bundleOnly.Bundle); + } + finally + { + if (Directory.Exists(root)) Directory.Delete(root, recursive: true); + } + } + [Fact] public void UnsupportedOpcodeFailsStrictTranslation() { From 1242f8e088b02c8d71000f4a76b72390366c87ac Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:35:52 +0100 Subject: [PATCH 06/17] keep the existing shader renderer --- .github/workflows/build.yml | 10 ---------- .github/workflows/recomp-test.yml | 12 ++++++++++++ aurora-main | 2 +- runtime/include/runtime_config.h | 14 -------------- runtime/src/main.cpp | 2 -- runtime/src/settings_overlay.cpp | 6 ------ runtime/tests/runtime_config_tests.cpp | 20 -------------------- 7 files changed, 13 insertions(+), 53 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e7c7ac7e8..72681a7b4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -136,13 +136,3 @@ jobs: runtime/tests/ax_mix_kernels_tests.cpp -o build-macos/ax-mix-sanitized build-macos/ax-mix-sanitized build-macos/mkw_ax_mix_kernels_tests --benchmark 20000 - - - name: Build and test GX performance paths - shell: bash - run: | - cmake -S runtime -B build-macos -DMKW_BUILD_AURORA_TESTS=ON - cmake --build build-macos --parallel 3 --target \ - gx_fifo_tests shader_module_cache_tests gx_shader_layout_tests \ - mkw_gx_texture_binding_tests mkw_gx_sync_tests mkw_gx_dl_cache_tests - ctest --test-dir build-macos --output-on-failure \ - -R '^(GXFifoTest|RuntimeDirectDrawTest|ShaderModuleCacheTest|GxShader(Layout|Key|Arena|Mode|State)|mkw_gx_(texture_binding|sync|dl_cache)_tests)' diff --git a/.github/workflows/recomp-test.yml b/.github/workflows/recomp-test.yml index 6ae6eb8ee..6b862ce88 100644 --- a/.github/workflows/recomp-test.yml +++ b/.github/workflows/recomp-test.yml @@ -69,6 +69,18 @@ jobs: shell: pwsh run: ./Launcher/Test-Recompilation.ps1 -Parallel 4 + - name: Test GX performance paths + shell: pwsh + run: | + $tools = Join-Path $PWD 'Launcher/artifacts/portable-tools' + $build = Join-Path $PWD 'build/recomp-test/native-build' + $env:PATH = "$build;$tools/llvm-mingw/bin;$tools/CMake/bin;$tools/Ninja;$env:PATH" + cmake -S build/recomp-test/runtime -B $build -DMKW_BUILD_AURORA_TESTS=ON + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + cmake --build $build --parallel 3 --target gx_fifo_tests shader_module_cache_tests mkw_gx_texture_binding_tests mkw_gx_sync_tests mkw_gx_dl_cache_tests + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + ctest --test-dir $build --output-on-failure --no-tests=error -R '^(GXFifoTest|RuntimeDirectDrawTest|ShaderModuleCacheTest|mkw_gx_(texture_binding|sync|dl_cache)_tests)' + # Print cache results (even if the build fails) - name: Show sccache stats if: always() diff --git a/aurora-main b/aurora-main index cf312581c..54f9704fd 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit cf312581c4c7cdcdf03f7d6f5e24875d9898c082 +Subproject commit 54f9704fddaf5f14d2a9e893dede5ce367757e31 diff --git a/runtime/include/runtime_config.h b/runtime/include/runtime_config.h index 101c50f59..d88ac372e 100644 --- a/runtime/include/runtime_config.h +++ b/runtime/include/runtime_config.h @@ -47,7 +47,6 @@ struct RuntimeUserConfig { std::optional displayMode; std::optional frameInterpolationFps; std::optional metalFxSpatialUpscaling; - std::optional shaderMode; std::optional skipUnreadyPipelines; std::optional vsync; std::optional gxWorker; @@ -295,7 +294,6 @@ inline void EnsureConfigFile() { "frame_interpolation_fps = 0\n" "display_mode = \"windowed\"\n" "graphics_api = \"auto\"\n" - "shader_mode = 'specialized'\n" "skip_unready_pipelines = true\n" "vsync = false\n" "# Runs GX command processing on its own thread. Read once, at startup.\n" @@ -487,10 +485,6 @@ inline RuntimeUserConfig ParseConfigDocument(const toml::value& document) { } config.metalFxSpatialUpscaling = FindConfigValue(document, "video", "metalfx_spatial_upscaling"); - if (auto value = FindConfigValue(document, "video", "shader_mode"); - value && (*value == "specialized" || *value == "hybrid")) { - config.shaderMode = *value; - } config.skipUnreadyPipelines = FindConfigValue(document, "video", "skip_unready_pipelines"); config.vsync = FindConfigValue(document, "video", "vsync"); config.gxWorker = FindConfigValue(document, "video", "gx_worker"); @@ -708,12 +702,6 @@ inline bool SetDisplayMode(std::string value) { return WriteSetting("video", "display_mode", FormatString(value)); } -inline bool SetShaderMode(std::string value) { - if (value != "specialized" && value != "hybrid") return false; - Mutable().shaderMode = value; - return WriteSetting("video", "shader_mode", FormatString(value)); -} - inline bool SetSkipUnreadyPipelines(bool value) { Mutable().skipUnreadyPipelines = value; return WriteSetting("video", "skip_unready_pipelines", value ? "true" : "false"); @@ -1048,8 +1036,6 @@ inline uint32_t FrameInterpolationFps(uint32_t fallback = 0) { return Get().frameInterpolationFps.value_or(fallback); } -inline std::string ShaderMode() { return Get().shaderMode.value_or("specialized"); } - // Whether to skip draws whose graphics pipeline has not finished compiling yet. inline bool SkipUnreadyPipelines(bool fallback = true) { return Get().skipUnreadyPipelines.value_or(fallback); diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 5266e25d0..3026abd6e 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -1464,8 +1464,6 @@ int RuntimeMain(int argc, char** argv) { // aurora_initialize does. Wiimote::ConfigureSdlHints(RuntimeConfigFile::WiiRemotesEnabled()); - aurora_set_shader_mode(RuntimeConfigFile::ShaderMode() == "hybrid" - ? AURORA_SHADER_HYBRID : AURORA_SHADER_SPECIALIZED); const AuroraInfo auroraInfo = aurora_initialize(0, nullptr, &auroraConfig); if (auroraInfo.initializationStatus != AURORA_INITIALIZATION_SUCCESS) { throw std::runtime_error(auroraInfo.initializationError != nullptr diff --git a/runtime/src/settings_overlay.cpp b/runtime/src/settings_overlay.cpp index 0e32c1334..6efe7e31b 100644 --- a/runtime/src/settings_overlay.cpp +++ b/runtime/src/settings_overlay.cpp @@ -110,7 +110,6 @@ int g_displayMode = [] { } return static_cast(AURORA_DISPLAY_MODE_WINDOWED); }(); -int g_shaderMode = RuntimeConfigFile::ShaderMode() == "hybrid" ? 1 : 0; bool g_skipUnreadyPipelines = RuntimeConfigFile::SkipUnreadyPipelines(true); bool g_vsync = RuntimeConfigFile::Vsync(); bool g_disableCopyFilter = RuntimeConfigFile::DisableCopyFilter(true); @@ -1279,16 +1278,11 @@ void DrawGraphicsSettings() { aurora_set_disable_copy_filter(g_disableCopyFilter); RuntimeConfigFile::SetDisableCopyFilter(g_disableCopyFilter); } - if (ImGui::Combo("Shaders (restart required)", &g_shaderMode, "Specialized\0Hybrid\0")) { - RuntimeConfigFile::SetShaderMode(g_shaderMode == 1 ? "hybrid" : "specialized"); - } - ImGui::BeginDisabled(g_shaderMode == 1); if (ImGui::Checkbox("Skip draws while shaders compile", &g_skipUnreadyPipelines)) { GX_SYNC_INLINE(); aurora_set_skip_unready_pipelines(g_skipUnreadyPipelines); RuntimeConfigFile::SetSkipUnreadyPipelines(g_skipUnreadyPipelines); } - ImGui::EndDisabled(); if (ImGui::Checkbox("VSync", &g_vsync)) { aurora_enable_vsync(g_vsync); RuntimeConfigFile::SetVsync(g_vsync); diff --git a/runtime/tests/runtime_config_tests.cpp b/runtime/tests/runtime_config_tests.cpp index 98f8fadcd..32e42f679 100644 --- a/runtime/tests/runtime_config_tests.cpp +++ b/runtime/tests/runtime_config_tests.cpp @@ -34,26 +34,6 @@ bool SetsOffset(const std::string& original, const char* message) { } // namespace int main() { - for (const std::string mode : {"specialized", "hybrid"}) { - std::istringstream input("[video]\nshader_mode = '" + mode + "'\n"); - const auto config = RuntimeConfigFile::ParseConfig(input, "shader.toml"); - if (!Require(config.shaderMode == mode, "valid shader mode was not loaded")) return 1; - } - for (const std::string text : {"", "video = 4\n", "[[video]]\nshader_mode = 'hybrid'\n", - "[video]\nshader_mode = true\n", "[video]\nshader_mode = 1\n", - "[video]\nshader_mode = []\n", "[video]\nshader_mode = {}\n", - "[video]\nshader_mode = 'force_generic'\n", "[video]\nshader_mode = 'HYBRID'\n", - "[video]\nshader_mode = 'hybrid'\nshader_mode = 'specialized'\n", - "[video]\nshader_mode = 'hybrid'\n[video]\n"}) { - std::istringstream input(text); - const auto config = RuntimeConfigFile::ParseConfig(input, "invalid-shader.toml"); - if (!Require(config.shaderMode.value_or("specialized") == "specialized", "invalid shader mode did not default")) return 1; - } - { - const auto doc = Update("[video]\nshader_mode = 'specialized'\n", "video", "shader_mode", "'hybrid'"); - if (!Require(doc && doc->at("video").at("shader_mode").as_string() == "hybrid", "next-start shader mode was not saved")) return 1; - } - { std::istringstream input("[video]\nmetalfx_spatial_upscaling = true\n"); const RuntimeUserConfig config = RuntimeConfigFile::ParseConfig(input, "enabled.toml"); From 542e4ad20e5a1d44d609d6b90065d666135563c8 Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:43:20 +0100 Subject: [PATCH 07/17] update aurora correctness fixes --- aurora-main | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aurora-main b/aurora-main index a222e54a4..720728a6f 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit a222e54a4e53d55415de1de1e4fe9dece2705288 +Subproject commit 720728a6f43c51fdbe211ccfea6859721f86213d From d8d8ae6c0bc7cd2c395afaed8fd5a8fe95e24dda Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:05:31 +0100 Subject: [PATCH 08/17] update controller save fixes --- aurora-main | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aurora-main b/aurora-main index 720728a6f..4a04aa839 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit 720728a6f43c51fdbe211ccfea6859721f86213d +Subproject commit 4a04aa839f1aa67753a3040e82c3e0589ba82d7f From dc2885ed616bc4c2c7fb5cd6e4db12b78fba79dd Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:15:54 +0100 Subject: [PATCH 09/17] reject stale linux aurora packages --- .github/workflows/build.yml | 4 +++ Launcher/Prepare-NativePrebuilt.sh | 7 ++--- Launcher/Test-AuroraPrebuilt.sh | 45 +++++++++++++++++++++++++++ Launcher/local-build.sh | 8 ++++- runtime/cmake/AuroraFingerprint.cmake | 40 ++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 6 deletions(-) create mode 100755 Launcher/Test-AuroraPrebuilt.sh create mode 100644 runtime/cmake/AuroraFingerprint.cmake diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d1ae32d3d..23e26fa76 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -59,6 +59,10 @@ jobs: with: persist-credentials: false + - name: Test Linux prebuilt package selection + if: runner.os == 'Linux' + run: bash Launcher/Test-AuroraPrebuilt.sh + - name: Configure without graphics dependencies or game assets run: >- cmake -S runtime -B build-platform -G Ninja diff --git a/Launcher/Prepare-NativePrebuilt.sh b/Launcher/Prepare-NativePrebuilt.sh index 61a94e36d..5dfc97871 100755 --- a/Launcher/Prepare-NativePrebuilt.sh +++ b/Launcher/Prepare-NativePrebuilt.sh @@ -191,9 +191,7 @@ if [[ -n "$sysroot" ]]; then fi flag_fingerprint=$(printf '%s\n' "${fixed_configure_flags[@]}" | sha256sum | awk '{print $1}') -# extern/ is excluded because the payload ships that tree separately (aurora-main/extern is bundled -# whole by build-appimage.sh); build/ is a plain developer build directory. -aurora_fingerprint=$(fingerprint_tree "$aurora_source" extern build) +aurora_fingerprint=$("$cmake_bin" "-DAURORA_DIR=$aurora_source" -P "$runtime_source/cmake/AuroraFingerprint.cmake") [[ -n "$aurora_fingerprint" ]] || fail "The aurora source tree could not be fingerprinted: $aurora_source" # The harvested Crypto++ archive is consumed against this tree's headers, so it is fingerprinted. @@ -636,8 +634,7 @@ generated_cmake="$output_dir/native_prebuilt.cmake" # --------------------------------------------------------------------------- # Provenance: the pinned toolchain is what makes a precompiled archive interchangeable with # locally compiled objects (bit-for-bit the compiler and flag set the user's machine will use); -# LocalBuild.ps1's Windows equivalent re-checks both before consuming a package, and a Linux -# consumer-side check (once local-build.sh gains --native-prebuilt-dir) should do the same. +# local-build.sh checks the Aurora source fingerprint before accepting this package. # compiler_sha256/flag_fingerprint/aurora_fingerprint/third_party_fingerprint were already computed # above (needed before the --print-fingerprint-only early exit). # --------------------------------------------------------------------------- diff --git a/Launcher/Test-AuroraPrebuilt.sh b/Launcher/Test-AuroraPrebuilt.sh new file mode 100755 index 000000000..de2adc463 --- /dev/null +++ b/Launcher/Test-AuroraPrebuilt.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -euo pipefail + +launcher=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +scratch=$(mktemp -d) +trap 'rm -rf "$scratch"' EXIT +cd "$scratch" +mkdir -p 'workspace/aurora-main/include' package +cd workspace/aurora-main +printf 'project(aurora)\n' > CMakeLists.txt +printf 'header\n' > 'include/a header.h' +# Manifest format used by existing Linux packages. +fingerprint=$( + for path in CMakeLists.txt 'include/a header.h'; do + printf '%s %s\n' "$path" "$(sha256sum < "$path" | cut -d ' ' -f 1)" + done | sha256sum | cut -d ' ' -f 1 +) +mkdir -p .git extern build +touch .git/config extern/dependency build/object +cd ../.. +touch package/native_prebuilt.cmake +args=(--workspace workspace --output-dir output --translator-bin unused + --cc true --cxx true --ninja true --x86-cpu-profile v2 --native-prebuilt-dir package) +expect_error() { + local expected=$1 output + if output=$(bash "$launcher/local-build.sh" "${args[@]}" 2>&1); then + echo "unexpected build success" >&2 + exit 1 + fi + if [[ "$output" != *"$expected"* ]]; then + printf 'expected %s, got %s\n' "$expected" "$output" >&2 + exit 1 + fi +} +expect_error 'Native prebuilt provenance is missing' +printf '{"AuroraSourceFingerprint":"%s"}\n' "$fingerprint" > package/provenance.json +expect_error 'Extracted main.dol' +printf 'edited\n' >> 'workspace/aurora-main/include/a header.h' +expect_error 'does not match the Aurora sources' +printf 'header\n' > 'workspace/aurora-main/include/a header.h' +for invalid in '{}' 'null' 'not json' '{"AuroraSourceFingerprint":false}' \ + "{\"nested\":{\"AuroraSourceFingerprint\":\"$fingerprint\"}}"; do + printf '%s\n' "$invalid" > package/provenance.json + expect_error 'does not match the Aurora sources' +done diff --git a/Launcher/local-build.sh b/Launcher/local-build.sh index c344ea2b3..6efbd6cfe 100755 --- a/Launcher/local-build.sh +++ b/Launcher/local-build.sh @@ -201,6 +201,10 @@ require_command "$cxx_bin" cxx if [[ -n "$native_prebuilt_dir" ]]; then assert_file "$native_prebuilt_dir/native_prebuilt.cmake" "Native prebuilt package" + assert_file "$native_prebuilt_dir/provenance.json" "Native prebuilt provenance" + native_prebuilt_dir=$(cd "$native_prebuilt_dir" && pwd) + "$cmake_bin" "-DAURORA_DIR=$workspace/aurora-main" "-DPROVENANCE_FILE=$native_prebuilt_dir/provenance.json" \ + -P "$script_dir/../runtime/cmake/AuroraFingerprint.cmake" fi host_supports_x86_64_v3() { @@ -497,7 +501,9 @@ if [[ -n "$fuse_ld_override" ]]; then configure_args+=(-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=$fuse_ld_quoted") fi if [[ -n "$native_prebuilt_dir" ]]; then - configure_args+=(-DMKW_NATIVE_PREBUILT_DIR="$native_prebuilt_dir") + configure_args+=(-DMKW_NATIVE_PREBUILT_DIR="$native_prebuilt_dir" -DMKW_AURORA_DIR="$workspace/aurora-main") +else + configure_args+=(-UMKW_NATIVE_PREBUILT_DIR) fi if [[ -n "$sysroot" ]]; then configure_args+=(-DCMAKE_SYSROOT="$sysroot") diff --git a/runtime/cmake/AuroraFingerprint.cmake b/runtime/cmake/AuroraFingerprint.cmake new file mode 100644 index 000000000..c12bb115c --- /dev/null +++ b/runtime/cmake/AuroraFingerprint.cmake @@ -0,0 +1,40 @@ +cmake_minimum_required(VERSION 3.20) + +if(NOT EXISTS "${AURORA_DIR}/CMakeLists.txt") + message(FATAL_ERROR "Aurora source tree is missing: ${AURORA_DIR}") +endif() + +# Keep the Linux package manifest independent of the checkout path. +file(GLOB _entries RELATIVE "${AURORA_DIR}" "${AURORA_DIR}/*") +list(REMOVE_ITEM _entries ".git" "extern" "build") +set(_files "") +foreach(_entry IN LISTS _entries) + if(IS_SYMLINK "${AURORA_DIR}/${_entry}") + continue() + elseif(IS_DIRECTORY "${AURORA_DIR}/${_entry}") + file(GLOB_RECURSE _children LIST_DIRECTORIES false RELATIVE "${AURORA_DIR}" "${AURORA_DIR}/${_entry}/*") + list(APPEND _files ${_children}) + else() + list(APPEND _files "${_entry}") + endif() +endforeach() +list(FILTER _files EXCLUDE REGEX "(^|/)\\.git(/|$)") +list(SORT _files) +set(_manifest "") +foreach(_file IN LISTS _files) + if(NOT IS_SYMLINK "${AURORA_DIR}/${_file}") + file(SHA256 "${AURORA_DIR}/${_file}" _hash) + string(APPEND _manifest "${_file} ${_hash}\n") + endif() +endforeach() +string(SHA256 _fingerprint "${_manifest}") + +if(DEFINED PROVENANCE_FILE) + file(READ "${PROVENANCE_FILE}" _provenance) + string(JSON _recorded ERROR_VARIABLE _error GET "${_provenance}" AuroraSourceFingerprint) + if(_error OR NOT _recorded STREQUAL _fingerprint) + message(FATAL_ERROR "Native prebuilt package does not match the Aurora sources. Regenerate it with Prepare-NativePrebuilt.sh or omit --native-prebuilt-dir.") + endif() +else() + execute_process(COMMAND "${CMAKE_COMMAND}" -E echo "${_fingerprint}" COMMAND_ERROR_IS_FATAL ANY) +endif() From 01a4f956c4667b4d2791131a0bc5fa73518faddc Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:38:36 +0100 Subject: [PATCH 10/17] update aurora linux test linkage --- aurora-main | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aurora-main b/aurora-main index 4a04aa839..fc09f2ead 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit 4a04aa839f1aa67753a3040e82c3e0589ba82d7f +Subproject commit fc09f2ead0df64621346ca5c5c745d8b24b089f7 From cf7de85410d4928134e0a42139bbb14bbbc439bd Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:51:04 +0100 Subject: [PATCH 11/17] fix gx cache validation and translator flags --- .github/workflows/recomp-test.yml | 2 +- aurora-main | 2 +- runtime/src/hle/gx/gx_dl.cpp | 171 ++++----------- runtime/src/hle/gx/gx_dl_cache.h | 152 +++++++++++-- runtime/src/hle/gx/gx_internal.h | 1 + runtime/src/hle/gx/gx_objects.cpp | 12 +- runtime/src/hle/gx/gx_texture.cpp | 58 +++-- .../src/hle/gx/gx_texture_binding_contract.h | 16 +- runtime/src/hle/gx/gx_worker.cpp | 8 + runtime/tests/ax_mix_kernels_tests.cpp | 14 +- runtime/tests/gx_copy_tests.cpp | 39 +++- runtime/tests/gx_direct_draw_tests.cpp | 27 ++- runtime/tests/gx_dl_cache_tests.cpp | 202 +++++++++++++----- .../runtime_seams.h | 8 +- runtime/tests/gx_texture_binding_tests.cpp | 140 +++++++++++- runtime/tests/gx_worker_tests.cpp | 31 ++- translator/src/Translator.Cli/Program.cs | 39 ++-- .../Translation/CanonicalIrDictionary.cs | 2 + .../TranslateRecursiveRuntimeConfigTests.cs | 20 ++ 19 files changed, 644 insertions(+), 300 deletions(-) diff --git a/.github/workflows/recomp-test.yml b/.github/workflows/recomp-test.yml index 6b862ce88..3c3daf3c6 100644 --- a/.github/workflows/recomp-test.yml +++ b/.github/workflows/recomp-test.yml @@ -75,7 +75,7 @@ jobs: $tools = Join-Path $PWD 'Launcher/artifacts/portable-tools' $build = Join-Path $PWD 'build/recomp-test/native-build' $env:PATH = "$build;$tools/llvm-mingw/bin;$tools/CMake/bin;$tools/Ninja;$env:PATH" - cmake -S build/recomp-test/runtime -B $build -DMKW_BUILD_AURORA_TESTS=ON + cmake -S build/recomp-test/runtime -B $build -DMKW_BUILD_AURORA_TESTS=ON -DFETCHCONTENT_FULLY_DISCONNECTED=OFF if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } cmake --build $build --parallel 3 --target gx_fifo_tests shader_module_cache_tests mkw_gx_texture_binding_tests mkw_gx_sync_tests mkw_gx_dl_cache_tests if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } diff --git a/aurora-main b/aurora-main index 07880b779..61fdd3584 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit 07880b779c5fb57324b19736afdc69427f69d873 +Subproject commit 61fdd3584e1f4adf8541a3537cb7e8e04c184746 diff --git a/runtime/src/hle/gx/gx_dl.cpp b/runtime/src/hle/gx/gx_dl.cpp index 255b19f94..bf47a7882 100644 --- a/runtime/src/hle/gx/gx_dl.cpp +++ b/runtime/src/hle/gx/gx_dl.cpp @@ -5,10 +5,7 @@ #include "isa/big_endian.h" #include - -// The display-list scan cache validates a cached scan against the live guest -// bytes with a 64-bit XXH3 digest (see GxDisplayListScanCache::CanReuse). -#include +#include namespace aurora::gx::fifo { bool in_display_list(); @@ -25,18 +22,9 @@ using namespace GxStream; using GxCpDecode::ApplyCpRegWrite; using GxCpDecode::SameVtxAttrFmt; -// Small display lists dominate the in-race call count. Cache them as well, but -// cap both individual entries and aggregate copied command bytes so malformed -// guest input cannot turn this optimization into unbounded host allocation. -constexpr uint32_t kDlScanCacheMaxEntryBytes = 64u * 1024u; constexpr size_t kDlScanCacheMaxEntries = 8192; constexpr size_t kDlScanCacheMaxStoredBytes = 8u * 1024u * 1024u; -// Display-list write tracking (audit F6a): re-digesting every list every call is the -// costliest step of GX__CallDisplayList, and wasted on BRRES shape lists that are written -// once at load and never touched again. -constexpr uint64_t kDlWriteGenerationUntracked = GxGuestWrite::kUntracked; - // Byte footprint of one vertex of the currently configured layout. Indexed attributes are // sized before the matrix-attribute check here (unlike BuildScanVertexLayout), but a // GX_INDEX16 matrix attribute is impossible on real hardware so both are unreachable. @@ -470,9 +458,6 @@ static HleGxVertexStateSnapshot CaptureGxVertexStateForCpWrites(const std::vecto } struct DlScanCacheEntry { - uint32_t listAddr = 0; - uint32_t nbytes = 0; - uint64_t layoutHash = 0; std::array maxIdx{}; std::array sawIdx{}; std::array maxXfIdx{}; @@ -484,22 +469,22 @@ struct DlScanCacheEntry { bool dlHasArrayStateWrites = false; bool dlHasCpWrites = false; bool scanOk = false; - // Flatten results for lists that need one. `flattenVerbatim` means the - // flattened stream is byte-identical to the source list, so aurora can be - // handed the guest bytes directly and no copy has to be stored. bool needsFlatten = false; bool flattenOk = false; - bool flattenVerbatim = false; }; struct DlScanCacheRecord { DlScanCacheEntry result{}; + uint32_t listAddr = 0; + uint32_t nbytes = 0; + uint64_t layoutHash = 0; // Answer of DisplayListMayContainDraw for these bytes bool mayContainDraw = false; uint64_t contentDigest = 0; - uint64_t writeGeneration = kDlWriteGenerationUntracked; + uint64_t writeGeneration = GxGuestWrite::kUntracked; std::vector cpWrites{}; - std::vector flattened{}; + bool streamIsSource = false; + std::vector stream{}; }; using DlScanCacheState = GxDisplayListScanCache::BoundedCache; @@ -529,92 +514,36 @@ static uint64_t HashScanLayoutState() { return g_hleGxState.vtxLayoutHash; } -static uint64_t DlScanCacheKey(uint32_t listAddr, uint32_t nbytes, uint64_t layoutHash) { - return GxDisplayListScanCache::MakeIdentityKey(listAddr, nbytes, layoutHash); -} - static DlScanCacheState& DlScanCache() { static thread_local DlScanCacheState s_cache{kDlScanCacheMaxEntries, kDlScanCacheMaxStoredBytes}; return s_cache; } -static uint64_t DlContentDigest(const uint8_t* list, uint32_t nbytes) { - return static_cast(XXH3_64bits(list, static_cast(nbytes))); -} - - -struct DlScanCacheProbe { - const DlScanCacheRecord* record = nullptr; - uint64_t contentDigest = 0; - bool digestValid = false; - uint64_t writeGeneration = kDlWriteGenerationUntracked; -}; - -static DlScanCacheProbe ProbeDlScanCache(const uint8_t* list, uint32_t listAddr, uint32_t nbytes, - uint64_t layoutHash) { - DlScanCacheProbe probe{}; - probe.writeGeneration = GxGuestWrite::GenerationForRange(listAddr, nbytes); - - auto& s_cache = DlScanCache(); - const uint64_t key = DlScanCacheKey(listAddr, nbytes, layoutHash); - if (auto* found = s_cache.Find(key)) { - auto& record = *found; - const auto& entry = record.result; - const bool identityMatches = entry.listAddr == CanonicalizeGxMainRamAddress(listAddr) && - entry.nbytes == nbytes && entry.layoutHash == layoutHash; - if (identityMatches && probe.writeGeneration != kDlWriteGenerationUntracked && - record.writeGeneration == probe.writeGeneration) { - // Nothing has flushed a write over these bytes since the digest was - // taken, so the stored digest still describes them. Skip XXH3. - probe.record = &record; - probe.contentDigest = record.contentDigest; - probe.digestValid = true; - } else { - probe.contentDigest = DlContentDigest(list, nbytes); - probe.digestValid = true; - if (identityMatches && - GxDisplayListScanCache::CanReuse(entry.listAddr, entry.nbytes, entry.layoutHash, - record.contentDigest, listAddr, nbytes, layoutHash, - probe.contentDigest)) { - // False alarm: the bump did not actually change this list - record.writeGeneration = probe.writeGeneration; - probe.record = &record; - } - } - } - if (probe.record != nullptr) { - s_cache.Touch(key); - } - return probe; -} - -static size_t DlScanCacheRecordBytes(const DlScanCacheRecord& record) { - return record.flattened.size() + record.cpWrites.size() * sizeof(DlCpWrite); -} - static void StoreDlScanCache(uint32_t listAddr, uint32_t nbytes, uint64_t layoutHash, uint64_t contentDigest, uint64_t writeGeneration, bool mayContainDraw, const DlScanCacheEntry& entry, - std::vector&& cpWrites, const uint8_t* flattened, - uint32_t flattenedBytes) { + std::vector&& cpWrites, const uint8_t* stream, + uint32_t streamBytes, bool streamIsSource) try { auto& s_cache = DlScanCache(); - const uint64_t key = DlScanCacheKey(listAddr, nbytes, layoutHash); + const uint64_t key = GxDisplayListScanCache::MakeIdentityKey(listAddr, nbytes, layoutHash); DlScanCacheRecord record{}; record.result = entry; record.mayContainDraw = mayContainDraw; - record.result.listAddr = CanonicalizeGxMainRamAddress(listAddr); - record.result.nbytes = nbytes; - record.result.layoutHash = layoutHash; + record.listAddr = CanonicalizeGxMainRamAddress(listAddr); + record.nbytes = nbytes; + record.layoutHash = layoutHash; record.contentDigest = contentDigest; - // Sampled before the digest was taken, so a write racing the digest can only - // make the next call re-digest, never make it trust a stale entry. + // Sampled before capture so later write notifications force revalidation. record.writeGeneration = writeGeneration; record.cpWrites = std::move(cpWrites); - if (flattened != nullptr && flattenedBytes != 0) { - record.flattened.assign(flattened, flattened + flattenedBytes); + record.streamIsSource = streamIsSource; + if (streamBytes != 0) { + record.stream.assign(stream, stream + streamBytes); } - const size_t incomingBytes = DlScanCacheRecordBytes(record); + const size_t incomingBytes = record.stream.size() + record.cpWrites.size() * sizeof(DlCpWrite); s_cache.Insert(key, std::move(record), incomingBytes); +} catch (const std::bad_alloc&) { + // The current draw still has its scan results and stream. } static void BuildScanVertexLayout(GXVtxFmt vtxfmt, std::array& indexedSteps, @@ -843,7 +772,6 @@ struct DlInterpretVisitor { uint32_t DrawVertexSize(GXVtxFmt vtxfmt) { return CalcDLVertexSize(vtxfmt); } bool OnDraw(const uint8_t*, uint8_t opcode, GXVtxFmt vtxfmt, uint16_t vtxCount, const uint8_t* vertices, uint32_t& payloadBytes) { - EnsureAuroraFrameActive(); const HleGxVertexStateSnapshot drawGuestState = CaptureGxVertexState(); ApplyAuroraVtxStateForDlBegin(vtxfmt); GXBegin(OpcodeToGXPrimitive(opcode), vtxfmt, vtxCount); @@ -1298,27 +1226,29 @@ extern "C" void GX__CallDisplayList_80172f64(uint32_t listAddr, uint32_t nbytes) return; } try { - const uint8_t* list = static_cast(GuestToHostPtr(listAddr, nbytes)); - if (!list) return; + const auto* guestList = static_cast(GuestToHostPtr(listAddr, nbytes)); + if (!guestList) return; - const bool allowScanCache = nbytes <= kDlScanCacheMaxEntryBytes; + // Frame waits can dispatch guest code before the worker-side snapshot is taken. + EnsureAuroraFrameActive(); + GxDisplayListScanCache::DisplayListBytes bytes{guestList, nbytes}; + const bool allowScanCache = bytes.CanCache(); const uint64_t scanLayoutHash = allowScanCache ? HashScanLayoutState() : 0; - // The digest is computed lazily inside the probe: an entry whose covering - // write-tracking granules have not moved since it was stored keeps its - // recorded digest and skips the XXH3 pass over the whole list entirely. - const DlScanCacheProbe probe = - allowScanCache ? ProbeDlScanCache(list, listAddr, nbytes, scanLayoutHash) - : DlScanCacheProbe{}; - const DlScanCacheRecord* cached = probe.record; + const uint64_t writeGeneration = allowScanCache + ? GxGuestWrite::GenerationForRange(listAddr, nbytes) + : GxGuestWrite::kUntracked; + const DlScanCacheRecord* cached = + allowScanCache ? GxDisplayListScanCache::Probe(DlScanCache(), bytes, listAddr, nbytes, + scanLayoutHash, writeGeneration) + : nullptr; // The scan a cached record came from already walked the list for this, // so only a miss pays for DisplayListMayContainDraw. const bool mayContainDraw = - (cached != nullptr) ? cached->mayContainDraw : DisplayListMayContainDraw(list, nbytes); + (cached != nullptr) ? cached->mayContainDraw : DisplayListMayContainDraw(bytes.Data(), nbytes); if (!mayContainDraw) { - EnsureAuroraFrameActive(); GXMarkFrameWork(); - GXCallDisplayList(list, nbytes); + GXCallDisplayList(cached ? cached->stream.data() : bytes.Data(), nbytes); // The republish is required even for register-only lists (verified by // experiment: dropping it corrupts in-race geometry decode). It is // instead memoized inside SyncAppliedVtxStateFromHleReal, so a run of @@ -1368,15 +1298,11 @@ extern "C" void GX__CallDisplayList_80172f64(uint32_t listAddr, uint32_t nbytes) ApplyCpRegWrite(write.reg, write.value); } if (needsFlatten && flattenOk) { - if (entry.flattenVerbatim) { - auroraStream = list; - auroraStreamBytes = nbytes; - } else { - auroraStream = cached->flattened.data(); - auroraStreamBytes = static_cast(cached->flattened.size()); - } + auroraStream = cached->stream.data(); + auroraStreamBytes = static_cast(cached->stream.size()); } } else { + const auto* list = bytes.Data(); std::vector cpWrites{}; // The scan discovers the list's CP writes as it walks, so this path // cannot narrow the snapshot the way the cached one does. @@ -1408,9 +1334,6 @@ extern "C" void GX__CallDisplayList_80172f64(uint32_t listAddr, uint32_t nbytes) // scan/flatten are pure functions of the list bytes and the layout-hash cache key. if (allowScanCache && scanOk && !dlHasNestedDl && (!needsFlatten || flattenOk)) { DlScanCacheEntry entry{}; - entry.listAddr = CanonicalizeGxMainRamAddress(listAddr); - entry.nbytes = nbytes; - entry.layoutHash = scanLayoutHash; entry.maxIdx = maxIdx; entry.sawIdx = sawIdx; entry.maxXfIdx = maxXfIdx; @@ -1424,15 +1347,12 @@ extern "C" void GX__CallDisplayList_80172f64(uint32_t listAddr, uint32_t nbytes) entry.scanOk = scanOk; entry.needsFlatten = needsFlatten; entry.flattenOk = flattenOk; - entry.flattenVerbatim = flattenVerbatim; const bool storeFlattened = needsFlatten && flattenOk && !flattenVerbatim; - const uint64_t contentDigest = - probe.digestValid ? probe.contentDigest : DlContentDigest(list, nbytes); - StoreDlScanCache(listAddr, nbytes, scanLayoutHash, contentDigest, - probe.writeGeneration, mayContainDraw, entry, std::move(cpWrites), - storeFlattened ? flattened.data() : nullptr, - storeFlattened ? static_cast(flattened.size()) : 0u); + StoreDlScanCache(listAddr, nbytes, scanLayoutHash, bytes.Digest(), + writeGeneration, mayContainDraw, entry, std::move(cpWrites), + storeFlattened ? flattened.data() : list, + storeFlattened ? static_cast(flattened.size()) : nbytes, !storeFlattened); } } @@ -1448,7 +1368,6 @@ extern "C" void GX__CallDisplayList_80172f64(uint32_t listAddr, uint32_t nbytes) flattenApplyOk = flattenArraysOk && flattenXfOk; } if (flattenApplyOk) { - EnsureAuroraFrameActive(); GXMarkFrameWork(); GXCallDisplayList(auroraStream, auroraStreamBytes); // No flip back to GX_DIRECT here @@ -1468,9 +1387,8 @@ extern "C" void GX__CallDisplayList_80172f64(uint32_t listAddr, uint32_t nbytes) applyOk = arraysOk && xfOk; } if (applyOk) { - EnsureAuroraFrameActive(); GXMarkFrameWork(); - GXCallDisplayList(list, nbytes); + GXCallDisplayList(cached ? cached->stream.data() : bytes.Data(), nbytes); SyncAppliedVtxStateFromHleReal(); return; @@ -1481,10 +1399,9 @@ extern "C" void GX__CallDisplayList_80172f64(uint32_t listAddr, uint32_t nbytes) if (haveStateBeforeScan) { RestoreGxVertexState(stateBeforeScan); } - EnsureAuroraFrameActive(); GXMarkFrameWork(); InvalidateAppliedAuroraState(); - ParseDisplayList(list, nbytes); + ParseDisplayList(bytes.Data(), nbytes); InvalidateAppliedAuroraState(); } catch (...) {} } diff --git a/runtime/src/hle/gx/gx_dl_cache.h b/runtime/src/hle/gx/gx_dl_cache.h index 927534214..6627bad53 100644 --- a/runtime/src/hle/gx/gx_dl_cache.h +++ b/runtime/src/hle/gx/gx_dl_cache.h @@ -1,9 +1,15 @@ #pragma once +#include "gx_internal.h" + +#include + #include #include #include +#include #include +#include #include #include #include @@ -16,6 +22,8 @@ class BoundedCache { uint64_t key; Value value; size_t bytes; + Entry* newerBytes = nullptr; + Entry* olderBytes = nullptr; }; using Entries = std::list; using Iterator = typename Entries::iterator; @@ -36,8 +44,11 @@ class BoundedCache { // Only validated hits refresh recency. void Touch(uint64_t key) { const auto it = m_index.find(key); - if (it != m_index.end()) { - m_entries.splice(m_entries.begin(), m_entries, it->second); + if (it != m_index.end() && it->second != m_entries.begin()) { + const auto entry = it->second; + UnlinkByteEntry(*entry); + LinkByteEntry(*entry); + m_entries.splice(m_entries.begin(), m_entries, entry); } } @@ -47,34 +58,34 @@ class BoundedCache { return false; } const auto found = m_index.find(key); - const auto old = found == m_index.end() ? m_entries.end() : found->second; - size_t retainedEntries = m_entries.size() - (old != m_entries.end() ? 1u : 0u); - size_t retainedBytes = m_storedBytes - (old != m_entries.end() ? old->bytes : 0u); - const auto fits = [&] { - return retainedEntries < m_maxEntries && retainedBytes <= m_maxBytes - bytes; - }; - - std::array victims{}; + Entry* const old = found == m_index.end() ? nullptr : &*found->second; + size_t retainedBytes = m_storedBytes - (old ? old->bytes : 0u); + const size_t availableBytes = m_maxBytes - bytes; + + std::array victims{}; size_t victimCount = 0; - auto next = m_entries.end(); - while (!fits() && next != m_entries.begin() && victimCount < victims.size()) { - const auto victim = --next; + for (auto* victim = m_oldestByteEntry; + victim && retainedBytes > availableBytes && victimCount < victims.size(); + victim = victim->newerBytes) { if (victim == old) { continue; } victims[victimCount++] = victim; retainedBytes -= victim->bytes; - --retainedEntries; } // Retire at most one batch so later attempts can make progress too. - if (!fits()) { + if (retainedBytes > availableBytes) { for (size_t i = 0; i < victimCount; ++i) { - Erase(victims[i]); + Erase(*victims[i]); } return false; } + // Any byte victim also frees the one slot a new entry can need. + if (victimCount == 0 && !old && m_entries.size() == m_maxEntries) { + victims[victimCount++] = &m_entries.back(); + } - if (old == m_entries.end()) { + if (!old) { m_entries.push_front({key, std::move(value), bytes}); try { m_index.emplace(key, m_entries.begin()); @@ -83,13 +94,15 @@ class BoundedCache { throw; } } else { + UnlinkByteEntry(*old); old->value = std::move(value); m_storedBytes -= old->bytes; old->bytes = bytes; - m_entries.splice(m_entries.begin(), m_entries, old); + m_entries.splice(m_entries.begin(), m_entries, found->second); } + LinkByteEntry(m_entries.front()); for (size_t i = 0; i < victimCount; ++i) { - Erase(victims[i]); + Erase(*victims[i]); } m_storedBytes += bytes; return true; @@ -98,6 +111,8 @@ class BoundedCache { void Clear() { m_index.clear(); m_entries.clear(); + m_newestByteEntry = nullptr; + m_oldestByteEntry = nullptr; m_storedBytes = 0; } @@ -105,17 +120,108 @@ class BoundedCache { size_t StoredBytes() const { return m_storedBytes; } private: - void Erase(Iterator entry) { - m_storedBytes -= entry->bytes; - m_index.erase(entry->key); - m_entries.erase(entry); + void LinkByteEntry(Entry& entry) { + if (entry.bytes == 0) return; + entry.newerBytes = nullptr; + entry.olderBytes = m_newestByteEntry; + if (m_newestByteEntry) { + m_newestByteEntry->newerBytes = &entry; + } else { + m_oldestByteEntry = &entry; + } + m_newestByteEntry = &entry; + } + + void UnlinkByteEntry(Entry& entry) { + if (entry.bytes == 0) return; + if (entry.newerBytes) { + entry.newerBytes->olderBytes = entry.olderBytes; + } else { + m_newestByteEntry = entry.olderBytes; + } + if (entry.olderBytes) { + entry.olderBytes->newerBytes = entry.newerBytes; + } else { + m_oldestByteEntry = entry.newerBytes; + } + entry.newerBytes = nullptr; + entry.olderBytes = nullptr; + } + + void Erase(Entry& entry) { + UnlinkByteEntry(entry); + m_storedBytes -= entry.bytes; + const auto found = m_index.find(entry.key); + const auto position = found->second; + m_index.erase(found); + m_entries.erase(position); } size_t m_maxEntries; size_t m_maxBytes; Entries m_entries; std::unordered_map m_index; + Entry* m_newestByteEntry = nullptr; + Entry* m_oldestByteEntry = nullptr; size_t m_storedBytes = 0; }; +inline constexpr uint32_t kMaxSourceBytes = 64u * 1024u; + +class DisplayListBytes { +public: + DisplayListBytes(const uint8_t* source, uint32_t size) : m_data(source), m_size(size) {} + + bool CanCache() const { return m_size <= kMaxSourceBytes; } + + const uint8_t* Data() { + if (!m_captured && CanCache()) { + // Each thread consumes its list synchronously before reusing the scratch. + static thread_local std::array scratch; + std::memcpy(scratch.data(), m_data, m_size); + m_data = scratch.data(); + m_captured = true; + } + return m_data; + } + + uint64_t SourceDigest() const { return XXH3_64bits(m_data, m_size); } + + uint64_t Digest() { + if (!m_digest) { + m_digest = XXH3_64bits(Data(), m_size); + } + return *m_digest; + } + +private: + const uint8_t* m_data; + uint32_t m_size; + bool m_captured = false; + std::optional m_digest; +}; + +template +const Record* Probe(BoundedCache& cache, DisplayListBytes& list, uint32_t listAddr, + uint32_t nbytes, uint64_t layoutHash, uint64_t writeGeneration) { + const uint64_t key = MakeIdentityKey(listAddr, nbytes, layoutHash); + auto* record = cache.Find(key); + if (!record) return nullptr; + + if (record->listAddr != CanonicalizeGxMainRamAddress(listAddr) || record->nbytes != nbytes || + record->layoutHash != layoutHash) { + return nullptr; + } + if (record->streamIsSource) { + // Submit the owned copy after comparison, even if the guest rewrites the list again. + if (list.SourceDigest() != record->contentDigest) return nullptr; + } else if (!GxGuestWrite::CanSkipDigest(record->writeGeneration, writeGeneration) && + list.Digest() != record->contentDigest) { + return nullptr; + } + record->writeGeneration = writeGeneration; + cache.Touch(key); + return record; +} + } // namespace GxDisplayListScanCache diff --git a/runtime/src/hle/gx/gx_internal.h b/runtime/src/hle/gx/gx_internal.h index 9addd1c07..36c4cdf40 100644 --- a/runtime/src/hle/gx/gx_internal.h +++ b/runtime/src/hle/gx/gx_internal.h @@ -354,6 +354,7 @@ TexObjMeta& GetTexObjMeta(uint32_t addr); TexObjMeta ExtractTexObjMetaFromGuest(uint32_t addr); bool TryGetOrExtractTexObjMeta(uint32_t addr, TexObjMeta& outMeta); // Lookup and publication require g_texObjMutex; reset acquires it itself. +void RefreshTexObjDecodeShadow(TexObjSlot& slot) noexcept; TexObjSlot* TryGetValidatedTexObjSlot(uint32_t addr); void MarkTexObjLoadValidated(uint32_t addr); void InvalidateTexObjLoads(); diff --git a/runtime/src/hle/gx/gx_objects.cpp b/runtime/src/hle/gx/gx_objects.cpp index d87665610..7aa1d78af 100644 --- a/runtime/src/hle/gx/gx_objects.cpp +++ b/runtime/src/hle/gx/gx_objects.cpp @@ -511,15 +511,15 @@ bool ShadowEquals(const uint64_t (&a)[4], const uint64_t (&b)[4]) noexcept { return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]; } +} // namespace + // Captures the current guest bytes as the slot's shadow so the cache is marked in sync. HLE // GXInitTexObj* callers fetch the meta reference before rewriting guest memory, so their capture // is intentionally stale; the next lookup just re-decodes once, which is the safe direction. -void MarkTexObjSyncedLocked(TexObjSlot& slot) noexcept { +void RefreshTexObjDecodeShadow(TexObjSlot& slot) noexcept { slot.hasGuestShadow = ReadTexObjShadow(slot.objAddr, slot.guestShadow); } -} // namespace - TexObjSlot* TryGetValidatedTexObjSlot(uint32_t addr) { TexObjSlot* slot = FindTexObjSlot(addr); if (slot == nullptr) { @@ -573,7 +573,7 @@ bool TryGetOrExtractTexObjMeta(uint32_t addr, TexObjMeta& outMeta) { outMeta = MergeGuestTexObjMeta(*slot, extracted); return true; } - MarkTexObjSyncedLocked(*slot); + RefreshTexObjDecodeShadow(*slot); outMeta = *slot; return true; } @@ -586,7 +586,7 @@ bool TryGetOrExtractTexObjMeta(uint32_t addr, TexObjMeta& outMeta) { QueueTexObjIntervalUpdate(*slot); slot->validatedLoad.Invalidate(); static_cast(*slot) = extracted; - MarkTexObjSyncedLocked(*slot); + RefreshTexObjDecodeShadow(*slot); outMeta = extracted; return true; } @@ -609,7 +609,7 @@ TexObjMeta& GetTexObjMeta(uint32_t addr) { TexObjSlot& slot = FindOrCreateTexObjSlot(addr); slot.validatedLoad.Invalidate(); QueueTexObjIntervalUpdate(slot); - MarkTexObjSyncedLocked(slot); + RefreshTexObjDecodeShadow(slot); return slot; } diff --git a/runtime/src/hle/gx/gx_texture.cpp b/runtime/src/hle/gx/gx_texture.cpp index f643d9daf..45dfdd6bd 100644 --- a/runtime/src/hle/gx/gx_texture.cpp +++ b/runtime/src/hle/gx/gx_texture.cpp @@ -2,6 +2,9 @@ #include "gx_internal.h" #include "runtime_log.h" +// Aurora's source layout supplies the identity carried by GXLoadTexObj. +#include <../lib/gfx/texture.hpp> + #include #include @@ -486,13 +489,25 @@ static void BindUnloadableTexturePlaceholder(uint32_t tid) { GXInitTexObj(&s_placeholder, nullptr, 1, 1, GX_TF_RGBA8, GX_CLAMP, GX_CLAMP, GX_FALSE); s_placeholderReady = true; } - // A zeroed binding record can never compare equal to a real load (those - // always carry a non-zero objAddr), so the CanSkipHostLoad fast path cannot - // mistake a later genuine bind for a no-op. - g_boundTexMaps[tid] = BoundTexInfo{}; + g_boundTexMaps[tid] = {}; GXLoadTexObj(&s_placeholder, (GXTexMapID)tid); } +static void BindHostTexObj(uint32_t oa, uint32_t tid, GXTexObj* obj, const TexObjMeta& meta, + bool forceBind = false) { + const auto& hostObj = *reinterpret_cast(obj); + BoundTexInfo binding{}; + static_cast(binding) = meta; + binding.objAddr = oa; + binding.dataAddr = CanonicalizeGxMainRamAddress(meta.dataAddr); + binding.texObjId = hostObj.texObjId; + binding.texDataVersion = hostObj.texDataVersion; + if (forceBind || !GxTextureBindingContract::SameBinding(g_boundTexMaps[tid], binding)) { + GXLoadTexObj(obj, (GXTexMapID)tid); + g_boundTexMaps[tid] = binding; + } +} + static void LoadTexObjImpl(uint32_t oa, uint32_t tid) { static uint32_t s_invalidMetaLogCount = 0; static uint32_t s_invalidTidLogCount = 0; @@ -513,15 +528,8 @@ static void LoadTexObjImpl(uint32_t oa, uint32_t tid) { if (TexObjSlot* slot = TryGetValidatedTexObjSlot(oa)) { try { - BoundTexInfo newBound{}; - static_cast(newBound) = *slot; - newBound.objAddr = oa; - newBound.dataAddr = CanonicalizeGxMainRamAddress(slot->dataAddr); - if (!GxTextureBindingContract::CanSkipHostLoad( - g_boundTexMaps[tid], newBound, false, false, false)) { - GXLoadTexObj(slot->host->PublicPtr(), (GXTexMapID)tid); - } - g_boundTexMaps[tid] = newBound; + BindHostTexObj(oa, tid, slot->host->PublicPtr(), *slot); + RefreshTexObjDecodeShadow(*slot); } catch (const std::exception& ex) { slot->validatedLoad.Invalidate(); if (s_hostExceptionLogCount++ < 64) { @@ -673,22 +681,8 @@ static void LoadTexObjImpl(uint32_t oa, uint32_t tid) { meta.needsUpload = false; textureDataUploaded = true; } - // Everything the binding contract compares apart from objAddr is the - // shared sampler state, so copy that base wholesale; only dataAddr needs - // to be canonicalized on the way in. - BoundTexInfo newBound{}; - static_cast(newBound) = meta; - newBound.objAddr = oa; - newBound.dataAddr = CanonicalizeGxMainRamAddress(meta.dataAddr); - const BoundTexInfo& oldBound = g_boundTexMaps[tid]; - const bool canSkipHostLoad = GxTextureBindingContract::CanSkipHostLoad( - oldBound, newBound, needsInit, tlutChanged, textureDataUploaded); - - g_boundTexMaps[tid] = newBound; GetTexObjMeta(oa) = meta; - if (!canSkipHostLoad) { - GXLoadTexObj(obj, (GXTexMapID)tid); - } + BindHostTexObj(oa, tid, obj, meta, needsInit || tlutChanged || textureDataUploaded); MarkTexObjLoadValidated(oa); } catch (const std::exception& ex) { if (s_hostExceptionLogCount++ < 64) { @@ -712,9 +706,11 @@ static void LoadTexObjImpl(uint32_t oa, uint32_t tid) { extern "C" void GX__LoadTexObj_80170f2c(uint32_t oa, uint32_t tid) { // Inline and deferred loads use the same complete call-time snapshot. std::optional bytes; - try { - bytes = CaptureTexObjBytes(oa); - } catch (...) { + if (tid < g_boundTexMaps.size()) { + try { + bytes = CaptureTexObjBytes(oa); + } catch (...) { + } } GxWorker::Run([oa, tid, bytes] { if (bytes) { diff --git a/runtime/src/hle/gx/gx_texture_binding_contract.h b/runtime/src/hle/gx/gx_texture_binding_contract.h index 0ede802b6..31577e752 100644 --- a/runtime/src/hle/gx/gx_texture_binding_contract.h +++ b/runtime/src/hle/gx/gx_texture_binding_contract.h @@ -26,9 +26,7 @@ struct ValidatedLoad { } }; -// Every piece of GX texture state that can change the bound view/sampler. Shared by `State` -// (this plus the guest GXTexObj address) and HLE's TexObjMeta (this plus the `userData`/ -// `needsUpload` bookkeeping fields), so a new sampler field here reaches both. +// Sampler fields shared by the binding record and HLE's TexObjMeta. struct SamplerState { uint32_t dataAddr = 0; uint16_t width = 0; @@ -53,6 +51,8 @@ struct SamplerState { // be part of the snapshot. struct State : SamplerState { uint32_t objAddr = 0; + uint32_t texObjId = 0; + uint32_t texDataVersion = 0; }; inline bool SameSamplerState(const SamplerState& lhs, const SamplerState& rhs) noexcept { @@ -75,14 +75,8 @@ inline bool SameSamplerState(const SamplerState& lhs, const SamplerState& rhs) n } inline bool SameBinding(const State& lhs, const State& rhs) noexcept { - return lhs.objAddr == rhs.objAddr && SameSamplerState(lhs, rhs); -} - -inline bool CanSkipHostLoad(const State& oldBinding, const State& newBinding, - bool needsInit, bool tlutChanged, - bool textureDataUploaded) noexcept { - return SameBinding(oldBinding, newBinding) && - !needsInit && !tlutChanged && !textureDataUploaded; + return lhs.objAddr == rhs.objAddr && lhs.texObjId == rhs.texObjId && lhs.texDataVersion == rhs.texDataVersion && + SameSamplerState(lhs, rhs); } } // namespace GxTextureBindingContract diff --git a/runtime/src/hle/gx/gx_worker.cpp b/runtime/src/hle/gx/gx_worker.cpp index 32279eb94..15f91ded5 100644 --- a/runtime/src/hle/gx/gx_worker.cpp +++ b/runtime/src/hle/gx/gx_worker.cpp @@ -73,6 +73,8 @@ bool g_testEnabled = true; bool g_testWakeEachRecord = false; std::atomic g_testNotifications{0}; std::atomic g_testWaits{0}; +bool g_testAcknowledgeRecord = false; // Worker only. +std::atomic g_testAccountedTail{0}; void (*g_testBeforeTailRecheck)() = nullptr; #endif @@ -262,6 +264,12 @@ void WorkerMain() { #endif g_tailCv.notify_all(); } +#if defined(MKW_GX_WORKER_TESTING) + if (g_testAcknowledgeRecord) { + g_testAcknowledgeRecord = false; + g_testAccountedTail.store(tail, std::memory_order_release); + } +#endif } } } diff --git a/runtime/tests/ax_mix_kernels_tests.cpp b/runtime/tests/ax_mix_kernels_tests.cpp index a9e6f398e..c6a9bfe49 100644 --- a/runtime/tests/ax_mix_kernels_tests.cpp +++ b/runtime/tests/ax_mix_kernels_tests.cpp @@ -42,6 +42,8 @@ constexpr Backend kSelected = {kSelectedName, AxMixKernels::MixAddRamp, uint32_t g_random = 0x12345678u; +char g_case[192]{}; + uint32_t NextRandom() { g_random ^= g_random << 13; g_random ^= g_random >> 17; @@ -50,7 +52,9 @@ uint32_t NextRandom() { } void Require(bool condition, const char* message) { - if (!condition) throw std::runtime_error(message); + if (condition) return; + if (g_case[0]) std::fprintf(stderr, "%s: ", g_case); + throw std::runtime_error(message); } int32_t Wrap32(int64_t value) { @@ -85,6 +89,8 @@ std::vector Counts() { void TestRamps(const Backend& backend, uint32_t count, size_t offset, uint16_t volume, uint16_t delta, unsigned pattern) { + std::snprintf(g_case, sizeof(g_case), "%s count=%u offset=%zu volume=%u delta=%u pattern=%u seed=%u", + backend.name, count, offset, unsigned(volume), unsigned(delta), pattern, g_random); // Source ends at the allocation boundary so ASan also catches tail overreads. std::vector input(offset + count, 12345); std::vector initial(offset + count + 1, 0x13579bdf); @@ -121,6 +127,8 @@ void TestRamps(const Backend& backend, uint32_t count, size_t offset, } void TestAccum(const Backend& backend, uint32_t count, size_t offset, unsigned pattern) { + std::snprintf(g_case, sizeof(g_case), "%s count=%u offset=%zu pattern=%u seed=%u", + backend.name, count, offset, pattern, g_random); std::vector source(offset + count, 0x2468ace); std::vector ramp(offset + count, 12345); std::vector initial(offset + count + 1, 0x13579bdf); @@ -155,6 +163,8 @@ void TestAccum(const Backend& backend, uint32_t count, size_t offset, unsigned p void TestAccumEdges(const Backend& backend) { for (int32_t source : kWords) for (int32_t initial : kWords) for (uint16_t gain : kGains) { + std::snprintf(g_case, sizeof(g_case), "%s source=%d initial=%d gain=%u", + backend.name, source, initial, unsigned(gain)); std::array src, dst; std::array ramp; src.fill(source); @@ -169,6 +179,8 @@ void TestAccumEdges(const Backend& backend) { } void TestEndian(const Backend& backend, size_t count, size_t offset) { + std::snprintf(g_case, sizeof(g_case), "%s count=%zu offset=%zu seed=%u", + backend.name, count, offset, g_random); std::vector source(offset + count, 0x13579bdf); for (size_t i = 0; i < count; ++i) { source[offset + i] = i % 2 == 0 ? kWords[(i / 2) % std::size(kWords)] : Wrap32(NextRandom()); diff --git a/runtime/tests/gx_copy_tests.cpp b/runtime/tests/gx_copy_tests.cpp index 8a8e04ab9..40ae458e3 100644 --- a/runtime/tests/gx_copy_tests.cpp +++ b/runtime/tests/gx_copy_tests.cpp @@ -2,6 +2,7 @@ #include "../src/hle/gx/gx_internal.h" #include "gx_sync_support/copy_observation.h" #include "gx/readback.hpp" +#include "gx/texture.hpp" #include "__gx.h" // Exercise the wrappers without registering guest ABI entry points in the host fixture. @@ -70,6 +71,7 @@ void Draw() noexcept { ++g_overlayDraws; } namespace { namespace stub = aurora::gfx::testing; +constexpr u32 kCopyWarmupFrames = 8; class GxCopyTest : public GXFifoTest, public ::testing::WithParamInterface { protected: @@ -109,6 +111,7 @@ class GxCopyTest : public GXFifoTest, public ::testing::WithParamInterface aurora::gx::fifo::end_frame(); GXSetDrawDoneCallback(nullptr); AuroraResetCopyProbesForTesting(); + aurora::gx::texture::shutdown(); aurora::gfx::readback::shutdown(); aurora::gfx::readback::set_supported(false); GXFifoTest::TearDown(); @@ -134,6 +137,13 @@ class GxCopyTest : public GXFifoTest, public ::testing::WithParamInterface GX__SetTexCopyDst_8016f4dc(width, height, format, GX_FALSE); GX__CopyTex_8016fd74(address, clear); } + void WarmCopy(u16 width, u16 height) { + for (u32 frame = 0; frame <= kCopyWarmupFrames; ++frame) { + Copy(kGuestBase, GX_FALSE, GX_TF_RGBA8, width, height); + Drain(); + aurora::gx::texture::end_frame(); + } + } }; TEST_P(GxCopyTest, InterleavedDrawsClearsAndReusedDestinationsNeedNoCompletionTokens) { @@ -206,19 +216,34 @@ TEST_P(GxCopyTest, InvalidationRetiresTheQueuedCopyBeforeLaterConsumers) { EXPECT_EQ(g_drawDoneCallbacks.load(), 0u); } -TEST_P(GxCopyTest, PersistentCopyPolicyAndDepthProbesSurviveWithoutTheWait) { - Copy(kGuestBase, GX_FALSE); - Drain(); - ASSERT_EQ(stub::copyObservations.size(), 1u); +TEST_P(GxCopyTest, LargeCopyBecomesNonpersistentAfterWarmup) { + WarmCopy(640, 480); + ASSERT_EQ(stub::copyObservations.size(), kCopyWarmupFrames + 1); + EXPECT_TRUE(stub::copyObservations[kCopyWarmupFrames - 1].options.persistent); + EXPECT_FALSE(stub::copyObservations.back().options.persistent); + EXPECT_EQ(g_drawDoneCallbacks.load(), 0u); +} + +TEST_P(GxCopyTest, ChangingCopySizeStartsWithColdKey) { + WarmCopy(8, 8); + ASSERT_EQ(stub::copyObservations.size(), kCopyWarmupFrames + 1); + const auto& copies = aurora::gx::g_gxState.copyTextures; + const auto oldCopy = copies.find(GuestToHostPtr(kGuestBase)); + ASSERT_NE(oldCopy, copies.end()); + ASSERT_EQ(oldCopy->second.consecutiveFrames, kCopyWarmupFrames); EXPECT_TRUE(stub::copyObservations.back().options.persistent); - // The renderer stub retains this field, allowing the real copy policy to see a warm destination. - aurora::gx::g_gxState.copyTextures[GuestToHostPtr(kGuestBase)].consecutiveFrames = 8; Copy(kGuestBase, GX_FALSE, GX_TF_RGBA8, 640, 480); Drain(); - EXPECT_FALSE(stub::copyObservations.back().options.persistent); + ASSERT_EQ(stub::copyObservations.size(), kCopyWarmupFrames + 2); + EXPECT_TRUE(stub::copyObservations.back().options.persistent); + EXPECT_EQ(g_drawDoneCallbacks.load(), 0u); +} + +TEST_P(GxCopyTest, DepthProbeStaysNonpersistentWithoutTheWait) { const auto probe = kGuestBase + (GetParam() ? 2048u : 3072u); Copy(probe, GX_FALSE, GX_TF_Z8, 4, 4); Drain(); + ASSERT_EQ(stub::copyObservations.size(), 1u); EXPECT_FALSE(stub::copyObservations.back().options.persistent); EXPECT_FALSE(stub::readbackJobs.empty()); EXPECT_EQ(g_guestBytes[probe - kGuestBase], 0xff); diff --git a/runtime/tests/gx_direct_draw_tests.cpp b/runtime/tests/gx_direct_draw_tests.cpp index 6ddd64f41..6baa6214c 100644 --- a/runtime/tests/gx_direct_draw_tests.cpp +++ b/runtime/tests/gx_direct_draw_tests.cpp @@ -5,6 +5,7 @@ #include #include +#include namespace { class RuntimeDirectDrawTest : public GXFifoTest { @@ -67,8 +68,15 @@ TEST_F(RuntimeDirectDrawTest, DirectNumericPayloadMatchesIncrementalForEveryWrit for (u32 width : {1u, 2u, 4u}) { const u32 bytesPerVertex = (cnt == GX_POS_XY ? 2u : 3u) * GetCompSizeBytes(type); std::vector payload(bytesPerVertex * 3); - for (size_t i = 0; i < payload.size(); ++i) { - payload[i] = static_cast(i * 17u + 11u); + if (type == GX_F32) { + for (size_t i = 0; i < payload.size() / sizeof(float); ++i) { + const float value = static_cast(i + 1) / 7.0f; + BigEndian::WriteFloat32(payload.data(), i * sizeof(float), value); + } + } else { + for (size_t i = 0; i < payload.size(); ++i) { + payload[i] = static_cast(i * 17u + 11u); + } } reset_runtime(); position(cnt, type, 7); @@ -89,6 +97,13 @@ TEST_F(RuntimeDirectDrawTest, DirectNumericPayloadMatchesIncrementalForEveryWrit } TEST_F(RuntimeDirectDrawTest, MixedMatrixColorsAndAllTextureCoordinatesMatchIncremental) { + constexpr u32 bytesPerVertex = 45; + std::vector payload(bytesPerVertex * 3); + std::iota(payload.begin(), payload.end(), u8{64}); + for (u8 vertex = 0; vertex < 3; ++vertex) { + payload[vertex * bytesPerVertex] = vertex * 6; + payload[vertex * bytesPerVertex + 1] = vertex * 6 + 3; + } std::vector reference; for (bool direct : {false, true}) { reset_runtime(); @@ -105,8 +120,7 @@ TEST_F(RuntimeDirectDrawTest, MixedMatrixColorsAndAllTextureCoordinatesMatchIncr g.vtxAttrFmt[0][tex] = {GX_TEX_ST, GX_S16, static_cast(tex)}; } begin(3, direct); - ASSERT_EQ(g.fifoDirectBytesRemaining, direct ? 45u * 3u : 0u); - const std::vector payload(45u * 3u, 0x12); + ASSERT_EQ(g.fifoDirectBytesRemaining, direct ? payload.size() : 0u); GX_HLE_FIFO_WriteBurst(payload.data(), static_cast(payload.size())); if (!direct) { reference = capture_fifo(); @@ -220,6 +234,11 @@ TEST_F(RuntimeDirectDrawTest, LayoutChangeBeforeFirstPayloadRechecksDirectEligib HleFifoWrite(0x11, 1); EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, 0u); EXPECT_TRUE(g_hleGxState.auroraBeginCalled); + HleFifoWrite(0x2233, 2); + HleFifoWrite(0x44556677, 4); + HleFifoWrite(0x8899, 2); + EXPECT_FALSE(g_hleGxState.inBegin); + EXPECT_FALSE(g_hleGxState.auroraBeginCalled); } TEST_F(RuntimeDirectDrawTest, DirectPayloadLargerThanQueueStillMatchesIncremental) { diff --git a/runtime/tests/gx_dl_cache_tests.cpp b/runtime/tests/gx_dl_cache_tests.cpp index 585eb0e90..13def52b6 100644 --- a/runtime/tests/gx_dl_cache_tests.cpp +++ b/runtime/tests/gx_dl_cache_tests.cpp @@ -1,16 +1,32 @@ #include "../src/hle/gx/gx_dl_cache.h" -#include "../src/hle/gx/gx_internal.h" - -#include #include +#include #include #include +#include #include #include extern "C" const uint8_t kMkwGuestRegion = 0; +namespace { +thread_local int g_allocationsBeforeFailure = -1; +} + +void* operator new(size_t size) { + if (g_allocationsBeforeFailure == 0) { + g_allocationsBeforeFailure = -1; + throw std::bad_alloc{}; + } + if (g_allocationsBeforeFailure > 0) --g_allocationsBeforeFailure; + if (auto* memory = std::malloc(size == 0 ? 1 : size)) return memory; + throw std::bad_alloc{}; +} + +void operator delete(void* memory) noexcept { std::free(memory); } +void operator delete(void* memory, size_t) noexcept { std::free(memory); } + namespace { void Require(bool condition, const char* message) { @@ -24,17 +40,21 @@ struct Record { uint64_t contentDigest = 0; uint64_t writeGeneration = GxGuestWrite::kUntracked; std::vector cpWrites; - std::vector flattened; + bool streamIsSource = false; + std::vector stream; - size_t Bytes() const { return flattened.size() + cpWrites.size() * sizeof(uint64_t); } + size_t Bytes() const { return stream.size() + cpWrites.size() * sizeof(uint64_t); } }; using Cache = GxDisplayListScanCache::BoundedCache; +using GxDisplayListScanCache::DisplayListBytes; +using GxDisplayListScanCache::MakeIdentityKey; +using GxDisplayListScanCache::Probe; bool Store(Cache& cache, uint64_t key, size_t flattenedBytes, size_t cpWrites = 0) { Record record{}; record.cpWrites.resize(cpWrites, key); - record.flattened.resize(flattenedBytes, static_cast(key)); + record.stream.resize(flattenedBytes, static_cast(key)); const size_t bytes = record.Bytes(); return cache.Insert(key, std::move(record), bytes); } @@ -62,7 +82,7 @@ void ByteBudgetAndReplacement() { Require(cache.Size() == 2 && cache.StoredBytes() == 12, "replacement byte accounting failed"); Require(cache.Find(1) && !cache.Find(2) && cache.Find(3) == retained, "oldest replacement was evicted or unrelated record moved"); - Require(cache.Find(1)->flattened == std::vector(8, 1), "replacement payload was lost"); + Require(cache.Find(1)->stream == std::vector(8, 1), "replacement payload was lost"); Require(Store(cache, 4, 4), "byte-budget eviction failed"); Require(cache.Size() == 2 && cache.StoredBytes() == 12 && !cache.Find(3) && cache.Find(1), "replacement was not promoted to most recent"); @@ -74,6 +94,49 @@ void ByteBudgetAndReplacement() { Require(cache.StoredBytes() == 8, "CP-write replacement counted old bytes twice"); } +void BytePressureSkipsEmptyRecords() { + Cache cache{514, 8}; + for (uint64_t key = 1; key <= 512; ++key) Require(Store(cache, key, 0), "empty record setup failed"); + Require(Store(cache, 513, 4) && Store(cache, 514, 4), "byte record setup failed"); + cache.Touch(513); + Require(Store(cache, 515, 4), "byte pressure starved behind empty records"); + Require(cache.Size() == 514 && cache.StoredBytes() == 8 && cache.Find(513) && !cache.Find(514), + "combined pressure failed to pick the oldest byte-bearing entry"); + for (uint64_t key = 1; key <= 512; ++key) Require(cache.Find(key), "byte pressure evicted an empty record"); + + const auto* promoted = cache.Find(512); + Require(Store(cache, 512, 4) && cache.Find(512) == promoted && !cache.Find(513), + "zero-to-positive replacement failed to join the byte order"); + Require(Store(cache, 515, 0) && Store(cache, 516, 8) && !cache.Find(512) && cache.Find(515), + "byte pressure evicted a positive-to-zero replacement"); + Require(Store(cache, 517, 0) && Store(cache, 518, 0) && !cache.Find(1) && cache.Find(516), + "entry pressure failed to pick the overall oldest entry"); +} + +void AllocationFailurePreservesVictims() { + for (const int failAfter : {0, 1}) { + Cache cache{2, 8}; + Require(Store(cache, 1, 4) && Store(cache, 2, 4), "allocation failure setup failed"); + auto* first = cache.Find(1); + auto* second = cache.Find(2); + Record record{}; + record.stream.resize(4); + bool failed = false; + g_allocationsBeforeFailure = failAfter; + try { + cache.Insert(3, std::move(record), 4); + } catch (const std::bad_alloc&) { + failed = true; + } + g_allocationsBeforeFailure = -1; + Require(failed, "cache insertion did not hit the allocation failure"); + Require(cache.Size() == 2 && cache.StoredBytes() == 8 && cache.Find(1) == first && + cache.Find(2) == second && !cache.Find(3), "allocation failure changed the working set"); + Require(Store(cache, 4, 4) && !cache.Find(1) && cache.Find(2) == second, + "allocation failure changed byte recency"); + } +} + void RejectedInsertsPreserveRecords() { Cache cache{2, 12}; Require(Store(cache, 1, 8) && Store(cache, 2, 4), "oversize setup failed"); @@ -122,29 +185,21 @@ void BoundedRejectionsMakeProgress() { const auto* protectedRecord = cache.Find(1); Require(!Store(cache, 1, 66), "replacement exceeded 64 victims"); Require(cache.Size() == 64 && cache.StoredBytes() == 64 && cache.Find(1) == protectedRecord && - protectedRecord->flattened.size() == 1, "bounded rejection lost the protected replacement"); + protectedRecord->stream.size() == 1, "bounded rejection lost the protected replacement"); Require(Store(cache, 1, 66) && cache.Size() == 63 && cache.StoredBytes() == 128, "replacement did not make progress for the next attempt"); - - cache.Clear(); - for (uint64_t key = 1; key <= 64; ++key) Require(Store(cache, key, 0), "empty record setup failed"); - Require(Store(cache, 65, 128), "large record setup failed"); - Require(!Store(cache, 66, 1) && cache.Size() == 1 && cache.StoredBytes() == 128, - "zero-byte victims bypassed the eviction work limit"); - Require(Store(cache, 66, 1) && cache.Size() == 1 && cache.StoredBytes() == 1, - "zero-byte victims caused repeated insertion starvation"); } void ClearAndPointerLifetime() { Cache cache{8192, 8192}; Require(Store(cache, 0, 1), "pointer setup failed"); const auto* first = cache.Find(0); - const auto* payload = first->flattened.data(); + const auto* payload = first->stream.data(); for (uint64_t key = 1; key < 8192; ++key) Require(Store(cache, key, 1), "growth insert failed"); cache.Touch(0); for (uint64_t key = 8192; key < 16384; ++key) { Require(Store(cache, key, 1), "churn insert failed"); - Require(cache.Find(0) == first && first->flattened.data() == payload, + Require(cache.Find(0) == first && first->stream.data() == payload, "rehash or unrelated eviction invalidated a retained stream"); cache.Touch(0); } @@ -157,49 +212,91 @@ void ClearAndPointerLifetime() { void LiveBytesStillInvalidateRetainedRecords() { constexpr uint32_t address = 0x80010000; + constexpr uint32_t alias = 0xC0010000; constexpr uint64_t layout = 0x12345678; std::array bytes{0x08, 0x50, 0, 0, 0, 1, 0, 0}; - const auto digest = [&] { return static_cast(XXH3_64bits(bytes.data(), bytes.size())); }; - const auto key = GxDisplayListScanCache::MakeIdentityKey(address, bytes.size(), layout); - Require(key == GxDisplayListScanCache::MakeIdentityKey(0xC0010000, bytes.size(), layout), - "cached and uncached aliases use different keys"); + const auto key = MakeIdentityKey(address, bytes.size(), layout); Cache cache{2, 32}; Record record{}; record.listAddr = CanonicalizeGxMainRamAddress(address); record.nbytes = bytes.size(); record.layoutHash = layout; - record.contentDigest = digest(); + record.contentDigest = XXH3_64bits(bytes.data(), bytes.size()); record.writeGeneration = GxGuestWrite::GenerationForRange(address, bytes.size()); - Require(cache.Insert(key, std::move(record), 0), "validation setup failed"); + record.stream = {0}; + Require(cache.Insert(key, std::move(record), 1), "validation setup failed"); auto* retained = cache.Find(key); - const auto reusable = [&](uint32_t addr, uint32_t count, uint64_t currentLayout, uint64_t currentDigest) { - return GxDisplayListScanCache::CanReuse(retained->listAddr, retained->nbytes, retained->layoutHash, - retained->contentDigest, addr, count, currentLayout, - currentDigest); + const auto probe = [&](uint32_t addr, uint32_t count, uint64_t currentLayout) { + const auto generation = GxGuestWrite::GenerationForRange(addr, count); + DisplayListBytes current{bytes.data(), count}; + return Probe(cache, current, addr, count, currentLayout, generation); }; - Require(reusable(0xC0010000, bytes.size(), layout, digest()), "unchanged alias was rejected"); - Require(!reusable(address, bytes.size() - 1, layout, digest()) && - !reusable(address, bytes.size(), layout + 1, digest()) && - !reusable(address + 32, bytes.size(), layout, digest()), "identity mismatch was reused"); - GxGuestWrite::NotifyWrite(0xC0010000, bytes.size()); - const auto generation = GxGuestWrite::GenerationForRange(address, bytes.size()); - Require(!GxGuestWrite::CanSkipDigest(retained->writeGeneration, generation), "write failed to invalidate digest"); - Require(reusable(address, bytes.size(), layout, digest()), "unchanged bytes after a write were rejected"); - bytes[5] ^= 0x80; - Require(!reusable(address, bytes.size(), layout, digest()), "changed command bytes reused a retained record"); - Require(!GxGuestWrite::CanSkipDigest(GxGuestWrite::kUntracked, GxGuestWrite::kUntracked), - "untracked writes bypassed digest validation"); - Require(Store(cache, 1, 16), "validation churn setup failed"); - cache.Touch(key); + Require(probe(alias, bytes.size(), layout) == retained, "unchanged alias was rejected"); + Require(!probe(address, bytes.size() - 1, layout) && !probe(address, bytes.size(), layout + 1) && + !probe(address + 32, bytes.size(), layout), "identity mismatch was reused"); + GxGuestWrite::NotifyWrite(alias, bytes.size()); + Require(Store(cache, 1, 16) && probe(address, bytes.size(), layout) == retained, + "unchanged bytes after a write were rejected"); + const auto validatedGeneration = GxGuestWrite::GenerationForRange(address, bytes.size()); + Require(retained->writeGeneration == validatedGeneration, "validated hit did not refresh the generation"); Require(Store(cache, 2, 16) && cache.Find(key) == retained && !cache.Find(1), - "unrelated eviction discarded the retained record"); - Require(!reusable(address, bytes.size(), layout, digest()), "churn made a stale digest reusable"); - Record replacement = *retained; - replacement.contentDigest = digest(); - replacement.writeGeneration = generation; - Require(cache.Insert(key, std::move(replacement), 0) && cache.StoredBytes() == 16, - "invalidated replacement changed unrelated byte accounting"); - Require(reusable(address, bytes.size(), layout, digest()), "replacement did not refresh the digest"); + "unrelated eviction discarded the validated record"); + bytes[5] ^= 0x80; + GxGuestWrite::NotifyWrite(alias, bytes.size()); + Require(!probe(address, bytes.size(), layout) && retained->writeGeneration == validatedGeneration, + "changed live commands reused a retained record"); + + constexpr uint32_t untrackedAddress = 0xF0000000; + Record untracked = *retained; + untracked.listAddr = untrackedAddress; + untracked.writeGeneration = GxGuestWrite::kUntracked; + Require(cache.Insert(MakeIdentityKey(untrackedAddress, bytes.size(), layout), std::move(untracked), 1), + "untracked validation setup failed"); + Require(!probe(untrackedAddress, bytes.size(), layout), "untracked writes bypassed digest validation"); +} + +void RawProbeAndScanUseTheSameCapture() { + constexpr uint32_t address = 0x80020000; + constexpr uint64_t layout = 0x12345678; + constexpr std::array original{0x90, 0, 1, 7}; + constexpr uint32_t size = original.size(); + const auto key = MakeIdentityKey(address, size, layout); + const auto generation = GxGuestWrite::GenerationForRange(address, size); + auto guest = original; + Cache cache{2, 32}; + { + DisplayListBytes bytes{guest.data(), size}; + Require(!Probe(cache, bytes, address, size, layout, generation), "empty cache hit"); + const auto* scanned = bytes.Data(); + guest.back() = 42; + Record record{}; + record.listAddr = CanonicalizeGxMainRamAddress(address); + record.nbytes = size; + record.layoutHash = layout; + record.writeGeneration = generation; + record.contentDigest = bytes.Digest(); + Require(scanned[3] == 7 && record.contentDigest == XXH3_64bits(original.data(), size), + "miss digest did not describe the captured bytes"); + record.streamIsSource = true; + record.stream.assign(scanned, scanned + size); + Require(cache.Insert(key, std::move(record), size), "raw validation setup failed"); + } + { + DisplayListBytes bytes{guest.data(), size}; + Require(!Probe(cache, bytes, address, size, layout, generation), + "unchanged generation bypassed raw-byte validation"); + const auto* rescanned = bytes.Data(); + guest.back() = 7; + Require(rescanned[3] == 42 && bytes.Digest() != cache.Find(key)->contentDigest, + "stale-hit scan did not retain the probed bytes"); + } + { + DisplayListBytes bytes{guest.data(), size}; + const auto* hit = Probe(cache, bytes, address, size, layout, generation); + Require(hit != nullptr, "unchanged raw bytes missed"); + guest.back() = 42; + Require(hit->stream[3] == 7, "raw hit submitted bytes different from the probe"); + } } struct LegacyCache { @@ -242,7 +339,7 @@ void Benchmark(Storage& cache, std::string_view policy, std::string_view scenari } else { ++misses; Record record{}; - record.flattened.resize(bytesPerEntry); + record.stream.resize(bytesPerEntry); const auto before = cache.Size(); const bool inserted = cache.Insert(key, std::move(record), bytesPerEntry); skips += !inserted; @@ -286,11 +383,14 @@ int main(int argc, char** argv) { } else { EntryBudgetAndRecency(); ByteBudgetAndReplacement(); + BytePressureSkipsEmptyRecords(); + AllocationFailurePreservesVictims(); RejectedInsertsPreserveRecords(); EvictionLimitAndProtection(); BoundedRejectionsMakeProgress(); ClearAndPointerLifetime(); LiveBytesStillInvalidateRetainedRecords(); + RawProbeAndScanUseTheSameCapture(); std::cout << "display-list scan cache tests passed\n"; } return 0; diff --git a/runtime/tests/gx_texture_binding_support/runtime_seams.h b/runtime/tests/gx_texture_binding_support/runtime_seams.h index 20a4d5e31..694e73751 100644 --- a/runtime/tests/gx_texture_binding_support/runtime_seams.h +++ b/runtime/tests/gx_texture_binding_support/runtime_seams.h @@ -9,13 +9,17 @@ extern "C" const uint8_t kMkwGuestRegion = 0; namespace TextureBindingTest { std::array memory{}; uint32_t unreadableObject = 0; +uint32_t unreadableObjectAccesses = 0; uint64_t rangeChecks = 0; std::vector> retiredRanges; uint8_t* Pointer(uint32_t address, size_t length) { const uint32_t offset = CanonicalizeGxMainRamAddress(address); - if ((unreadableObject != 0 && offset >= unreadableObject && offset < unreadableObject + 32) || - offset > memory.size() || length > memory.size() - offset) { + if (unreadableObject != 0 && offset >= unreadableObject && offset < unreadableObject + 32) { + ++unreadableObjectAccesses; + throw Memory::AccessViolation(address, length, "test unmapped range"); + } + if (offset > memory.size() || length > memory.size() - offset) { throw Memory::AccessViolation(address, length, "test unmapped range"); } return memory.data() + offset; diff --git a/runtime/tests/gx_texture_binding_tests.cpp b/runtime/tests/gx_texture_binding_tests.cpp index 1220fdab6..c85f4295d 100644 --- a/runtime/tests/gx_texture_binding_tests.cpp +++ b/runtime/tests/gx_texture_binding_tests.cpp @@ -2,6 +2,8 @@ #include "../src/hle/gx/gx_internal.h" #include "isa/big_endian.h" +#include + // Keep the production loaders and metadata writers, without a guest dispatch registry. #undef PPC_NATIVE_OVERRIDE #undef PPC_NATIVE_OVERRIDE_VOID @@ -29,6 +31,7 @@ class RuntimeTextureBindingTest : public GXFifoTest { GxWorker::detail::t_onWorker = false; TextureBindingTest::memory.fill(0); TextureBindingTest::unreadableObject = 0; + TextureBindingTest::unreadableObjectAccesses = 0; TextureBindingTest::rangeChecks = 0; TextureBindingTest::retiredRanges.clear(); InvalidateTexObjLoads(); @@ -93,9 +96,11 @@ class RuntimeTextureBindingTest : public GXFifoTest { TEST_F(RuntimeTextureBindingTest, RepeatedBindingSkipsMetadataAndExistingHostLoad) { Init(); Warm(); + Memory::Write32(kGuestGxData + 0x5FC, 0x20); + Memory::Write16(kGuestGxData + 2, 0x1234); ExpectFast(); EXPECT_TRUE(capture_fifo().empty()); - EXPECT_EQ(Memory::Read32(kGuestGxData + 0x5FC) & 1u, 1u); + EXPECT_EQ(Memory::Read32(kGuestGxData + 0x5FC), 0x21u); EXPECT_EQ(Memory::Read16(kGuestGxData + 2), 0); } @@ -166,6 +171,26 @@ TEST_F(RuntimeTextureBindingTest, GetterShadowCannotValidateAnotherSnapshot) { ExpectFast(); } +TEST_F(RuntimeTextureBindingTest, ValidatedLoadRestoresClampedLodAfterGetterRefresh) { + constexpr uint32_t lodOutput = kGuestGxData + 0x600; + const auto getMaxLod = [&] { + GX__GetTexObjLODAll_80170cbc(kObject, 0, 0, 0, lodOutput, 0, 0, 0, 0); + return std::bit_cast(Memory::Read32(lodOutput)); + }; + + Init(); + Memory::Write32(kObject + 4, 0x5000); + Warm(); + ASSERT_EQ(getMaxLod(), 3.0f); + Memory::Write32(kObject + 4, 0x3000); + ASSERT_EQ(getMaxLod(), 3.0f); + Memory::Write32(kObject + 4, 0x5000); + + ExpectFast(); + EXPECT_TRUE(capture_fifo().empty()); + EXPECT_EQ(getMaxLod(), 3.0f); +} + TEST_F(RuntimeTextureBindingTest, ProducerBacklogUsesExecutedBytes) { Init(); Warm(); @@ -236,6 +261,70 @@ TEST_F(RuntimeTextureBindingTest, PixelDmaBetweenQueuedLoadsCannotBeSkipped) { EXPECT_GT(gxState().loadedTextures[1].texDataVersion, oldVersion); } +TEST_F(RuntimeTextureBindingTest, PixelDmaRequiresRebindingEachLoadedMap) { + Init(); + for (uint32_t map : {0u, 1u, 2u}) { + Load(kObject, map); + } + decode_fifo(capture_fifo()); + const auto oldVersion = gxState().loadedTextures[0].texDataVersion; + ASSERT_NE(oldVersion, 0u); + ASSERT_EQ(gxState().loadedTextures[1].texDataVersion, oldVersion); + ASSERT_EQ(gxState().loadedTextures[2].texDataVersion, oldVersion); + + GxWorker::detail::g_active = true; + GxNotifyGuestRamDmaWrite(kPixels, 256); + ExpectFull(kObject, 0); + decode_fifo(capture_fifo()); + const auto newVersion = gxState().loadedTextures[0].texDataVersion; + ASSERT_GT(newVersion, oldVersion); + ASSERT_FALSE(g_TexObjMeta.at(kObject).needsUpload); + ASSERT_EQ(TextureBindingTest::retiredRanges.size(), 1u); + EnsureTexObjIntervalsLocked(); + + ExpectFast(kObject, 1); + auto bytes = capture_fifo(); + EXPECT_FALSE(bytes.empty()); + if (!bytes.empty()) { + decode_fifo(bytes); + } + EXPECT_EQ(gxState().loadedTextures[1].texDataVersion, newVersion); + ExpectFast(kObject, 1); + EXPECT_TRUE(capture_fifo().empty()); + + g_TexObjMeta.at(kObject).validatedLoad.Invalidate(); + ExpectFull(kObject, 2); + bytes = capture_fifo(); + EXPECT_FALSE(bytes.empty()); + if (!bytes.empty()) { + decode_fifo(bytes); + } + EXPECT_EQ(gxState().loadedTextures[2].texDataVersion, newVersion); +} + +TEST_F(RuntimeTextureBindingTest, HostReinitializationRequiresRebindingOtherMaps) { + Init(); + Load(); + Load(kObject, 1); + decode_fifo(capture_fifo()); + const auto oldId = gxState().loadedTextures[1].texObjId; + const auto oldVersion = gxState().loadedTextures[1].texDataVersion; + + Init(); + Load(); + decode_fifo(capture_fifo()); + const auto newId = gxState().loadedTextures[0].texObjId; + ASSERT_NE(newId, oldId); + ASSERT_EQ(gxState().loadedTextures[0].texDataVersion, oldVersion); + EnsureTexObjIntervalsLocked(); + + ExpectFast(kObject, 1); + const auto bytes = capture_fifo(); + ASSERT_FALSE(bytes.empty()); + decode_fifo(bytes); + EXPECT_EQ(gxState().loadedTextures[1].texObjId, newId); +} + TEST_F(RuntimeTextureBindingTest, DirtyAliasesAndAddressReuseUploadBeforeRebinding) { Init(); Warm(); @@ -272,9 +361,14 @@ TEST_F(RuntimeTextureBindingTest, InvalidationAndCopyEvictionPreserveMetadataVal GX__InvalidateTexAll_80171110(); ExpectFast(); EXPECT_FALSE(capture_fifo().empty()); - GXDestroyCopyTex(GuestToHostPtr(kPixels)); + void* copyDest = GuestToHostPtr(kPixels); + ASSERT_NE(copyDest, nullptr); + gxState().copyTextures[copyDest] = {.revision = 1}; + GXDestroyCopyTex(copyDest); + decode_fifo(capture_fifo()); + EXPECT_FALSE(gxState().copyTextures.contains(copyDest)); ExpectFast(); - EXPECT_FALSE(capture_fifo().empty()); + EXPECT_TRUE(capture_fifo().empty()); } TEST_F(RuntimeTextureBindingTest, ResetAndHostDestructionCannotReuseCompletedLoad) { @@ -289,17 +383,47 @@ TEST_F(RuntimeTextureBindingTest, ResetAndHostDestructionCannotReuseCompletedLoa EXPECT_TRUE(g_TexObjMeta.at(kObject).host->constructed); } -TEST_F(RuntimeTextureBindingTest, UnreadableSnapshotAndBadMapDoNotBecomeValidated) { +TEST_F(RuntimeTextureBindingTest, UnreadableSnapshotDoesNotBecomeValidated) { Init(); Warm(); TextureBindingTest::unreadableObject = CanonicalizeGxMainRamAddress(kObject); ExpectFull(); EXPECT_FALSE(g_TexObjMeta.at(kObject).validatedLoad.valid); TextureBindingTest::unreadableObject = 0; - Warm(); - Load(kObject, 8); - EXPECT_TRUE(capture_fifo().empty()); - ExpectFast(); + ExpectFull(); + EXPECT_TRUE(g_TexObjMeta.at(kObject).validatedLoad.valid); +} + +TEST_F(RuntimeTextureBindingTest, InvalidMapDoesNotCaptureOrValidateDescriptor) { + Init(); + for (bool defer : {false, true}) { + SCOPED_TRACE(defer); + GxWorker::detail::g_active = defer; + auto& validation = g_TexObjMeta.at(kObject).validatedLoad; + validation.Invalidate(); + Load(kObject, 8); + GxWorker::Sync(); + EXPECT_FALSE(validation.valid); + EXPECT_TRUE(capture_fifo().empty()); + + TextureBindingTest::unreadableObject = CanonicalizeGxMainRamAddress(kObject); + const auto accesses = TextureBindingTest::unreadableObjectAccesses; + Memory::Write32(kGuestGxData + 0x5FC, 0x20); + Memory::Write16(kGuestGxData + 2, 0x1234); + + Load(kObject, 8); + EXPECT_EQ(Memory::Read32(kGuestGxData + 0x5FC), 0x21u); + EXPECT_EQ(Memory::Read16(kGuestGxData + 2), 0u); + GxWorker::Sync(); + EXPECT_EQ(TextureBindingTest::unreadableObjectAccesses, accesses); + EXPECT_FALSE(validation.valid); + EXPECT_TRUE(capture_fifo().empty()); + + TextureBindingTest::unreadableObject = 0; + ExpectFull(); + EXPECT_TRUE(validation.valid); + capture_fifo(); + } } TEST_F(RuntimeTextureBindingTest, InvalidGuestDescriptorKeepsPlaceholderFallback) { diff --git a/runtime/tests/gx_worker_tests.cpp b/runtime/tests/gx_worker_tests.cpp index 5f9660238..7bf634f92 100644 --- a/runtime/tests/gx_worker_tests.cpp +++ b/runtime/tests/gx_worker_tests.cpp @@ -45,6 +45,10 @@ class Gate { std::unique_lock lock{m_mutex}; return m_cv.wait_for(lock, std::chrono::seconds{2}, [&] { return m_entered; }); } + bool WaitOpened() { + std::unique_lock lock{m_mutex}; + return m_cv.wait_for(lock, std::chrono::seconds{2}, [&] { return m_open; }); + } void Open() { std::lock_guard lock{m_mutex}; m_open = true; @@ -87,7 +91,7 @@ TEST_F(GxWorkerTest, DoesNotNotifyBeforeTheRequestedTail) { } GxWorker::Submit([&] { last.Block(); }); std::thread release{[&] { - EXPECT_TRUE(WaitUntil([] { return GxWorker::g_waitTarget.load() != GxWorker::kNotWaiting; })); + EXPECT_TRUE(WaitUntil([] { return GxWorker::g_testWaits.load() != 0; })); first.Open(); EXPECT_TRUE(last.WaitEntered()); EXPECT_EQ(GxWorker::g_testNotifications.load(), 0u); @@ -127,7 +131,14 @@ TEST_F(GxWorkerTest, ServicesCallbacksAndDrainsTheirNestedSyncAndNewWork) { } }; GxWorker::SetWaitCallback(ServiceWait); + std::thread watchdog{[&] { + if (!gate.WaitOpened()) { + ADD_FAILURE() << "The wait callback did not release the worker gate"; + gate.Open(); + } + }}; GxWorker::Sync(); + watchdog.join(); EXPECT_GE(callbacks, 1u); EXPECT_EQ(completed, 2u); EXPECT_EQ(GxWorker::g_waitTarget.load(), GxWorker::kNotWaiting); @@ -235,6 +246,7 @@ TEST(GxWorkerDeathTest, AbandonDoesNotExecuteTheFollowingRecord) { #endif TEST_F(GxWorkerTest, DISABLED_WakeBenchmark) { + constexpr uint32_t recordsPerBatch = 20000; std::array, 2> times; std::array notifications{}; std::array waits{}; @@ -248,20 +260,27 @@ TEST_F(GxWorkerTest, DISABLED_WakeBenchmark) { Gate gate; const auto start = std::chrono::steady_clock::now(); GxWorker::Submit([&] { gate.Block(); }); - for (uint32_t i = 0; i < 20000; ++i) { + for (uint32_t i = 1; i < recordsPerBatch; ++i) { GxWorker::Submit([&] { ++completed; }); } + GxWorker::Submit([&] { + ++completed; + GxWorker::g_testAcknowledgeRecord = true; + }); std::thread release{[&] { - EXPECT_TRUE(WaitUntil([] { return GxWorker::g_waitTarget.load() != GxWorker::kNotWaiting; })); + EXPECT_TRUE(WaitUntil([] { return GxWorker::g_testWaits.load() != 0; })); gate.Open(); }}; GxWorker::Sync(); + const auto end = std::chrono::steady_clock::now(); release.join(); - times[mode].push_back(std::chrono::duration_cast( - std::chrono::steady_clock::now() - start).count()); + times[mode].push_back(std::chrono::duration_cast(end - start).count()); + + // The worker accounts for notifications after publishing the tail. + ASSERT_TRUE(WaitUntil([] { return GxWorker::g_testAccountedTail.load() == GxWorker::g_write; })); notifications[mode] += GxWorker::g_testNotifications.load(); waits[mode] += GxWorker::g_testWaits.load(); - EXPECT_EQ(completed, 20000u); + EXPECT_EQ(completed, recordsPerBatch); } } for (uint32_t mode = 0; mode < 2; ++mode) { diff --git a/translator/src/Translator.Cli/Program.cs b/translator/src/Translator.Cli/Program.cs index 064db8bca..55f62f664 100644 --- a/translator/src/Translator.Cli/Program.cs +++ b/translator/src/Translator.Cli/Program.cs @@ -59,7 +59,10 @@ GuestSaveRestoreThunks.Current = GuestSaveRestoreThunks.FromFunctionMap(functionMap); } var root = project?.WorkspaceRoot ?? Directory.GetCurrentDirectory(); -var preferCachedInputs = HasFlag(tail, "--prefer-cached-inputs"); +var enabledFlags = new HashSet(StringComparer.Ordinal); +if (HandleCommandLineOptions(command, tail, enabledFlags) is { } commandLineExitCode) + return commandLineExitCode; +var preferCachedInputs = enabledFlags.Contains("--prefer-cached-inputs"); var dolFile = new Lazy(LoadDol); var relFile = new Lazy(LoadRel); var image = new Lazy(LoadImage); @@ -156,15 +159,6 @@ BaseTranslationOutputMetadata ReadBaseMetadataCached(string path) // Argument registers checked for callback function pointers at every call site var argumentRegisters = new[] { "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10" }; -// Argument validation runs before any command can execute. `--help` used to -// fall straight through to the command's parser, which ignored every argument -// it did not recognise, so `emit-build-shards --help` ran the command with its -// defaults and rewrote generated/build_shards. -if (HandleCommandLineOptions(command, tail) is { } commandLineExitCode) -{ - return commandLineExitCode; -} - return command switch { "help" or "--help" or "-h" or "-?" => ShowGlobalHelp(), @@ -460,12 +454,13 @@ int RunTranslateRecursive(string[] argsTail) var outDir = OptionValue(argsTail, "--outdir") ?? RequireProject().Output.Functions; var outputMetadataPath = OptionValue(argsTail, "--output-metadata"); var productionSourceBundlePath = OptionValue(argsTail, "--production-source-bundle"); - var emitFunctionFiles = !HasFlag(argsTail, "--no-function-files"); - var boundedIr = HasFlag(argsTail, "--bounded-ir"); + var emitFunctionFiles = !enabledFlags.Contains("--no-function-files"); + var boundedIr = enabledFlags.Contains("--bounded-ir"); if (!emitFunctionFiles && string.IsNullOrWhiteSpace(productionSourceBundlePath)) throw new ArgumentException("--no-function-files requires --production-source-bundle."); BaseTranslationOutputMetadata? previousOutputMetadata = null; - if (HasFlag(argsTail, "--prune-stale") && + var pruneStale = enabledFlags.Contains("--prune-stale"); + if (pruneStale && outputMetadataPath is not null && File.Exists(outputMetadataPath)) { @@ -479,7 +474,6 @@ outputMetadataPath is not null && $"[translator] Warning: existing output metadata could not be read; stale pruning will use one-time tree migration: {ex.Message}"); } } - var pruneStale = HasFlag(argsTail, "--prune-stale"); var threadsOption = OptionValue(argsTail, "--threads"); var maxThreads = threadsOption != null ? Math.Max(1, ParseInt(threadsOption)) : Math.Max(1, Environment.ProcessorCount); Console.WriteLine($"[translator] Using up to {maxThreads} worker thread(s)."); @@ -1347,10 +1341,10 @@ static string StateMaskSortKey(GuestStateMask mask) => .Take(4) .ToArray(); var variantIndex = 0; + var targetFunction = canonicalGuestFunctions[target]; foreach (var outputGroup in rankedOutputs) { var output = outputGroup.Key; - var targetFunction = canonicalGuestFunctions[target]; var entryLive = GuestStateLivenessAnalyzer.Analyze( targetFunction, stateFreeLivenessContracts, output).BlockLiveIn[targetFunction.EntryLabel]; var publicContract = guestAbiContracts[target]; @@ -1774,7 +1768,7 @@ void LogModPhase(string label) $"[translator] Warning: no base translation output metadata at {baseTranslationOutputMetadataPath}; " + "cannot verify the base translation is aware of this mod's patch set."); } - var skipRetroWfc = HasFlag(argsTail, "--skip-retro-wfc"); + var skipRetroWfc = enabledFlags.Contains("--skip-retro-wfc"); var explicitRetroWfcPayloadSpec = OptionValue(argsTail, "--retro-wfc-payload"); var retroWfcPayloadSpec = skipRetroWfc ? null @@ -1975,7 +1969,7 @@ void LogModPhase(string label) .OrderBy(s => s.Address) .ToList(); var cppFailureCount = 0; - var emitCpp = HasFlag(argsTail, "--emit-cpp"); + var emitCpp = enabledFlags.Contains("--emit-cpp"); var maxThreads = OptionValue(argsTail, "--threads") is { } threadsText ? Math.Max(1, ParseInt(threadsText)) : Math.Max(1, Environment.ProcessorCount); @@ -3788,7 +3782,7 @@ static string CommandUsage(string command) /// command's parser would discard), null otherwise. Only inspects option-like tokens; positional /// addresses, paths and passthrough values are never rejected. /// -static int? HandleCommandLineOptions(string command, string[] argsTail) +static int? HandleCommandLineOptions(string command, string[] argsTail, HashSet enabledFlags) { if (CommandSpec(command) is not { } spec) { @@ -3820,7 +3814,12 @@ static string CommandUsage(string command) index++; continue; } - if (flags.Contains(token) || !token.StartsWith('-')) + if (flags.Contains(token)) + { + enabledFlags.Add(token); + continue; + } + if (!token.StartsWith('-')) { continue; } @@ -3971,8 +3970,6 @@ static int ParseInt(string text) => return (null, values); } -static bool HasFlag(string[] argsTail, string name) => argsTail.Any(a => string.Equals(a, name, StringComparison.Ordinal)); - /// /// Addresses a leaf splice must never cover: the decoded bytes aren't what the runtime /// executes there (native registration/fatal stub, exclusion, or a mod overlay rewrite). diff --git a/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs b/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs index b707683e8..6a730d336 100644 --- a/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs +++ b/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs @@ -5,6 +5,8 @@ namespace Translator.Core.Translation; /// /// Fixed keys over canonical SSA decoded on demand without caching bodies. +/// Keys, Count and ContainsKey describe the address snapshot. Enumeration requires +/// the provider to retain every body in that snapshot for the duration of the read. /// Concurrent readers require a thread-safe provider, such as CanonicalIrStore. /// public sealed class CanonicalIrDictionary : IReadOnlyDictionary diff --git a/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs b/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs index 982495af5..acaa2f6cd 100644 --- a/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs +++ b/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs @@ -1,4 +1,5 @@ using System.Reflection; +using System.Diagnostics; using System.Threading.Tasks; using Translator.Cli.Configuration; using Translator.Core.Mods; @@ -73,6 +74,25 @@ public void BoundedIrMatchesBufferedFilesMetadataAndBundleAcrossThreadCounts() var buffered = Translate("buffered", 1, bounded: false); Assert.Equal(2, buffered.Sources.Count); + + var optionPath = new ProcessStartInfo("dotnet") + { + WorkingDirectory = root, + ArgumentList = { typeof(TranslationProjectConfig).Assembly.Location, "translate-recursive", + $"0x{entry:X8}", "--project", projectPath, "--outdir", "--no-function-files", "--bounded-ir", + "--threads", "1" } + }; + using (var process = Process.Start(optionPath)!) + { + if (!process.WaitForExit(30_000)) + { + process.Kill(entireProcessTree: true); + process.WaitForExit(); + throw new TimeoutException("Translation with an option-shaped output path timed out."); + } + Assert.Equal(0, process.ExitCode); + Assert.Equal(2, Directory.GetFiles(Path.Combine(root, "--no-function-files"), "*.cpp").Length); + } foreach (var threads in new[] { 1, 3 }) { var lazy = Translate($"lazy-{threads}", threads, bounded: true); From be4dc84d6f452bae3e031e262b94f0a2f7c76db7 Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:13:01 +0100 Subject: [PATCH 12/17] handle literal paths in package checks --- Launcher/Prepare-NativePrebuilt.sh | 19 +++---------- Launcher/Test-AuroraPrebuilt.sh | 7 +++-- Launcher/build-cache.sh | 14 +++++++++ Launcher/local-build.sh | 4 ++- aurora-main | 2 +- runtime/cmake/AuroraFingerprint.cmake | 41 +++------------------------ 6 files changed, 31 insertions(+), 56 deletions(-) diff --git a/Launcher/Prepare-NativePrebuilt.sh b/Launcher/Prepare-NativePrebuilt.sh index 5dfc97871..6dd81358f 100755 --- a/Launcher/Prepare-NativePrebuilt.sh +++ b/Launcher/Prepare-NativePrebuilt.sh @@ -20,6 +20,7 @@ set -euo pipefail script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) workspace=$(cd "$script_dir/.." && pwd) +source "$script_dir/build-cache.sh" arch="" output_dir="" @@ -129,19 +130,7 @@ assert_file "$clang_binary" "Portable clang driver binary" (( parallel > 0 )) || parallel=$(nproc) -fingerprint_tree() { - # $1 = root dir, remaining args = top-level subdirectory names to exclude - local root=$1; shift - root=$(normalize "$root") - [[ -d "$root" ]] || return 0 - local find_args=("$root" -name .git -prune -o) - local ex - for ex in "$@"; do find_args+=(-path "$root/$ex" -prune -o); done - find_args+=(-type f -print) - find "${find_args[@]}" | LC_ALL=C sort | while IFS= read -r abs; do - printf '%s %s\n' "${abs#$root/}" "$(sha256_of "$abs")" - done | sha256sum | awk '{print $1}' -} + # The fixed (path-independent) half of the configure command line - identical for every # harvest, which is what lets one package be valid regardless of where it was built. @@ -191,11 +180,11 @@ if [[ -n "$sysroot" ]]; then fi flag_fingerprint=$(printf '%s\n' "${fixed_configure_flags[@]}" | sha256sum | awk '{print $1}') -aurora_fingerprint=$("$cmake_bin" "-DAURORA_DIR=$aurora_source" -P "$runtime_source/cmake/AuroraFingerprint.cmake") +aurora_fingerprint=$(mkw_fingerprint_tree "$aurora_source" extern build) [[ -n "$aurora_fingerprint" ]] || fail "The aurora source tree could not be fingerprinted: $aurora_source" # The harvested Crypto++ archive is consumed against this tree's headers, so it is fingerprinted. -third_party_fingerprint=$(fingerprint_tree "$runtime_source/third_party") +third_party_fingerprint=$(mkw_fingerprint_tree "$runtime_source/third_party") [[ -n "$third_party_fingerprint" ]] || fail "The vendored third-party tree could not be fingerprinted: $runtime_source/third_party" mbedtls_fingerprint=$(sha256_of "$runtime_source/cmake/MbedTLSPin.cmake") diff --git a/Launcher/Test-AuroraPrebuilt.sh b/Launcher/Test-AuroraPrebuilt.sh index de2adc463..7e29b77c4 100755 --- a/Launcher/Test-AuroraPrebuilt.sh +++ b/Launcher/Test-AuroraPrebuilt.sh @@ -35,9 +35,12 @@ expect_error() { expect_error 'Native prebuilt provenance is missing' printf '{"AuroraSourceFingerprint":"%s"}\n' "$fingerprint" > package/provenance.json expect_error 'Extracted main.dol' -printf 'edited\n' >> 'workspace/aurora-main/include/a header.h' +mv workspace 'workspace[old]' +args[1]='workspace[old]' +expect_error 'Extracted main.dol' +printf 'edited\n' >> 'workspace[old]/aurora-main/include/a header.h' expect_error 'does not match the Aurora sources' -printf 'header\n' > 'workspace/aurora-main/include/a header.h' +printf 'header\n' > 'workspace[old]/aurora-main/include/a header.h' for invalid in '{}' 'null' 'not json' '{"AuroraSourceFingerprint":false}' \ "{\"nested\":{\"AuroraSourceFingerprint\":\"$fingerprint\"}}"; do printf '%s\n' "$invalid" > package/provenance.json diff --git a/Launcher/build-cache.sh b/Launcher/build-cache.sh index 55cc070a9..431b735b9 100644 --- a/Launcher/build-cache.sh +++ b/Launcher/build-cache.sh @@ -14,6 +14,20 @@ mkw_sha256() { "${mkw_sha256_cmd[@]}" "$@" | awk '{print $1}' } +# Linux package provenance uses sorted relative paths followed by their file digests. +mkw_fingerprint_tree() ( + set -o pipefail + cd -- "$1" || exit 1 + shift + local args=(. -name .git -prune -o) dir record + for dir in "$@"; do args+=(-path "./$dir" -prune -o); done + find "${args[@]}" -type f -print0 | LC_ALL=C sort -z | xargs -0 -r sha256sum -z -- | + while IFS= read -r -d '' record; do + # sha256sum -z emits the digest, two spaces, then the ./ path. + printf '%s %s\n' "${record:68}" "${record:0:64}" + done | mkw_sha256 +) + # Prints " " for every file under each directory, relative to $1. mkw_digest_tree() { local root=$1; shift diff --git a/Launcher/local-build.sh b/Launcher/local-build.sh index 6efbd6cfe..326e04748 100755 --- a/Launcher/local-build.sh +++ b/Launcher/local-build.sh @@ -203,7 +203,9 @@ if [[ -n "$native_prebuilt_dir" ]]; then assert_file "$native_prebuilt_dir/native_prebuilt.cmake" "Native prebuilt package" assert_file "$native_prebuilt_dir/provenance.json" "Native prebuilt provenance" native_prebuilt_dir=$(cd "$native_prebuilt_dir" && pwd) - "$cmake_bin" "-DAURORA_DIR=$workspace/aurora-main" "-DPROVENANCE_FILE=$native_prebuilt_dir/provenance.json" \ + assert_file "$workspace/aurora-main/CMakeLists.txt" "Aurora source tree" + aurora_fingerprint=$(mkw_fingerprint_tree "$workspace/aurora-main" extern build) + "$cmake_bin" "-DAURORA_FINGERPRINT=$aurora_fingerprint" "-DPROVENANCE_FILE=$native_prebuilt_dir/provenance.json" \ -P "$script_dir/../runtime/cmake/AuroraFingerprint.cmake" fi diff --git a/aurora-main b/aurora-main index fc09f2ead..1a67fa419 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit fc09f2ead0df64621346ca5c5c745d8b24b089f7 +Subproject commit 1a67fa419c7ced727a8f4523471d7df18ed610d1 diff --git a/runtime/cmake/AuroraFingerprint.cmake b/runtime/cmake/AuroraFingerprint.cmake index c12bb115c..c02832e37 100644 --- a/runtime/cmake/AuroraFingerprint.cmake +++ b/runtime/cmake/AuroraFingerprint.cmake @@ -1,40 +1,7 @@ cmake_minimum_required(VERSION 3.20) -if(NOT EXISTS "${AURORA_DIR}/CMakeLists.txt") - message(FATAL_ERROR "Aurora source tree is missing: ${AURORA_DIR}") -endif() - -# Keep the Linux package manifest independent of the checkout path. -file(GLOB _entries RELATIVE "${AURORA_DIR}" "${AURORA_DIR}/*") -list(REMOVE_ITEM _entries ".git" "extern" "build") -set(_files "") -foreach(_entry IN LISTS _entries) - if(IS_SYMLINK "${AURORA_DIR}/${_entry}") - continue() - elseif(IS_DIRECTORY "${AURORA_DIR}/${_entry}") - file(GLOB_RECURSE _children LIST_DIRECTORIES false RELATIVE "${AURORA_DIR}" "${AURORA_DIR}/${_entry}/*") - list(APPEND _files ${_children}) - else() - list(APPEND _files "${_entry}") - endif() -endforeach() -list(FILTER _files EXCLUDE REGEX "(^|/)\\.git(/|$)") -list(SORT _files) -set(_manifest "") -foreach(_file IN LISTS _files) - if(NOT IS_SYMLINK "${AURORA_DIR}/${_file}") - file(SHA256 "${AURORA_DIR}/${_file}" _hash) - string(APPEND _manifest "${_file} ${_hash}\n") - endif() -endforeach() -string(SHA256 _fingerprint "${_manifest}") - -if(DEFINED PROVENANCE_FILE) - file(READ "${PROVENANCE_FILE}" _provenance) - string(JSON _recorded ERROR_VARIABLE _error GET "${_provenance}" AuroraSourceFingerprint) - if(_error OR NOT _recorded STREQUAL _fingerprint) - message(FATAL_ERROR "Native prebuilt package does not match the Aurora sources. Regenerate it with Prepare-NativePrebuilt.sh or omit --native-prebuilt-dir.") - endif() -else() - execute_process(COMMAND "${CMAKE_COMMAND}" -E echo "${_fingerprint}" COMMAND_ERROR_IS_FATAL ANY) +file(READ "${PROVENANCE_FILE}" _provenance) +string(JSON _recorded ERROR_VARIABLE _error GET "${_provenance}" AuroraSourceFingerprint) +if(_error OR NOT _recorded STREQUAL AURORA_FINGERPRINT) + message(FATAL_ERROR "Native prebuilt package does not match the Aurora sources. Regenerate it with Prepare-NativePrebuilt.sh or omit --native-prebuilt-dir.") endif() From f04649a89f14789143e2aab2264022a417b14221 Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:13:31 +0100 Subject: [PATCH 13/17] run audio tests on supported cpus --- aurora-main | 2 +- runtime/cmake/PlatformTests.cmake | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/aurora-main b/aurora-main index 61fdd3584..6315f3dae 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit 61fdd3584e1f4adf8541a3537cb7e8e04c184746 +Subproject commit 6315f3dae3554752ee696180f04ed747e30edc78 diff --git a/runtime/cmake/PlatformTests.cmake b/runtime/cmake/PlatformTests.cmake index 5e634d23f..31247e576 100644 --- a/runtime/cmake/PlatformTests.cmake +++ b/runtime/cmake/PlatformTests.cmake @@ -213,18 +213,21 @@ if(TARGET gx_fifo_tests) add_test(NAME mkw_gx_dl_cache_tests COMMAND mkw_gx_dl_cache_tests) endif() -add_executable(mkw_ax_mix_kernels_tests "${MKW_TEST_RUNTIME_DIR}/tests/ax_mix_kernels_tests.cpp") -target_include_directories(mkw_ax_mix_kernels_tests PRIVATE - "${MKW_TEST_RUNTIME_DIR}/include" - "${MKW_TEST_RUNTIME_DIR}/src/hle/audio") -target_compile_features(mkw_ax_mix_kernels_tests PRIVATE cxx_std_17) +function(mkw_add_ax_mix_test target) + add_executable(${target} "${MKW_TEST_RUNTIME_DIR}/tests/ax_mix_kernels_tests.cpp") + target_include_directories(${target} PRIVATE + "${MKW_TEST_RUNTIME_DIR}/include" "${MKW_TEST_RUNTIME_DIR}/src/hle/audio") + target_compile_features(${target} PRIVATE cxx_std_17) + set_target_properties(${target} PROPERTIES UNITY_BUILD OFF) + add_test(NAME ${target} COMMAND ${target}) +endfunction() + +mkw_add_ax_mix_test(mkw_ax_mix_kernels_tests) if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|ARM64)$") target_compile_definitions(mkw_ax_mix_kernels_tests PRIVATE MKW_AX_MIX_REQUIRE_NEON=1) elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|amd64|x86_64|X86_64)$") - target_compile_options(mkw_ax_mix_kernels_tests PRIVATE -march=x86-64-${MKW_X86_CPU_PROFILE}) + target_compile_options(mkw_ax_mix_kernels_tests PRIVATE -march=x86-64-v2) endif() -set_target_properties(mkw_ax_mix_kernels_tests PROPERTIES UNITY_BUILD OFF) -add_test(NAME mkw_ax_mix_kernels_tests COMMAND mkw_ax_mix_kernels_tests) add_executable(mkw_fiber_manager_tests "${MKW_TEST_RUNTIME_DIR}/tests/fiber_manager_tests.cpp" @@ -357,6 +360,11 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|amd64|x86_64|X86_64)$") ]=] MKW_HOST_SUPPORTS_X86_V3) cmake_pop_check_state() endif() + if((NOT CMAKE_CROSSCOMPILING OR CMAKE_CROSSCOMPILING_EMULATOR) AND MKW_HOST_SUPPORTS_X86_V3) + mkw_add_ax_mix_test(mkw_ax_mix_kernels_v3_tests) + target_compile_options(mkw_ax_mix_kernels_v3_tests PRIVATE -march=x86-64-v3) + endif() + # Compile the same oracle cases for both paths. The v2 binary proves the # scalar fallback stays free of FMA; the v3 binary checks the existing # vector intrinsic path against the same strict result. From 03fa7c60663da487f159124a15e14d945ac5680a Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:32:32 +0100 Subject: [PATCH 14/17] match linux package sources --- Launcher/Prepare-NativePrebuilt.sh | 1 + Launcher/Test-AuroraPrebuilt.sh | 11 +++++++++++ Launcher/build-cache.sh | 19 +++++++++++++------ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Launcher/Prepare-NativePrebuilt.sh b/Launcher/Prepare-NativePrebuilt.sh index 6dd81358f..94f1f9bb5 100755 --- a/Launcher/Prepare-NativePrebuilt.sh +++ b/Launcher/Prepare-NativePrebuilt.sh @@ -233,6 +233,7 @@ echo "Prepare-NativePrebuilt.sh: configuring the aurora/third-party staging buil -DCMAKE_C_COMPILER="$cc" -DCMAKE_CXX_COMPILER="$cxx" \ -DCMAKE_MAKE_PROGRAM="$ninja_bin" \ -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" \ + -DMKW_AURORA_DIR="$aurora_source" \ -DMKW_NATIVE_PREBUILT_EXPORT_DIR="$export_dir" meta_path="$export_dir/meta.txt" diff --git a/Launcher/Test-AuroraPrebuilt.sh b/Launcher/Test-AuroraPrebuilt.sh index 7e29b77c4..eed15a28c 100755 --- a/Launcher/Test-AuroraPrebuilt.sh +++ b/Launcher/Test-AuroraPrebuilt.sh @@ -18,6 +18,17 @@ fingerprint=$( mkdir -p .git extern build touch .git/config extern/dependency build/object cd ../.. +if command -v shasum >/dev/null 2>&1; then + mkdir fallback-tools + for tool in shasum awk find sort xargs; do + ln -s "$(command -v "$tool")" "fallback-tools/$tool" + done + ( + PATH="$PWD/fallback-tools" + source "$launcher/build-cache.sh" + [[ $(mkw_fingerprint_tree workspace/aurora-main extern build) == "$fingerprint" ]] + ) +fi touch package/native_prebuilt.cmake args=(--workspace workspace --output-dir output --translator-bin unused --cc true --cxx true --ninja true --x86-cpu-profile v2 --native-prebuilt-dir package) diff --git a/Launcher/build-cache.sh b/Launcher/build-cache.sh index 431b735b9..30e7014c4 100644 --- a/Launcher/build-cache.sh +++ b/Launcher/build-cache.sh @@ -19,13 +19,20 @@ mkw_fingerprint_tree() ( set -o pipefail cd -- "$1" || exit 1 shift - local args=(. -name .git -prune -o) dir record + local args=(. -name .git -prune -o) dir record digest for dir in "$@"; do args+=(-path "./$dir" -prune -o); done - find "${args[@]}" -type f -print0 | LC_ALL=C sort -z | xargs -0 -r sha256sum -z -- | - while IFS= read -r -d '' record; do - # sha256sum -z emits the digest, two spaces, then the ./ path. - printf '%s %s\n' "${record:68}" "${record:0:64}" - done | mkw_sha256 + find "${args[@]}" -type f -print0 | LC_ALL=C sort -z | + if [[ ${mkw_sha256_cmd[0]} == sha256sum ]]; then + xargs -0 -r sha256sum -z -- | while IFS= read -r -d '' record; do + # sha256sum -z emits the digest, two spaces, then the ./ path. + printf '%s %s\n' "${record:68}" "${record:0:64}" + done + else + while IFS= read -r -d '' record; do + digest=$(mkw_sha256 < "$record") || exit 1 + printf '%s %s\n' "${record#./}" "$digest" + done + fi | mkw_sha256 ) # Prints " " for every file under each directory, relative to $1. From df86d5c93d04ac5413f73e5bbb30b8cba9d8233a Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:35:12 +0100 Subject: [PATCH 15/17] fix translator options and gx recovery --- Launcher/macos/macos-x86_64-toolchain.cmake | 9 + runtime/cmake/PlatformTests.cmake | 4 +- runtime/src/hle/gx/gx_dl.cpp | 5 + runtime/tests/ax_mix_kernels_tests.cpp | 7 +- runtime/tests/gx_copy_tests.cpp | 5 +- runtime/tests/gx_direct_draw_tests.cpp | 5 + runtime/tests/gx_texture_binding_tests.cpp | 39 ++- runtime/tests/gx_worker_tests.cpp | 51 +++- translator/src/Translator.Cli/Program.cs | 247 ++++++++---------- .../GuestAbiInterproceduralAnalyzer.cs | 38 ++- .../Translation/CanonicalIrDictionary.cs | 21 +- .../CanonicalIrDictionaryTests.cs | 46 +++- .../TranslateRecursiveRuntimeConfigTests.cs | 81 ++++-- 13 files changed, 360 insertions(+), 198 deletions(-) diff --git a/Launcher/macos/macos-x86_64-toolchain.cmake b/Launcher/macos/macos-x86_64-toolchain.cmake index f36749827..7ce5f7737 100644 --- a/Launcher/macos/macos-x86_64-toolchain.cmake +++ b/Launcher/macos/macos-x86_64-toolchain.cmake @@ -9,3 +9,12 @@ set(CMAKE_SYSTEM_NAME Darwin) set(CMAKE_SYSTEM_PROCESSOR x86_64) set(CMAKE_OSX_ARCHITECTURES x86_64 CACHE STRING "Target macOS architectures" FORCE) + +if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin" AND NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR + AND NOT DEFINED ENV{CMAKE_CROSSCOMPILING_EMULATOR}) + execute_process(COMMAND /usr/bin/arch -x86_64 /usr/bin/true + RESULT_VARIABLE _mkw_can_run_x86_64 OUTPUT_QUIET ERROR_QUIET) + if(_mkw_can_run_x86_64 EQUAL 0) + set(CMAKE_CROSSCOMPILING_EMULATOR /usr/bin/arch -x86_64) + endif() +endif() diff --git a/runtime/cmake/PlatformTests.cmake b/runtime/cmake/PlatformTests.cmake index 31247e576..52fac38a4 100644 --- a/runtime/cmake/PlatformTests.cmake +++ b/runtime/cmake/PlatformTests.cmake @@ -219,7 +219,9 @@ function(mkw_add_ax_mix_test target) "${MKW_TEST_RUNTIME_DIR}/include" "${MKW_TEST_RUNTIME_DIR}/src/hle/audio") target_compile_features(${target} PRIVATE cxx_std_17) set_target_properties(${target} PROPERTIES UNITY_BUILD OFF) - add_test(NAME ${target} COMMAND ${target}) + if(NOT CMAKE_CROSSCOMPILING OR CMAKE_CROSSCOMPILING_EMULATOR) + add_test(NAME ${target} COMMAND ${target}) + endif() endfunction() mkw_add_ax_mix_test(mkw_ax_mix_kernels_tests) diff --git a/runtime/src/hle/gx/gx_dl.cpp b/runtime/src/hle/gx/gx_dl.cpp index bf47a7882..dc747106c 100644 --- a/runtime/src/hle/gx/gx_dl.cpp +++ b/runtime/src/hle/gx/gx_dl.cpp @@ -1399,6 +1399,11 @@ extern "C" void GX__CallDisplayList_80172f64(uint32_t listAddr, uint32_t nbytes) if (haveStateBeforeScan) { RestoreGxVertexState(stateBeforeScan); } + if (!GxWorker::OnWorker() && !g_auroraFrameActive.load(std::memory_order_acquire)) { + EnsureAuroraFrameForFifo(); + // A frame wait may have run another display list on this thread. + bytes = GxDisplayListScanCache::DisplayListBytes{guestList, nbytes}; + } GXMarkFrameWork(); InvalidateAppliedAuroraState(); ParseDisplayList(bytes.Data(), nbytes); diff --git a/runtime/tests/ax_mix_kernels_tests.cpp b/runtime/tests/ax_mix_kernels_tests.cpp index c6a9bfe49..8909eb5a0 100644 --- a/runtime/tests/ax_mix_kernels_tests.cpp +++ b/runtime/tests/ax_mix_kernels_tests.cpp @@ -216,8 +216,11 @@ void TestBackend(const Backend& backend) { for (unsigned pattern = 0; pattern < 8; ++pattern) TestAccum(backend, count, offset, pattern); } for (unsigned trial = 0; trial < 512; ++trial) { - TestRamps(backend, NextRandom() % 162, 1 + NextRandom() % 3, - static_cast(NextRandom()), static_cast(NextRandom()), 1); + const size_t count = NextRandom() % 162; + const size_t offset = 1 + NextRandom() % 3; + const auto volume = static_cast(NextRandom()); + const auto delta = static_cast(NextRandom()); + TestRamps(backend, count, offset, volume, delta, 1); } TestRamps(backend, 65539, 1, 0xffff, 0xffff, 0); TestAccumEdges(backend); diff --git a/runtime/tests/gx_copy_tests.cpp b/runtime/tests/gx_copy_tests.cpp index 40ae458e3..3b18cb063 100644 --- a/runtime/tests/gx_copy_tests.cpp +++ b/runtime/tests/gx_copy_tests.cpp @@ -53,7 +53,7 @@ void* GuestToHostPtr(uint32_t address, size_t length) { length > g_guestBytes.size() - (canonical - base)) { return nullptr; } - return g_guestBytes.data() + canonical - base; + return g_guestBytes.data() + (canonical - base); } void EnsureAuroraFrameActive() {} void VI_HLE_SetXfbReady(uint32_t address) { g_readyXfb = address; } @@ -257,7 +257,8 @@ TEST_P(GxCopyTest, DisplayCopyRetainsPresentationAndInlineRenderWorkerJoin) { GXSetDispCopyDst(640, 480); }); GX__CopyDisp_8016fc38(kGuestBase, GX_TRUE); - EXPECT_EQ(stub::copyObservations.size(), 1u); + ASSERT_EQ(stub::copyObservations.size(), 1u); + EXPECT_EQ(stub::copyObservations.front().draws, 1u); EXPECT_EQ(g_readyXfb, kGuestBase); EXPECT_EQ(g_presents, 1u); EXPECT_EQ(g_overlayDraws, 1u); diff --git a/runtime/tests/gx_direct_draw_tests.cpp b/runtime/tests/gx_direct_draw_tests.cpp index 6baa6214c..259a64993 100644 --- a/runtime/tests/gx_direct_draw_tests.cpp +++ b/runtime/tests/gx_direct_draw_tests.cpp @@ -54,7 +54,11 @@ class RuntimeDirectDrawTest : public GXFifoTest { for (; pos + width <= bytes.size(); pos += width) { const u32 value = width == 4 ? BigEndian::Read32(bytes.data() + pos) : width == 2 ? BigEndian::Read16(bytes.data() + pos) : bytes[pos]; + const auto remaining = g_hleGxState.fifoDirectBytesRemaining; HleFifoWrite(value, width); + if (pos == 0 && remaining >= width) { + EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, remaining - width); + } } for (; pos < bytes.size(); ++pos) { HleFifoWrite(bytes[pos], 1); @@ -89,6 +93,7 @@ TEST_F(RuntimeDirectDrawTest, DirectNumericPayloadMatchesIncrementalForEveryWrit ASSERT_EQ(g_hleGxState.fifoDirectBytesRemaining, payload.size()); write(payload, width); EXPECT_EQ(capture_fifo(), reference) << "type=" << type << " count=" << cnt << " width=" << width; + EXPECT_EQ(g_hleGxState.fifoDirectBytesRemaining, 0u); EXPECT_FALSE(g_hleGxState.inBegin); EXPECT_EQ(g_hleGxState.vertsRemaining, 0u); } diff --git a/runtime/tests/gx_texture_binding_tests.cpp b/runtime/tests/gx_texture_binding_tests.cpp index c85f4295d..c5d2d2872 100644 --- a/runtime/tests/gx_texture_binding_tests.cpp +++ b/runtime/tests/gx_texture_binding_tests.cpp @@ -144,6 +144,10 @@ TEST_F(RuntimeTextureBindingTest, EverySnapshotByteParticipatesInValidation) { Warm(); const auto original = CaptureTexObjBytes(kObject); for (uint32_t offset = 0; offset < original.size(); ++offset) { + { + TexObjSnapshotScope before(kObject, original); + ASSERT_NE(TryGetValidatedTexObjSlot(kObject), nullptr) << offset; + } const uint8_t changed = original[offset] ^ 0x80; Memory::Write8(kObject + offset, changed); const auto snapshot = CaptureTexObjBytes(kObject); @@ -357,10 +361,18 @@ TEST_F(RuntimeTextureBindingTest, PaletteFormatsAlwaysKeepFullPathAcrossTlutChan TEST_F(RuntimeTextureBindingTest, InvalidationAndCopyEvictionPreserveMetadataValidation) { Init(); - Warm(); + Load(); + EnsureTexObjIntervalsLocked(); + auto bind = capture_fifo(); + ASSERT_FALSE(bind.empty()); + decode_fifo(bind); + ASSERT_EQ(gxState().loadedTextures[0].data, GuestToHostPtr(kPixels)); GX__InvalidateTexAll_80171110(); + auto invalidate = capture_fifo(); + ASSERT_FALSE(invalidate.empty()); + decode_fifo(invalidate); ExpectFast(); - EXPECT_FALSE(capture_fifo().empty()); + EXPECT_TRUE(capture_fifo().empty()); void* copyDest = GuestToHostPtr(kPixels); ASSERT_NE(copyDest, nullptr); gxState().copyTextures[copyDest] = {.revision = 1}; @@ -427,14 +439,31 @@ TEST_F(RuntimeTextureBindingTest, InvalidMapDoesNotCaptureOrValidateDescriptor) } TEST_F(RuntimeTextureBindingTest, InvalidGuestDescriptorKeepsPlaceholderFallback) { - WriteGuestTexObj(kObject, kPixels, 8, 8, GX_CTF_R4, GX_CLAMP, GX_CLAMP, false, false, 0); + Init(); Load(); + EnsureTexObjIntervalsLocked(); + decode_fifo(capture_fifo()); + auto& loaded = gxState().loadedTextures[0]; + ASSERT_EQ(loaded.data, GuestToHostPtr(kPixels)); + const auto validId = loaded.texObjId; + + WriteGuestTexObj(kOtherObject, kOtherPixels, 8, 8, GX_CTF_R4, GX_CLAMP, GX_CLAMP, false, false, 0); + Load(kOtherObject); auto bytes = capture_fifo(); ASSERT_FALSE(bytes.empty()); + decode_fifo(bytes); + EXPECT_EQ(loaded.width(), 1u); + EXPECT_EQ(loaded.height(), 1u); + EXPECT_NE(loaded.texObjId, validId); EXPECT_EQ(g_boundTexMaps[0].objAddr, 0u); - Init(); - Warm(); + + EnsureTexObjIntervalsLocked(); ExpectFast(); + bytes = capture_fifo(); + ASSERT_FALSE(bytes.empty()); + decode_fifo(bytes); + EXPECT_EQ(loaded.texObjId, validId); + EXPECT_EQ(loaded.data, GuestToHostPtr(kPixels)); } TEST_F(RuntimeTextureBindingTest, DISABLED_BenchmarkRepeatedLoads) { diff --git a/runtime/tests/gx_worker_tests.cpp b/runtime/tests/gx_worker_tests.cpp index 7bf634f92..4c7d4cdbd 100644 --- a/runtime/tests/gx_worker_tests.cpp +++ b/runtime/tests/gx_worker_tests.cpp @@ -104,6 +104,29 @@ TEST_F(GxWorkerTest, DoesNotNotifyBeforeTheRequestedTail) { EXPECT_EQ(GxWorker::g_waitTarget.load(), GxWorker::kNotWaiting); } +TEST_F(GxWorkerTest, ReachingTargetNotifiesTheBlockedProducer) { + Gate gate; + GxWorker::Submit([&] { gate.Block(); }); + GxWorker::Publish(); + if (!gate.WaitEntered()) { + gate.Open(); + GxWorker::Sync(); + FAIL() << "The worker did not enter the gate"; + } + std::unique_lock lock{GxWorker::g_tailMutex}; + GxWorker::g_waitTarget.store(GxWorker::g_write); + gate.Open(); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds{2}; + while (GxWorker::g_testNotifications.load() == 0) { + if (GxWorker::g_tailCv.wait_until(lock, deadline) == std::cv_status::timeout) { + ADD_FAILURE() << "The worker did not signal the target"; + break; + } + } + EXPECT_EQ(GxWorker::g_tail.load(), GxWorker::g_write); + GxWorker::g_waitTarget.store(GxWorker::kNotWaiting); +} + TEST_F(GxWorkerTest, RechecksCompletionAfterPublishingTheWaitTarget) { Gate gate; GxWorker::Submit([&] { gate.Block(); }); @@ -118,16 +141,19 @@ TEST_F(GxWorkerTest, RechecksCompletionAfterPublishingTheWaitTarget) { EXPECT_EQ(GxWorker::g_waitTarget.load(), GxWorker::kNotWaiting); } -TEST_F(GxWorkerTest, ServicesCallbacksAndDrainsTheirNestedSyncAndNewWork) { +void CheckWorkSubmittedByCallback(bool syncInsideCallback) { Gate gate; uint32_t callbacks = 0; - uint32_t completed = 0; + std::atomic_uint32_t completed{0}; GxWorker::Submit([&] { gate.Block(); ++completed; }); g_service = [&] { if (++callbacks == 1) { GxWorker::Submit([&] { ++completed; }); gate.Open(); - GxWorker::Sync(); + if (syncInsideCallback) { + GxWorker::Sync(); + EXPECT_EQ(completed.load(), 2u); + } } }; GxWorker::SetWaitCallback(ServiceWait); @@ -140,10 +166,21 @@ TEST_F(GxWorkerTest, ServicesCallbacksAndDrainsTheirNestedSyncAndNewWork) { GxWorker::Sync(); watchdog.join(); EXPECT_GE(callbacks, 1u); - EXPECT_EQ(completed, 2u); + EXPECT_EQ(completed.load(), 2u); + GxWorker::SetWaitCallback(nullptr); + GxWorker::Publish(); + GxWorker::Sync(); EXPECT_EQ(GxWorker::g_waitTarget.load(), GxWorker::kNotWaiting); } +TEST_F(GxWorkerTest, NestedSyncDrainsWorkBeforeReturningToCallback) { + CheckWorkSubmittedByCallback(true); +} + +TEST_F(GxWorkerTest, OuterSyncDrainsWorkSubmittedByCallback) { + CheckWorkSubmittedByCallback(false); +} + TEST_F(GxWorkerTest, RingBackpressureAndWrapPreserveOrderWithoutServicingGuestCallbacks) { Gate gate; uint32_t callbacks = 0; @@ -218,6 +255,12 @@ TEST_F(GxWorkerTest, ThrowingCommandsStillCompleteTheirRecordAndReleaseTheirPayl TEST(GxWorkerDeathTest, AbandonWakesTheIdleConsumerAndSignalsExit) { ASSERT_EXIT({ GxWorker::Start(); + if (!WaitUntil([] { return GxWorker::g_consumerWaiting.load(); })) { + std::_Exit(2); + } + { + std::lock_guard lock{GxWorker::g_headMutex}; + } GxWorker::Abandon(); std::_Exit(!GxWorker::Active() && GxWorker::g_thread == nullptr && GxWorker::g_exited.load() ? 0 : 1); }, ::testing::ExitedWithCode(0), ""); diff --git a/translator/src/Translator.Cli/Program.cs b/translator/src/Translator.Cli/Program.cs index 55f62f664..ddb719c89 100644 --- a/translator/src/Translator.Cli/Program.cs +++ b/translator/src/Translator.Cli/Program.cs @@ -37,8 +37,10 @@ CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture; var command = args.Length > 0 ? args[0] : "info"; -var (projectOption, tailWithoutProject) = ExtractOption(args.Skip(1).ToArray(), "--project"); -var (profileOption, tail) = ExtractOption(tailWithoutProject, "--profile"); +var (optionValues, enabledFlags, firstCommandArgument, helpRequested, unrecognizedOption) = + ParseCommandLineOptions(command, args); +var projectOption = optionValues.GetValueOrDefault("--project"); +var profileOption = optionValues.GetValueOrDefault("--profile"); var project = !string.IsNullOrWhiteSpace(projectOption) ? TranslationProjectConfig.Load(projectOption) : null; @@ -59,9 +61,14 @@ GuestSaveRestoreThunks.Current = GuestSaveRestoreThunks.FromFunctionMap(functionMap); } var root = project?.WorkspaceRoot ?? Directory.GetCurrentDirectory(); -var enabledFlags = new HashSet(StringComparer.Ordinal); -if (HandleCommandLineOptions(command, tail, enabledFlags) is { } commandLineExitCode) - return commandLineExitCode; +if (helpRequested) + return ShowCommandHelp(command); +if (unrecognizedOption is not null) +{ + Console.Error.WriteLine($"Unrecognized option '{unrecognizedOption}' for command '{command}'."); + WriteUsage(Console.Error, new[] { command }); + return 2; +} var preferCachedInputs = enabledFlags.Contains("--prefer-cached-inputs"); var dolFile = new Lazy(LoadDol); var relFile = new Lazy(LoadRel); @@ -164,13 +171,13 @@ BaseTranslationOutputMetadata ReadBaseMetadataCached(string path) "help" or "--help" or "-h" or "-?" => ShowGlobalHelp(), "info" or "--info" or "--version" => RunInfo(), "generate-data-init" => RunGenerateDataInit(), - "translate-recursive" => RunTranslateRecursive(tail), - "translate-mod" => RunTranslateMod(tail), - "emit-build-shards" => RunEmitBuildShards(tail), - "emit-native-index" => RunEmitNativeIndex(tail), - "emit-base-manifest" => RunEmitBaseManifest(tail), - "check-base-mod-awareness" => RunCheckBaseModAwareness(tail), - "validate-retro-wfc-payload" => RunValidateRetroWfcPayload(tail), + "translate-recursive" => RunTranslateRecursive(firstCommandArgument), + "translate-mod" => RunTranslateMod(), + "emit-build-shards" => RunEmitBuildShards(), + "emit-native-index" => RunEmitNativeIndex(), + "emit-base-manifest" => RunEmitBaseManifest(), + "check-base-mod-awareness" => RunCheckBaseModAwareness(), + "validate-retro-wfc-payload" => RunValidateRetroWfcPayload(), _ => ShowHelp(command) }; @@ -179,11 +186,11 @@ BaseTranslationOutputMetadata ReadBaseMetadataCached(string path) /// Exit 0 = yes, 3 = no, anything else = error (also treat as no). Answering wrong in the permissive /// direction bakes vanilla code into a modded product, so every uncertainty exits non-zero. /// -int RunCheckBaseModAwareness(string[] argsTail) +int RunCheckBaseModAwareness() { - var metadataPath = OptionValue(argsTail, "--translation-output-metadata") + var metadataPath = optionValues.GetValueOrDefault("--translation-output-metadata") ?? Path.Combine(root, "generated", "base_translation_output.json"); - var codePul = OptionValue(argsTail, "--code-pul") ?? profile?.CodePul; + var codePul = optionValues.GetValueOrDefault("--code-pul") ?? profile?.CodePul; if (string.IsNullOrWhiteSpace(codePul)) { Console.Error.WriteLine("[translator] check-base-mod-awareness needs --code-pul or a profile that has one."); @@ -232,7 +239,7 @@ RuntimeNativeIndex ResolveRegionalNativeIndex(string nativeSourceDirectory, stri return index.ForRegion(GuestAddressTable.Load(tablePath), region); } -int RunEmitBuildShards(string[] argsTail) +int RunEmitBuildShards() { // The project decides the guest region the shards are built for. if (project is null) @@ -240,13 +247,13 @@ int RunEmitBuildShards(string[] argsTail) Console.Error.WriteLine("emit-build-shards requires --project ."); return 1; } - var baseMetadata = OptionValue(argsTail, "--base-metadata") ?? Path.Combine(root, "generated", "base_translation_output.json"); - var baseFunctions = OptionValue(argsTail, "--base-functions-dir") ?? Path.Combine(root, "generated", "functions"); - var output = OptionValue(argsTail, "--out") ?? Path.Combine(root, "generated", "build_shards"); - var nativeSources = OptionValue(argsTail, "--native-source-dir") ?? Path.Combine(root, "runtime", "src"); - var nativeIndexOption = OptionValue(argsTail, "--native-index"); - var resolvedProfile = OptionValue(argsTail, "--resolved-profile"); - var retroCpp = OptionValue(argsTail, "--retro-cpp-dir"); + var baseMetadata = optionValues.GetValueOrDefault("--base-metadata") ?? Path.Combine(root, "generated", "base_translation_output.json"); + var baseFunctions = optionValues.GetValueOrDefault("--base-functions-dir") ?? Path.Combine(root, "generated", "functions"); + var output = optionValues.GetValueOrDefault("--out") ?? Path.Combine(root, "generated", "build_shards"); + var nativeSources = optionValues.GetValueOrDefault("--native-source-dir") ?? Path.Combine(root, "runtime", "src"); + var nativeIndexOption = optionValues.GetValueOrDefault("--native-index"); + var resolvedProfile = optionValues.GetValueOrDefault("--resolved-profile"); + var retroCpp = optionValues.GetValueOrDefault("--retro-cpp-dir"); try { @@ -300,16 +307,16 @@ int RunEmitBuildShards(string[] argsTail) } } -int RunEmitNativeIndex(string[] argsTail) +int RunEmitNativeIndex() { - var output = OptionValue(argsTail, "--out"); + var output = optionValues.GetValueOrDefault("--out"); if (string.IsNullOrWhiteSpace(output)) { Console.Error.WriteLine("[translator] emit-native-index needs --out."); return 1; } var nativeSourcePath = Path.GetFullPath( - OptionValue(argsTail, "--native-source-dir") ?? RequireProject().Runtime.NativeRegistrationRoot); + optionValues.GetValueOrDefault("--native-source-dir") ?? RequireProject().Runtime.NativeRegistrationRoot); if (!Directory.Exists(nativeSourcePath)) { Console.Error.WriteLine($"[translator] native source directory not found: {nativeSourcePath}"); @@ -413,9 +420,9 @@ int RunInfo() return 0; } -int RunValidateRetroWfcPayload(string[] argsTail) +int RunValidateRetroWfcPayload() { - var directory = OptionValue(argsTail, "--directory"); + var directory = optionValues.GetValueOrDefault("--directory"); if (string.IsNullOrWhiteSpace(directory)) { Console.Error.WriteLine("--directory is required."); @@ -425,7 +432,7 @@ int RunValidateRetroWfcPayload(string[] argsTail) try { WiiCompiled.Setup.Common.RetroWfcPayload.ValidateStagedRetroWfcPayloadDirectory(directory, - gameId: OptionValue(argsTail, "--game-id")!); + gameId: optionValues.GetValueOrDefault("--game-id")!); Console.WriteLine("[translator] Retro WFC payload signature validated."); return 0; } @@ -441,19 +448,19 @@ int RunValidateRetroWfcPayload(string[] argsTail) } } -int RunTranslateRecursive(string[] argsTail) +int RunTranslateRecursive(string? firstCommandArgument) { - if (argsTail.Length == 0 || argsTail[0].StartsWith("--", StringComparison.Ordinal)) + if (firstCommandArgument is null || firstCommandArgument.StartsWith("--", StringComparison.Ordinal)) { Console.Error.WriteLine("Usage: translator translate-recursive [--outdir path] [--output-metadata path] [--threads N] [--prune-stale]"); return 1; } - var startAddr = ParseAddress(argsTail[0]); + var startAddr = ParseAddress(firstCommandArgument); var allowUnsupportedInstructions = RequireProject().Translation.AllowUnsupportedInstructions; - var outDir = OptionValue(argsTail, "--outdir") ?? RequireProject().Output.Functions; - var outputMetadataPath = OptionValue(argsTail, "--output-metadata"); - var productionSourceBundlePath = OptionValue(argsTail, "--production-source-bundle"); + var outDir = optionValues.GetValueOrDefault("--outdir") ?? RequireProject().Output.Functions; + var outputMetadataPath = optionValues.GetValueOrDefault("--output-metadata"); + var productionSourceBundlePath = optionValues.GetValueOrDefault("--production-source-bundle"); var emitFunctionFiles = !enabledFlags.Contains("--no-function-files"); var boundedIr = enabledFlags.Contains("--bounded-ir"); if (!emitFunctionFiles && string.IsNullOrWhiteSpace(productionSourceBundlePath)) @@ -474,7 +481,7 @@ outputMetadataPath is not null && $"[translator] Warning: existing output metadata could not be read; stale pruning will use one-time tree migration: {ex.Message}"); } } - var threadsOption = OptionValue(argsTail, "--threads"); + var threadsOption = optionValues.GetValueOrDefault("--threads"); var maxThreads = threadsOption != null ? Math.Max(1, ParseInt(threadsOption)) : Math.Max(1, Environment.ProcessorCount); Console.WriteLine($"[translator] Using up to {maxThreads} worker thread(s)."); @@ -953,6 +960,7 @@ void LogPhase(string label) IReadOnlyDictionary canonicalGuestFunctions = bufferedGuestFunctions is not null ? bufferedGuestFunctions : new CanonicalIrDictionary(canonicalIrStore, emissionAddresses); + Console.WriteLine($"[translator] Canonical IR mode: {(bufferedGuestFunctions is null ? "bounded" : "buffered")}."); var irInstructionCounts = new Dictionary(emissionAddresses.Length); // Each slot is an independent pure decode plus contract analysis, so // compute them in parallel into position-indexed arrays and fill the @@ -988,7 +996,8 @@ void LogPhase(string label) canonicalGuestFunctions, nativeGuestEffects.Contracts, retainBodies: !boundedIr, - maxDegreeOfParallelism: boundedIr ? maxThreads : null); + maxDegreeOfParallelism: boundedIr ? maxThreads : null, + localContracts: boundedIr ? localGuestAbiContracts : null); var guestAbiContracts = interproceduralGuestAbi.Contracts .ToDictionary(static pair => pair.Key, static pair => pair.Value); var recursiveComponents = interproceduralGuestAbi.StronglyConnectedComponents @@ -1624,12 +1633,12 @@ static bool ContainsInteriorBoundary(uint start, uint end, uint[] boundaries) return 0; } -int RunTranslateMod(string[] argsTail) +int RunTranslateMod() { - var outDir = OptionValue(argsTail, "--out") ?? profile?.Output; + var outDir = optionValues.GetValueOrDefault("--out") ?? profile?.Output; if (string.IsNullOrWhiteSpace(outDir)) { - return RunTranslateModCore(argsTail, null); + return RunTranslateModCore(null); } var publishedOutput = Path.GetFullPath(outDir); @@ -1641,7 +1650,7 @@ int RunTranslateMod(string[] argsTail) { var result = TransactionalDirectoryOutput.GenerateAndPublish( publishedOutput, - stagingDirectory => RunTranslateModCore(argsTail, stagingDirectory), + RunTranslateModCore, (stagingDirectory, exitCode) => PreserveTranslateModFailureDiagnostics( stagingDirectory, publishedOutput, @@ -1689,7 +1698,7 @@ void PreserveTranslateModFailureDiagnostics(string stagingDirectory, string publ Console.Error.WriteLine($"[translator] Preserved failure diagnostics: {diagnosticsDirectory}"); } -int RunTranslateModCore(string[] argsTail, string? outputDirectoryOverride) +int RunTranslateModCore(string? outputDirectoryOverride) { // Wall-clock accounting mirroring RunTranslateRecursive's LogPhase output. var modPhaseStart = DateTime.Now; @@ -1701,9 +1710,9 @@ void LogModPhase(string label) modLastPhaseSeconds = now; } - var codePul = OptionValue(argsTail, "--code-pul") ?? profile?.CodePul; - var baseManifestPath = OptionValue(argsTail, "--base-manifest"); - var outDir = outputDirectoryOverride ?? OptionValue(argsTail, "--out") ?? profile?.Output; + var codePul = optionValues.GetValueOrDefault("--code-pul") ?? profile?.CodePul; + var baseManifestPath = optionValues.GetValueOrDefault("--base-manifest"); + var outDir = outputDirectoryOverride ?? optionValues.GetValueOrDefault("--out") ?? profile?.Output; if (string.IsNullOrWhiteSpace(codePul) || string.IsNullOrWhiteSpace(baseManifestPath) || string.IsNullOrWhiteSpace(outDir)) @@ -1712,8 +1721,8 @@ void LogModPhase(string label) return 1; } - var modRoot = OptionValue(argsTail, "--mod-root") ?? profile?.ModRoot; - var baseTranslationOutputMetadataPath = OptionValue(argsTail, "--base-translation-output-metadata") + var modRoot = optionValues.GetValueOrDefault("--mod-root") ?? profile?.ModRoot; + var baseTranslationOutputMetadataPath = optionValues.GetValueOrDefault("--base-translation-output-metadata") ?? Path.Combine(root, "generated", "base_translation_output.json"); // The base translation bakes mod-safety decisions (leaf-inlining blocks, residency fences) // around the patch set it knew about; a base tree that never saw this Code.pul can splice @@ -1769,7 +1778,7 @@ void LogModPhase(string label) "cannot verify the base translation is aware of this mod's patch set."); } var skipRetroWfc = enabledFlags.Contains("--skip-retro-wfc"); - var explicitRetroWfcPayloadSpec = OptionValue(argsTail, "--retro-wfc-payload"); + var explicitRetroWfcPayloadSpec = optionValues.GetValueOrDefault("--retro-wfc-payload"); var retroWfcPayloadSpec = skipRetroWfc ? null : explicitRetroWfcPayloadSpec ?? @@ -1780,19 +1789,19 @@ void LogModPhase(string label) Console.Error.WriteLine("[translator] Retro WFC lowering is project-specific and requires a profile with enable_retro_wfc: true."); return 1; } - var modName = OptionValue(argsTail, "--mod-name") + var modName = optionValues.GetValueOrDefault("--mod-name") ?? (!string.IsNullOrWhiteSpace(modRoot) ? Path.GetFileName(Path.GetFullPath(modRoot)) : "Pulsar Mod"); - var region = OptionValue(argsTail, "--region") ?? profile?.Region ?? "P"; + var region = optionValues.GetValueOrDefault("--region") ?? profile?.Region ?? "P"; if (project?.Identity.Region is { } projectRegion && !string.Equals(region, projectRegion, StringComparison.OrdinalIgnoreCase)) { Console.Error.WriteLine($"[translator] --region {region} does not match the project's region {projectRegion}."); return 1; } - var moduleGuestBase = OptionValue(argsTail, "--module-guest-base") is { } moduleBaseText + var moduleGuestBase = optionValues.GetValueOrDefault("--module-guest-base") is { } moduleBaseText ? ParseAddress(moduleBaseText) : profile?.ModuleGuestBase ?? 0x81700000u; - var moduleLinkBaseText = OptionValue(argsTail, "--module-link-base"); + var moduleLinkBaseText = optionValues.GetValueOrDefault("--module-link-base"); var moduleLinkBase = moduleLinkBaseText is not null ? ParseAddress(moduleLinkBaseText) : profile?.ModuleLinkBase ?? 0x803992E0u; @@ -1970,7 +1979,7 @@ void LogModPhase(string label) .ToList(); var cppFailureCount = 0; var emitCpp = enabledFlags.Contains("--emit-cpp"); - var maxThreads = OptionValue(argsTail, "--threads") is { } threadsText + var maxThreads = optionValues.GetValueOrDefault("--threads") is { } threadsText ? Math.Max(1, ParseInt(threadsText)) : Math.Max(1, Environment.ProcessorCount); LogModPhase("setup + patch planning"); @@ -1992,8 +2001,7 @@ void LogModPhase(string label) moduleLinkBase, selected.CodeSize, nonReturningModuleCallTargets, - maxThreads, - argsTail); + maxThreads); } LogModPhase("mod C++ emission"); @@ -2342,11 +2350,7 @@ int EmitModCpp( uint moduleLinkBase, uint moduleLinkedCodeSize, IReadOnlySet nonReturningCallTargets, - int maxThreads, - // The translate-mod command line, so mod emission honours the same codegen - // policy overrides (residency, boundary narrowing) as base emission instead - // of quietly diverging from it. - string[] argsTail) + int maxThreads) { var modImage = BuildSyntheticModProgramImage(outDir, overlayBuild); var inferredAbi = new InferredGuestFunctionAbiProvider(modImage); @@ -2880,17 +2884,17 @@ static bool RetroWfcHookSetsLinkRegister(RetroWfcExecutableHookPlan hook) => hook.TypeName is "call" or "branchCtrLink" || hook.Intent.Contains("Call", StringComparison.Ordinal); -int RunEmitBaseManifest(string[] argsTail) +int RunEmitBaseManifest() { var loadedProject = RequireProject(); - var outDir = OptionValue(argsTail, "--out") ?? Path.GetDirectoryName(loadedProject.Output.BaseManifest)!; - var functionsDir = OptionValue(argsTail, "--functions-dir") ?? loadedProject.Output.Functions; - var outputMetadataPath = OptionValue(argsTail, "--translation-output-metadata"); + var outDir = optionValues.GetValueOrDefault("--out") ?? Path.GetDirectoryName(loadedProject.Output.BaseManifest)!; + var functionsDir = optionValues.GetValueOrDefault("--functions-dir") ?? loadedProject.Output.Functions; + var outputMetadataPath = optionValues.GetValueOrDefault("--translation-output-metadata"); var outputMetadata = outputMetadataPath is null ? null : BaseTranslationOutputMetadataFile.Read(outputMetadataPath); outputMetadata?.RequireReleaseEligible(outputMetadataPath!); - var region = OptionValue(argsTail, "--region") ?? loadedProject.Identity.Region ?? "unknown"; + var region = optionValues.GetValueOrDefault("--region") ?? loadedProject.Identity.Region ?? "unknown"; var rel = relFile.Value ?? throw new InvalidOperationException("Base mod manifest generation currently requires the project's single configured REL."); var relImage = rel.BuildImage(loadedProject.Inputs.Rel!.LoadAddress); @@ -3771,65 +3775,60 @@ static string CommandUsage(string command) return text.ToString(); } -// Options every command tolerates. --project and --profile are consumed before -// dispatch, so they only reach a command's tail when they were repeated. -static string[] GlobalValueOptions() => new[] { "--project", "--profile" }; - -static string[] GlobalFlagOptions() => new[] { "--prefer-cached-inputs" }; - -/// -/// Returns an exit code when the command line was fully handled here (help, or an option the -/// command's parser would discard), null otherwise. Only inspects option-like tokens; positional -/// addresses, paths and passthrough values are never rejected. -/// -static int? HandleCommandLineOptions(string command, string[] argsTail, HashSet enabledFlags) +static (Dictionary Values, HashSet Flags, string? FirstCommandArgument, + bool HelpRequested, string? UnrecognizedOption) ParseCommandLineOptions(string command, string[] args) { - if (CommandSpec(command) is not { } spec) - { - return null; - } - - if (argsTail.Any(static token => - string.Equals(token, "--help", StringComparison.Ordinal) || - string.Equals(token, "-h", StringComparison.Ordinal) || - string.Equals(token, "-?", StringComparison.Ordinal))) + var spec = CommandSpec(command); + var valueNames = new HashSet(StringComparer.Ordinal) { "--project", "--profile" }; + var flagNames = new HashSet(StringComparer.Ordinal) { "--prefer-cached-inputs" }; + foreach (var option in spec?.Options ?? Array.Empty()) { - return ShowCommandHelp(command); + (option.Value is null ? flagNames : valueNames).Add(option.Name); } - var values = new HashSet(StringComparer.Ordinal); - values.UnionWith(spec.Options.Where(static option => option.Value is not null).Select(static option => option.Name)); - values.UnionWith(GlobalValueOptions()); + var values = new Dictionary(StringComparer.Ordinal); var flags = new HashSet(StringComparer.Ordinal); - flags.UnionWith(spec.Options.Where(static option => option.Value is null).Select(static option => option.Name)); - flags.UnionWith(GlobalFlagOptions()); + string? firstCommandArgument = null; + string? unrecognizedOption = null; + var helpRequested = false; - for (var index = 0; index < argsTail.Length; index++) + for (var index = 1; index < args.Length; index++) { - var token = argsTail[index]; - if (values.Contains(token)) - { - // Step over the value so an option argument that happens to look - // like an option of its own is never rejected. - index++; - continue; - } - if (flags.Contains(token)) + var token = args[index]; + // The first --project and --profile pairs can precede the command's positional argument. + var firstGlobalValue = (token is "--project" or "--profile") && !values.ContainsKey(token); + if (!firstGlobalValue) + firstCommandArgument ??= token; + + if (valueNames.Contains(token)) { - enabledFlags.Add(token); + if (index + 1 == args.Length) + { + if (firstGlobalValue) throw new ArgumentException($"{token} requires a value."); + continue; + } + + var value = args[++index]; + values.TryAdd(token, value); + // Help historically takes precedence over command arguments, including value operands. + helpRequested |= !firstGlobalValue && IsHelpOption(value); continue; } - if (!token.StartsWith('-')) + if (flagNames.Contains(token)) { + flags.Add(token); continue; } - - Console.Error.WriteLine($"Unrecognized option '{token}' for command '{command}'."); - WriteUsage(Console.Error, new[] { command }); - return 2; + if (IsHelpOption(token)) + helpRequested = true; + else if (token.StartsWith('-')) + unrecognizedOption ??= token; } - return null; + return (values, flags, firstCommandArgument, spec is not null && helpRequested, + spec is not null ? unrecognizedOption : null); + + static bool IsHelpOption(string token) => token is "--help" or "-h" or "-?"; } ProgramImage LoadImage() @@ -3940,36 +3939,6 @@ static int ParseInt(string text) => ? int.Parse(text[2..], NumberStyles.HexNumber, CultureInfo.InvariantCulture) : int.Parse(text, CultureInfo.InvariantCulture); -static string? OptionValue(string[] argsTail, string name) -{ - var index = Array.IndexOf(argsTail, name); - if (index >= 0 && index + 1 < argsTail.Length) - { - return argsTail[index + 1]; - } - return null; -} - -static (string? Value, string[] Remaining) ExtractOption(string[] values, string name) -{ - for (var index = 0; index < values.Length; index++) - { - if (!string.Equals(values[index], name, StringComparison.Ordinal)) - { - continue; - } - if (index + 1 >= values.Length) - { - throw new ArgumentException($"{name} requires a value."); - } - - var remaining = values.Where((_, itemIndex) => itemIndex != index && itemIndex != index + 1).ToArray(); - return (values[index + 1], remaining); - } - - return (null, values); -} - /// /// Addresses a leaf splice must never cover: the decoded bytes aren't what the runtime /// executes there (native registration/fatal stub, exclusion, or a mod overlay rewrite). diff --git a/translator/src/Translator.Core/Analysis/GuestAbiInterproceduralAnalyzer.cs b/translator/src/Translator.Core/Analysis/GuestAbiInterproceduralAnalyzer.cs index a5374ac88..aa3f529b6 100644 --- a/translator/src/Translator.Core/Analysis/GuestAbiInterproceduralAnalyzer.cs +++ b/translator/src/Translator.Core/Analysis/GuestAbiInterproceduralAnalyzer.cs @@ -16,11 +16,16 @@ public sealed record GuestAbiInterproceduralResult( /// public static class GuestAbiInterproceduralAnalyzer { + /// + /// If supplied, must come from the same bodies analyzed + /// without callee contracts and cover exactly the supplied function addresses. + /// public static GuestAbiInterproceduralResult Analyze( IReadOnlyDictionary functions, IReadOnlyDictionary? externalContracts = null, bool retainBodies = true, - int? maxDegreeOfParallelism = null) + int? maxDegreeOfParallelism = null, + IReadOnlyDictionary? localContracts = null) { externalContracts ??= new Dictionary(); var addresses = functions.Keys.OrderBy(static address => address).ToArray(); @@ -33,6 +38,21 @@ public static GuestAbiInterproceduralResult Analyze( MaxDegreeOfParallelism = maxDegreeOfParallelism ?? Environment.ProcessorCount }; + // Mirror of `contracts` keyed by ordinal. The fixpoint compares against + // it from the parallel phase, where the dictionary must stay untouched. + var current = new GuestAbiContract[addresses.Length]; + if (localContracts is not null) + { + if (localContracts.Count != addresses.Length) + throw new ArgumentException("Local contracts must match the function addresses.", nameof(localContracts)); + for (var index = 0; index < addresses.Length; ++index) + { + if (!localContracts.TryGetValue(addresses[index], out var contract)) + throw new ArgumentException($"Missing local contract for 0x{addresses[index]:X8}.", nameof(localContracts)); + current[index] = contract; + } + } + // Lazy callers must supply stable bodies and support concurrent reads. var bodies = retainBodies ? new IrFunction[addresses.Length] : null; if (bodies is not null) @@ -41,14 +61,14 @@ public static GuestAbiInterproceduralResult Analyze( } IrFunction Body(int index) => bodies is not null ? bodies[index] : functions[addresses[index]]; - // Mirror of `contracts` keyed by ordinal. The fixpoint compares against - // it from the parallel phase, where the dictionary must stay untouched. - var current = new GuestAbiContract[addresses.Length]; - Parallel.For( - 0, - addresses.Length, - parallelOptions, - index => current[index] = GuestAbiContractAnalyzer.Analyze(Body(index))); + if (localContracts is null) + { + Parallel.For( + 0, + addresses.Length, + parallelOptions, + index => current[index] = GuestAbiContractAnalyzer.Analyze(Body(index))); + } var contracts = new Dictionary(addresses.Length); for (var index = 0; index < addresses.Length; ++index) contracts.Add(addresses[index], current[index]); diff --git a/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs b/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs index 6a730d336..4a7acdb08 100644 --- a/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs +++ b/translator/src/Translator.Core/Translation/CanonicalIrDictionary.cs @@ -1,12 +1,13 @@ using System.Collections; +using System.Diagnostics.CodeAnalysis; using Translator.Core.Ir; namespace Translator.Core.Translation; /// /// Fixed keys over canonical SSA decoded on demand without caching bodies. -/// Keys, Count and ContainsKey describe the address snapshot. Enumeration requires -/// the provider to retain every body in that snapshot for the duration of the read. +/// The provider must supply a body for every address in the snapshot for as long +/// as the dictionary is used. /// Concurrent readers require a thread-safe provider, such as CanonicalIrStore. /// public sealed class CanonicalIrDictionary : IReadOnlyDictionary @@ -39,15 +40,21 @@ public IEnumerable Values public IrFunction this[uint key] => TryGetValue(key, out var value) ? value - : throw new KeyNotFoundException($"Missing canonical SSA for 0x{key:X8}."); + : throw new KeyNotFoundException($"Address 0x{key:X8} is outside the canonical SSA snapshot."); public bool ContainsKey(uint key) => _membership.Contains(key); - public bool TryGetValue(uint key, out IrFunction value) + public bool TryGetValue(uint key, [MaybeNullWhen(false)] out IrFunction value) { - if (_membership.Contains(key) && _provider.TryGet(key, out value)) return true; - value = null!; - return false; + if (!_membership.Contains(key)) + { + value = null; + return false; + } + + if (!_provider.TryGet(key, out value)) + throw new InvalidOperationException($"Missing canonical SSA for snapshot address 0x{key:X8}."); + return true; } public IEnumerator> GetEnumerator() diff --git a/translator/tests/Translator.Tests/CanonicalIrDictionaryTests.cs b/translator/tests/Translator.Tests/CanonicalIrDictionaryTests.cs index 7acc5bb5b..3411c0328 100644 --- a/translator/tests/Translator.Tests/CanonicalIrDictionaryTests.cs +++ b/translator/tests/Translator.Tests/CanonicalIrDictionaryTests.cs @@ -51,7 +51,7 @@ public void KeysAndMembershipUseAnIndependentSnapshotWithoutDecoding() } [Fact] - public void MissingAndReplacedBodiesDoNotChangeMembershipOrLeaveCachedValues() + public void ReplacedBodiesAreReadAndMissingSnapshotBodiesViolateTheInvariant() { var store = new CanonicalIrStore(); store.Put(10, Function("original", new IrReturn(null))); @@ -61,14 +61,14 @@ public void MissingAndReplacedBodiesDoNotChangeMembershipOrLeaveCachedValues() Assert.Equal("replacement", view[10].Name); store.Remove(10); - Assert.Single(view.Keys); Assert.True(view.ContainsKey(10)); Assert.Equal(new uint[] { 10 }, view.Keys); - Assert.False(view.TryGetValue(10, out var missing)); - Assert.Null(missing); - Assert.Throws(() => view[10]); - Assert.Throws(() => view.Values.ToArray()); - Assert.Throws(() => view.ToArray()); + Assert.Equal(view.Keys.Count(), view.Count); + var error = Assert.Throws(() => view.TryGetValue(10, out _)); + Assert.Contains("0x0000000A", error.Message, StringComparison.Ordinal); + Assert.Throws(() => view[10]); + Assert.Throws(() => view.Values.ToArray()); + Assert.Throws(() => view.ToArray()); } [Fact] @@ -177,10 +177,11 @@ public void LazyAnalysisMatchesBufferedContractsAndComponents(int parallelism) } [Fact] - public void DefaultAnalysisResolvesEachBodyOnceAndLazyAnalysisResolvesEachPass() + public void LocalContractsSaveOneLazyBodyReadWithoutChangingTheResult() { var store = new CanonicalIrStore(); - store.Put(10, Function("leaf", new IrReturn(null))); + var body = Function("leaf", new IrReturn(null)); + store.Put(10, body); var provider = new CountingProvider(store); var view = new CanonicalIrDictionary(provider, new uint[] { 10 }); @@ -189,6 +190,29 @@ public void DefaultAnalysisResolvesEachBodyOnceAndLazyAnalysisResolvesEachPass() var lazy = GuestAbiInterproceduralAnalyzer.Analyze(view, retainBodies: false, maxDegreeOfParallelism: 1); Assert.Equal(4, provider.Reads); Assert.Equal(JsonSerializer.Serialize(buffered), JsonSerializer.Serialize(lazy)); + + var localContracts = new Dictionary { [10] = GuestAbiContractAnalyzer.Analyze(body) }; + var seeded = GuestAbiInterproceduralAnalyzer.Analyze(view, retainBodies: false, + maxDegreeOfParallelism: 1, localContracts: localContracts); + Assert.Equal(6, provider.Reads); + Assert.Equal(JsonSerializer.Serialize(lazy), JsonSerializer.Serialize(seeded)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void LocalContractsMustMatchFunctionAddressesBeforeDecoding(bool includeUnrelatedAddress) + { + var provider = new CountingProvider(new CanonicalIrStore()); + var view = new CanonicalIrDictionary(provider, new uint[] { 10 }); + var localContracts = new Dictionary(); + if (includeUnrelatedAddress) + localContracts[20] = GuestAbiContractAnalyzer.Analyze(Function("other", new IrReturn(null))); + + var error = Assert.Throws(() => GuestAbiInterproceduralAnalyzer.Analyze( + view, retainBodies: false, maxDegreeOfParallelism: 1, localContracts: localContracts)); + Assert.Equal("localContracts", error.ParamName); + Assert.Equal(0, provider.Reads); } [Fact] @@ -212,7 +236,9 @@ public void LazyAnalysisRevisitsCallersOfInlineThunksAfterTheirCalleesChange() foreach (var pair in functions) store.Put(pair.Key, pair.Value); var buffered = GuestAbiInterproceduralAnalyzer.Analyze(functions); var lazy = GuestAbiInterproceduralAnalyzer.Analyze(new CanonicalIrDictionary(store, functions.Keys), - retainBodies: false, maxDegreeOfParallelism: 2); + retainBodies: false, maxDegreeOfParallelism: 2, + localContracts: functions.ToDictionary(static pair => pair.Key, + static pair => GuestAbiContractAnalyzer.Analyze(pair.Value))); Assert.Equal(JsonSerializer.Serialize(buffered), JsonSerializer.Serialize(lazy)); Assert.DoesNotContain(thunk, lazy.Contracts[0x80001000].DirectCallTargets); diff --git a/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs b/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs index acaa2f6cd..7461f3be0 100644 --- a/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs +++ b/translator/tests/Translator.Tests/TranslateRecursiveRuntimeConfigTests.cs @@ -64,7 +64,11 @@ public void BoundedIrMatchesBufferedFilesMetadataAndBundleAcrossThreadCounts() }; if (bounded) args.Add("--bounded-ir"); if (!emitFiles) args.Add("--no-function-files"); - Assert.Equal(0, InvokeCli(args.ToArray())); + var run = InvokeCliProcess(root, args.ToArray()); + Assert.Equal(0, run.ExitCode); + Assert.Contains( + $"[translator] Canonical IR mode: {(bounded ? "bounded" : "buffered")}.", + run.Output, StringComparison.Ordinal); var sources = Directory.Exists(functions) ? Directory.GetFiles(functions, "*.cpp").ToDictionary( static path => Path.GetFileName(path), static path => File.ReadAllText(path)) @@ -75,24 +79,27 @@ public void BoundedIrMatchesBufferedFilesMetadataAndBundleAcrossThreadCounts() var buffered = Translate("buffered", 1, bounded: false); Assert.Equal(2, buffered.Sources.Count); - var optionPath = new ProcessStartInfo("dotnet") - { - WorkingDirectory = root, - ArgumentList = { typeof(TranslationProjectConfig).Assembly.Location, "translate-recursive", - $"0x{entry:X8}", "--project", projectPath, "--outdir", "--no-function-files", "--bounded-ir", - "--threads", "1" } - }; - using (var process = Process.Start(optionPath)!) - { - if (!process.WaitForExit(30_000)) - { - process.Kill(entireProcessTree: true); - process.WaitForExit(); - throw new TimeoutException("Translation with an option-shaped output path timed out."); - } - Assert.Equal(0, process.ExitCode); - Assert.Equal(2, Directory.GetFiles(Path.Combine(root, "--no-function-files"), "*.cpp").Length); - } + var metadataCollision = InvokeCliProcess(root, "translate-recursive", "--project", projectPath, + $"0x{entry:X8}", "--outdir", "--output-metadata", "--bounded-ir", + "--output-metadata", "result.json", "--output-metadata", "ignored.json", "--threads", "0x1"); + Assert.Equal(0, metadataCollision.ExitCode); + Assert.Contains("[translator] Canonical IR mode: bounded.", metadataCollision.Output, StringComparison.Ordinal); + Assert.Contains("[translator] Using up to 1 worker thread(s).", metadataCollision.Output, StringComparison.Ordinal); + Assert.Equal(2, BaseTranslationOutputMetadataFile.Read(Path.Combine(root, "result.json")).Functions.Count); + Assert.Equal(2, Directory.GetFiles(Path.Combine(root, "--output-metadata"), "*.cpp").Length); + Assert.False(File.Exists(Path.Combine(root, "ignored.json"))); + Assert.False(File.Exists(Path.Combine(root, "--bounded-ir"))); + + var flagOperands = InvokeCliProcess(root, "translate-recursive", $"0x{entry:X8}", + "--outdir", "--bounded-ir", "--output-metadata", "--project", + "--production-source-bundle", "--no-function-files", "--threads", "-1", "--project", projectPath); + Assert.Equal(0, flagOperands.ExitCode); + Assert.Contains("[translator] Canonical IR mode: buffered.", flagOperands.Output, StringComparison.Ordinal); + Assert.Contains("[translator] Using up to 1 worker thread(s).", flagOperands.Output, StringComparison.Ordinal); + Assert.Equal(2, Directory.GetFiles(Path.Combine(root, "--bounded-ir"), "*.cpp").Length); + Assert.Equal(2, BaseTranslationOutputMetadataFile.Read(Path.Combine(root, "--project")).Functions.Count); + Assert.True(File.Exists(Path.Combine(root, "--no-function-files"))); + foreach (var threads in new[] { 1, 3 }) { var lazy = Translate($"lazy-{threads}", threads, bounded: true); @@ -113,6 +120,19 @@ public void BoundedIrMatchesBufferedFilesMetadataAndBundleAcrossThreadCounts() } } + [Theory] + [InlineData("--unknown", "--help")] + [InlineData("--unknown", "-h")] + [InlineData("--outdir", "-?")] + public void CommandHelpTakesPrecedenceOverOtherArguments(string option, string help) + { + var run = InvokeCliProcess(Path.GetTempPath(), "translate-recursive", option, help); + Assert.Equal(0, run.ExitCode); + Assert.Contains("translator translate-recursive ", run.Output, StringComparison.Ordinal); + Assert.Contains("[--bounded-ir]", run.Output, StringComparison.Ordinal); + Assert.Empty(run.Error); + } + [Fact] public void UnsupportedOpcodeFailsStrictTranslation() { @@ -292,6 +312,29 @@ public void RepeatedRunsReuseUnchangedOutputsAndPruneStaleFiles() } } + private static (int ExitCode, string Output, string Error) InvokeCliProcess(string workingDirectory, params string[] args) + { + var startInfo = new ProcessStartInfo("dotnet") + { + WorkingDirectory = workingDirectory, + RedirectStandardOutput = true, + RedirectStandardError = true, + ArgumentList = { typeof(TranslationProjectConfig).Assembly.Location } + }; + foreach (var arg in args) startInfo.ArgumentList.Add(arg); + + using var process = Process.Start(startInfo)!; + var output = process.StandardOutput.ReadToEndAsync(); + var error = process.StandardError.ReadToEndAsync(); + if (!process.WaitForExit(30_000)) + { + process.Kill(entireProcessTree: true); + process.WaitForExit(); + throw new TimeoutException("Translator subprocess timed out."); + } + return (process.ExitCode, output.GetAwaiter().GetResult(), error.GetAwaiter().GetResult()); + } + private static int InvokeCli(params string[] args) { var entryPoint = typeof(TranslationProjectConfig).Assembly.EntryPoint; From 9d4df4070dfa955661fc373e7d9d68facea108d9 Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:47:46 +0100 Subject: [PATCH 16/17] reject empty aurora checkouts --- Launcher/Prepare-NativePrebuilt.sh | 2 +- Launcher/Test-AuroraPrebuilt.sh | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Launcher/Prepare-NativePrebuilt.sh b/Launcher/Prepare-NativePrebuilt.sh index 94f1f9bb5..51cf0224e 100755 --- a/Launcher/Prepare-NativePrebuilt.sh +++ b/Launcher/Prepare-NativePrebuilt.sh @@ -124,7 +124,7 @@ assert_file "$cmake_bin" "Portable CMake (run prepare-portable-tools.sh --arch $ assert_file "$ninja_bin" "Portable Ninja" assert_file "$cc" "Portable C compiler" assert_file "$cxx" "Portable C++ compiler" -assert_dir "$aurora_source" "aurora-main source tree" +assert_file "$aurora_source/CMakeLists.txt" "aurora-main source tree" clang_binary=$(normalize "${llvm_dir:-$toolchain_dir}/bin/clang-22") assert_file "$clang_binary" "Portable clang driver binary" diff --git a/Launcher/Test-AuroraPrebuilt.sh b/Launcher/Test-AuroraPrebuilt.sh index eed15a28c..d2d48ea2c 100755 --- a/Launcher/Test-AuroraPrebuilt.sh +++ b/Launcher/Test-AuroraPrebuilt.sh @@ -57,3 +57,17 @@ for invalid in '{}' 'null' 'not json' '{"AuroraSourceFingerprint":false}' \ printf '%s\n' "$invalid" > package/provenance.json expect_error 'does not match the Aurora sources' done + +mkdir -p producer/{Launcher,aurora-main,runtime/{third_party,cmake},tools/bin} +cp "$launcher"/{Prepare-NativePrebuilt.sh,build-cache.sh} producer/Launcher/ +touch producer/runtime/cmake/MbedTLSPin.cmake producer/tools/bin/{clang,clang++,clang-22} +producer_args=(--arch aarch64 --print-fingerprint-only --toolchain-dir "$PWD/producer/tools" + --cmake-bin /bin/true --ninja-bin /bin/true) +if output=$(bash producer/Launcher/Prepare-NativePrebuilt.sh "${producer_args[@]}" 2>&1); then + echo 'accepted an empty aurora checkout' >&2 + exit 1 +fi +[[ "$output" == *'aurora-main/CMakeLists.txt'* ]] +cp 'workspace[old]/aurora-main/CMakeLists.txt' producer/aurora-main/ +output=$(bash producer/Launcher/Prepare-NativePrebuilt.sh "${producer_args[@]}") +[[ "$output" =~ aurora_fingerprint=[0-9a-f]{64} ]] From 51d897856450d654f9838e20b87ff8ea7677a1bd Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:09:24 +0100 Subject: [PATCH 17/17] point aurora at merged main --- aurora-main | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aurora-main b/aurora-main index 6315f3dae..5ba138858 160000 --- a/aurora-main +++ b/aurora-main @@ -1 +1 @@ -Subproject commit 6315f3dae3554752ee696180f04ed747e30edc78 +Subproject commit 5ba1388584fa9dc78f16208c7a9ae10802e14b1c