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/11] 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/11] 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/11] 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/11] 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 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 05/11] 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 06/11] 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 07/11] 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 08/11] 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 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 09/11] 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 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 10/11] 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 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 11/11] 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} ]]