diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index f9970a384..e51d2c57b 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -104,6 +104,15 @@ endforeach() add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS}) if(BUILD_TESTING) + add_executable(mspace_tests tests/Mspace.cpp) + target_include_directories(mspace_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(mspace_tests PRIVATE libc) + configure_windows_unwind(mspace_tests) + add_test(NAME mspace COMMAND mspace_tests) + if(WIN32) + set_tests_properties(mspace PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_executable(guest_memory_tests tests/GuestMemory.cpp) target_include_directories(guest_memory_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(guest_memory_tests PRIVATE libkernel libc) diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 4b490e626..b3428c80d 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -10,6 +10,7 @@ target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/FileAndHeap.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/GuestAllocations.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/GuestHeap.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/Mspace.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/ApplicationHeap.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/ApplicationProcessParameters.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/JumpBuffer.cpp diff --git a/core/libs/prx/libc/src/Mspace.cpp b/core/libs/prx/libc/src/Mspace.cpp new file mode 100644 index 000000000..5e1119fa5 --- /dev/null +++ b/core/libs/prx/libc/src/Mspace.cpp @@ -0,0 +1,177 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include + +extern "C" int* APS5_VABI __error_nid_postfix(); + +namespace { +struct alignas(16) Block { + std::size_t capacity; + Block* next; + void* pointer; + std::size_t used; +}; +struct alignas(16) Arena { + Arena* next; + Block* first; + std::uintptr_t end; +}; +std::mutex arenaMutex; +Arena* arenas = nullptr; + +void Error(int value) { *__error_nid_postfix() = value; } +Arena* Find(void* handle) { + for (auto* arena = arenas; arena; arena = arena->next) + if (arena == handle) return arena; + Error(22); + return nullptr; +} +Block* FindBlock(Arena* arena, const void* pointer) { + if (arena && pointer) + for (auto* block = arena->first; block; block = block->next) + if (block->pointer == pointer) return block; + Error(22); + return nullptr; +} +void* Allocate(Arena* arena, std::size_t size, std::size_t alignment) { + if (!arena) return nullptr; + size = std::max(size, 1); + for (auto* block = arena->first; block; block = block->next) { + if (block->pointer) continue; + const auto start = reinterpret_cast(block + 1); + if (start > std::numeric_limits::max() - (alignment - 1)) continue; + const auto aligned = (start + alignment - 1) & ~(alignment - 1); + const auto padding = aligned - start; + if (padding > block->capacity || size > block->capacity - padding) continue; + auto consumed = padding + size; + // Splitting preserves header alignment and avoids small unusable fragments. + if (consumed <= std::numeric_limits::max() - 15) { + const auto rounded = (consumed + 15) & ~std::size_t{15}; + if (rounded <= block->capacity && block->capacity - rounded >= sizeof(Block) + 16) { + auto* tail = reinterpret_cast(start + rounded); + *tail = {block->capacity - rounded - sizeof(Block), block->next, nullptr, 0}; + block->capacity = rounded; + block->next = tail; + } + } + block->pointer = reinterpret_cast(aligned); + block->used = size; + return block->pointer; + } + Error(12); + return nullptr; +} +void Release(Arena* arena, Block* released) { + released->pointer = nullptr; + released->used = 0; + for (auto* block = arena->first; block && block->next;) { + if (!block->pointer && !block->next->pointer) { + block->capacity += sizeof(Block) + block->next->capacity; + block->next = block->next->next; + } else block = block->next; + } +} +} + +extern "C" { +void* APS5_VABI sceLibcMspaceCreate_nid_postfix(const char* name, void* base, + std::size_t size, unsigned flags) { + (void)name; + const auto start = reinterpret_cast(base); + if (!base || (start & 15) || size < sizeof(Arena) + sizeof(Block) + 16 || + size > std::numeric_limits::max() - start || flags != 0) { + Error(22); + return nullptr; + } + std::lock_guard lock(arenaMutex); + for (auto* arena = arenas; arena; arena = arena->next) { + if (start < arena->end && reinterpret_cast(arena) < start + size) { + Error(22); + return nullptr; + } + } + auto* arena = static_cast(base); + auto* block = reinterpret_cast(arena + 1); + *block = {size - sizeof(Arena) - sizeof(Block), nullptr, nullptr, 0}; + *arena = {arenas, block, start + size}; + arenas = arena; + return arena; +} + +int APS5_VABI sceLibcMspaceDestroy_nid_postfix(void* handle) { + std::lock_guard lock(arenaMutex); + for (auto** entry = &arenas; *entry; entry = &(*entry)->next) { + if (*entry == handle) { + *entry = (*entry)->next; + return 0; + } + } + Error(22); + return -1; +} + +void* APS5_VABI sceLibcMspaceMalloc_nid_postfix(void* handle, std::size_t size) { + std::lock_guard lock(arenaMutex); + return Allocate(Find(handle), size, 16); +} + +void APS5_VABI sceLibcMspaceFree_nid_postfix(void* handle, void* pointer) { + if (!pointer) return; + std::lock_guard lock(arenaMutex); + auto* arena = Find(handle); + if (auto* block = FindBlock(arena, pointer)) Release(arena, block); +} + +void* APS5_VABI sceLibcMspaceCalloc_nid_postfix(void* handle, std::size_t count, std::size_t size) { + if (size && count > std::numeric_limits::max() / size) { + Error(12); + return nullptr; + } + std::lock_guard lock(arenaMutex); + void* result = Allocate(Find(handle), count * size, 16); + if (result) std::memset(result, 0, count * size); + return result; +} + +void* APS5_VABI sceLibcMspaceRealloc_nid_postfix(void* handle, void* pointer, std::size_t size) { + std::lock_guard lock(arenaMutex); + auto* arena = Find(handle); + if (!pointer) return Allocate(arena, size, 16); + auto* block = FindBlock(arena, pointer); + if (!block) return nullptr; + if (!size) { Release(arena, block); return nullptr; } + const auto padding = reinterpret_cast(pointer) - reinterpret_cast(block + 1); + if (size <= block->capacity - padding) { block->used = size; return pointer; } + void* result = Allocate(arena, size, 16); + if (result) { + std::memcpy(result, pointer, block->used); + Release(arena, block); + } + return result; +} + +int APS5_VABI sceLibcMspacePosixMemalign_nid_postfix(void* handle, void** result, + std::size_t alignment, std::size_t size) { + if (!result || alignment < sizeof(void*) || (alignment & (alignment - 1))) return 22; + std::lock_guard lock(arenaMutex); + auto* arena = Find(handle); + if (!arena) return 22; + void* pointer = Allocate(arena, size, std::max(alignment, 16)); + if (!pointer) return 12; + *result = pointer; + return 0; +} + +std::size_t APS5_VABI sceLibcMspaceMallocUsableSize_nid_postfix(const void* pointer) { + if (!pointer) return 0; + std::lock_guard lock(arenaMutex); + for (auto* arena = arenas; arena; arena = arena->next) + for (auto* block = arena->first; block; block = block->next) + if (block->pointer == pointer) return block->used; + Error(22); + return 0; +} +} diff --git a/core/libs/tests/Mspace.cpp b/core/libs/tests/Mspace.cpp new file mode 100644 index 000000000..f286a7dc5 --- /dev/null +++ b/core/libs/tests/Mspace.cpp @@ -0,0 +1,65 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +void* APS5_VABI sceLibcMspaceCreate_nid_postfix(const char*, void*, std::size_t, unsigned); +int APS5_VABI sceLibcMspaceDestroy_nid_postfix(void*); +void* APS5_VABI sceLibcMspaceMalloc_nid_postfix(void*, std::size_t); +void* APS5_VABI sceLibcMspaceCalloc_nid_postfix(void*, std::size_t, std::size_t); +void* APS5_VABI sceLibcMspaceRealloc_nid_postfix(void*, void*, std::size_t); +void APS5_VABI sceLibcMspaceFree_nid_postfix(void*, void*); +int APS5_VABI sceLibcMspacePosixMemalign_nid_postfix(void*, void**, std::size_t, std::size_t); +std::size_t APS5_VABI sceLibcMspaceMallocUsableSize_nid_postfix(const void*); +} +static void Require(bool value) { + if (!value) { std::fputs("Mspace check failed\n", stderr); std::abort(); } +} +int main() { + alignas(16) std::array storage{}; + void* arena = sceLibcMspaceCreate_nid_postfix("test", storage.data(), storage.size(), 0); + Require(arena != nullptr); + Require(sceLibcMspaceCreate_nid_postfix("overlap", storage.data(), storage.size(), 0) == nullptr); + auto* first = static_cast(sceLibcMspaceCalloc_nid_postfix(arena, 32, 4)); + Require(first > storage.data() && first + 128 <= storage.data() + storage.size()); + for (int i = 0; i < 128; ++i) { Require(first[i] == 0); first[i] = static_cast(i); } + void* blocker = sceLibcMspaceMalloc_nid_postfix(arena, 128); + auto* grown = static_cast(sceLibcMspaceRealloc_nid_postfix(arena, first, 4096)); + Require(grown && grown != first); + for (int i = 0; i < 128; ++i) Require(grown[i] == i); + Require(sceLibcMspaceMallocUsableSize_nid_postfix(grown) >= 4096); + Require(sceLibcMspaceRealloc_nid_postfix(arena, grown, storage.size()) == nullptr); + Require(grown[127] == 127); + void* aligned = nullptr; + Require(sceLibcMspacePosixMemalign_nid_postfix(arena, &aligned, 4096, 1024) == 0); + Require((reinterpret_cast(aligned) & 4095) == 0); + void* unchanged = aligned; + Require(sceLibcMspacePosixMemalign_nid_postfix(arena, &unchanged, 3, 8) == 22 && unchanged == aligned); + Require(sceLibcMspaceCalloc_nid_postfix(arena, std::numeric_limits::max(), 2) == nullptr); + sceLibcMspaceFree_nid_postfix(arena, blocker); + sceLibcMspaceFree_nid_postfix(arena, aligned); + Require(sceLibcMspaceRealloc_nid_postfix(arena, grown, 0) == nullptr); + void* large = sceLibcMspaceMalloc_nid_postfix(arena, storage.size() - 256); + Require(large != nullptr); // freeing coalesced all the fragmented blocks + sceLibcMspaceFree_nid_postfix(arena, large); + std::array workers; + for (auto& worker : workers) worker = std::thread([&] { + for (int i = 0; i < 1000; ++i) { + void* pointer = sceLibcMspaceMalloc_nid_postfix(arena, 97); + Require(pointer != nullptr); + std::memset(pointer, 42, 97); + Require(sceLibcMspaceMallocUsableSize_nid_postfix(pointer) >= 97); + sceLibcMspaceFree_nid_postfix(arena, pointer); + } + }); + for (auto& worker : workers) worker.join(); + Require(sceLibcMspaceDestroy_nid_postfix(arena) == 0); + Require(sceLibcMspaceMalloc_nid_postfix(arena, 8) == nullptr); + Require(sceLibcMspaceCreate_nid_postfix("reuse", storage.data(), storage.size(), 0) == arena); + Require(sceLibcMspaceDestroy_nid_postfix(arena) == 0); +}