diff --git a/core/libs/prx/libSceAjm.native/CMakeLists.txt b/core/libs/prx/libSceAjm.native/CMakeLists.txt index aa4e24f83..a231a1529 100644 --- a/core/libs/prx/libSceAjm.native/CMakeLists.txt +++ b/core/libs/prx/libSceAjm.native/CMakeLists.txt @@ -36,6 +36,13 @@ if(BUILD_TESTING) target_link_libraries(guest_ajm_tests PRIVATE libSceAjm.native libc) configure_windows_unwind(guest_ajm_tests) add_test(NAME guest_ajm COMMAND guest_ajm_tests) + if(UNIX) + add_test_executable(guest_ajm_teardown_tests ${LIBS_INCLUDE_DIR}/tests/GuestAjmTeardown.cpp) + target_include_directories(guest_ajm_teardown_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_ajm_teardown_tests PRIVATE libc ${CMAKE_DL_LIBS}) + add_dependencies(guest_ajm_teardown_tests libSceAjm.native) + add_test(NAME guest_ajm_teardown COMMAND guest_ajm_teardown_tests $) + endif() if(WIN32) set_tests_properties(guest_ajm PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") diff --git a/core/libs/prx/libSceAjm.native/src/Ajm.cpp b/core/libs/prx/libSceAjm.native/src/Ajm.cpp index b7649e151..2a10bf7d4 100644 --- a/core/libs/prx/libSceAjm.native/src/Ajm.cpp +++ b/core/libs/prx/libSceAjm.native/src/Ajm.cpp @@ -144,13 +144,13 @@ struct Instance { } }; -std::mutex g_lock; -std::map> g_instances; +std::mutex& g_lock = *new std::mutex(); +std::map>& g_instances = *new std::map>(); std::atomic g_nextContext{1}; std::atomic g_nextInstance{1}; std::atomic g_nextBatch{1}; -std::mutex g_batchLock; -std::set g_batches; +std::mutex& g_batchLock = *new std::mutex(); +std::set& g_batches = *new std::set(); enum class JobKind : std::uint32_t { Initialize = 1, diff --git a/core/libs/prx/libkernel/Apr/src/Apr.cpp b/core/libs/prx/libkernel/Apr/src/Apr.cpp index a363671b9..d4e4cd421 100644 --- a/core/libs/prx/libkernel/Apr/src/Apr.cpp +++ b/core/libs/prx/libkernel/Apr/src/Apr.cpp @@ -39,15 +39,15 @@ struct AprFile { std::uint64_t size; }; -std::mutex g_filesLock; -std::vector g_files; -std::unordered_map g_idsByPath; +std::mutex& g_filesLock = *new std::mutex(); +std::vector& g_files = *new std::vector(); +std::unordered_map& g_idsByPath = *new std::unordered_map(); // File sizes by directory, listed on first use. Titles resolve thousands of package paths at startup, // and one size query per path took over 20 s; a listing costs one directory read. Names are compared // case-insensitively, like the host file system. -std::mutex g_directoriesLock; -std::unordered_map> g_directories; +std::mutex& g_directoriesLock = *new std::mutex(); +std::unordered_map>& g_directories = *new std::unordered_map>(); std::string _foldCase(std::string text) { for (auto& character : text) { @@ -164,7 +164,7 @@ void _writeAddress(const Apr::WriteAddressCommand& command) { std::array, 256> g_counters{}; -std::mutex g_counterWrites; +std::mutex& g_counterWrites = *new std::mutex(); std::uint32_t _counter(std::uint32_t index) { return g_counters[index % g_counters.size()].load(std::memory_order_acquire); @@ -263,7 +263,7 @@ struct AmmState { }; AmmState& _amm() { - static AmmState state; + static auto& state = *new AmmState(); return state; } diff --git a/core/libs/tests/GuestAjmTeardown.cpp b/core/libs/tests/GuestAjmTeardown.cpp new file mode 100644 index 000000000..c62f9ffff --- /dev/null +++ b/core/libs/tests/GuestAjmTeardown.cpp @@ -0,0 +1,81 @@ +#include "SceTypes.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include + +namespace { + +using Initialize = int (APS5_VABI*)(std::int64_t, std::uint32_t*); +using InstanceCreate = int (APS5_VABI*)(std::uint32_t, std::uint32_t, std::uint64_t, std::uint32_t*); +using InstanceDestroy = int (APS5_VABI*)(std::uint32_t, std::uint32_t); +using BatchInitialize = int (APS5_VABI*)(void*, std::size_t, AjmBatchInfo*); +using BatchJobClearContext = int (APS5_VABI*)(AjmBatchInfo*, std::uint32_t, void*); +using BatchStart = int (APS5_VABI*)(std::uint32_t, const AjmBatchInfo*, int, AjmBatchError*, std::uint32_t*); +using BatchWait = int (APS5_VABI*)(std::uint32_t, std::uint32_t, std::uint32_t, AjmBatchError*); + +struct Functions { + Initialize initialize; + InstanceCreate instanceCreate; + InstanceDestroy instanceDestroy; + BatchInitialize batchInitialize; + BatchJobClearContext batchJobClearContext; + BatchStart batchStart; + BatchWait batchWait; +}; + +struct SidebandResult { + std::int32_t result; + std::int32_t internalResult; +}; + +Functions ajm{}; +std::uint32_t context = 0; +std::uint32_t instance = 0; + +[[noreturn]] void Fail(const char* message) { + std::fprintf(stderr, "%s\n", message); + std::fflush(stderr); + std::_Exit(1); +} + +template +T Resolve(void* library, const char* name) { + auto* symbol = dlsym(library, name); + if (symbol == nullptr) Fail(name); + return reinterpret_cast(symbol); +} + +void UseAfterStaticTeardown() { + alignas(16) std::uint8_t buffer[0x100]{}; + AjmBatchInfo info{}; + SidebandResult sideband{-1, -1}; + if (ajm.batchInitialize(buffer, sizeof(buffer), &info) != 0 || ajm.batchJobClearContext(&info, instance, &sideband) != 0) Fail("a batch cannot be built after static teardown"); + std::uint32_t batch = 0; + AjmBatchError error{}; + if (ajm.batchStart(context, &info, 0, &error, &batch) != 0) Fail("sceAjmBatchStart failed after static teardown"); + if (sideband.result != 0) Fail("an instance created before exit is unknown after static teardown"); + if (ajm.batchWait(context, batch, 0, &error) != 0) Fail("sceAjmBatchWait does not find a batch started after static teardown"); + if (ajm.instanceDestroy(context, instance) != 0) Fail("sceAjmInstanceDestroy does not find the instance after static teardown"); +} + +} + +int main(int argc, char** argv) { + if (argc != 2) Fail("usage: guest_ajm_teardown_tests "); + if (std::atexit(UseAfterStaticTeardown) != 0) Fail("atexit failed"); + auto* library = dlopen(argv[1], RTLD_NOW); + if (library == nullptr) Fail(dlerror()); + ajm = { + Resolve(library, "sceAjmInitialize"), + Resolve(library, "sceAjmInstanceCreate"), + Resolve(library, "sceAjmInstanceDestroy"), + Resolve(library, "sceAjmBatchInitialize"), + Resolve(library, "sceAjmBatchJobClearContext"), + Resolve(library, "sceAjmBatchStart"), + Resolve(library, "sceAjmBatchWait"), + }; + if (ajm.initialize(0, &context) != 0 || ajm.instanceCreate(context, 0, 0, &instance) != 0) Fail("AJM setup failed"); + return 0; +}