mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(libc): support SDK startup without allocator replacements
This commit is contained in:
@@ -357,11 +357,21 @@ if(BUILD_TESTING)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_executable(application_heap_tests EXCLUDE_FROM_ALL tests/ApplicationHeap.cpp prx/libc/src/ApplicationHeap.cpp)
|
||||
add_executable(application_heap_tests EXCLUDE_FROM_ALL tests/ApplicationHeap.cpp)
|
||||
target_include_directories(application_heap_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
set_target_properties(application_heap_tests PROPERTIES CXX_EXTENSIONS OFF)
|
||||
target_link_libraries(application_heap_tests PRIVATE libc)
|
||||
configure_windows_unwind(application_heap_tests)
|
||||
set_target_properties(application_heap_tests PROPERTIES CXX_EXTENSIONS OFF)
|
||||
if(BUILD_TESTING)
|
||||
set_target_properties(application_heap_tests PROPERTIES EXCLUDE_FROM_ALL FALSE)
|
||||
add_test(NAME application_heap_replacement COMMAND application_heap_tests)
|
||||
add_test(NAME application_heap_invalid_metadata COMMAND application_heap_tests invalid)
|
||||
add_test(NAME application_heap_default COMMAND application_heap_tests default)
|
||||
if(WIN32)
|
||||
set_tests_properties(application_heap_replacement application_heap_invalid_metadata application_heap_default
|
||||
PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_executable(guest_locale_tests EXCLUDE_FROM_ALL tests/GuestLocale.cpp)
|
||||
target_include_directories(guest_locale_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "prx/libc/include/ApplicationHeap.hpp"
|
||||
#include "prx/libc/include/GuestHeap.hpp"
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
@@ -9,6 +10,7 @@
|
||||
#include <mutex>
|
||||
#include <new>
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -28,6 +30,28 @@ Initialize heapFinalize = nullptr;
|
||||
bool heapFinalized = false;
|
||||
thread_local bool heapCallbackActive = false;
|
||||
|
||||
void* APS5_VABI defaultAllocate(std::size_t bytes) { return GuestHeap::GuestHeapAllocate_nid_postfix(bytes); }
|
||||
void APS5_VABI defaultFree(void* pointer) { GuestHeap::GuestHeapFree_nid_postfix(pointer); }
|
||||
void* APS5_VABI defaultReallocate(void* pointer, std::size_t bytes) { return GuestHeap::GuestHeapReallocate_nid_postfix(pointer, bytes); }
|
||||
void* APS5_VABI defaultCalloc(std::size_t count, std::size_t bytes) {
|
||||
if (bytes != 0 && count > std::numeric_limits<std::size_t>::max() / bytes) throw std::bad_alloc();
|
||||
auto* pointer = defaultAllocate(count * bytes);
|
||||
std::memset(pointer, 0, count * bytes);
|
||||
return pointer;
|
||||
}
|
||||
void* APS5_VABI defaultAlign(std::size_t alignment, std::size_t bytes) { return GuestHeap::GuestHeapAlign_nid_postfix(alignment, bytes); }
|
||||
int APS5_VABI defaultPosixAlign(void** pointer, std::size_t alignment, std::size_t bytes) {
|
||||
if (pointer == nullptr || alignment < sizeof(void*) || (alignment & (alignment - 1)) != 0) return 22;
|
||||
try { *pointer = defaultAlign(alignment, bytes); return 0; }
|
||||
catch (const std::bad_alloc&) { return 12; }
|
||||
}
|
||||
|
||||
std::array<void*, 10> defaultApi() {
|
||||
return {reinterpret_cast<void*>(defaultAllocate), reinterpret_cast<void*>(defaultFree),
|
||||
reinterpret_cast<void*>(defaultCalloc), reinterpret_cast<void*>(defaultReallocate),
|
||||
reinterpret_cast<void*>(defaultAlign), nullptr, reinterpret_cast<void*>(defaultPosixAlign)};
|
||||
}
|
||||
|
||||
class CallbackScope {
|
||||
public:
|
||||
CallbackScope() {
|
||||
@@ -87,8 +111,12 @@ void ApplicationHeapRegister_nid_no_patch(void* const* api) {
|
||||
if (api == nullptr) throw std::invalid_argument("application heap: null allocator API");
|
||||
std::array<void*, 10> replacement;
|
||||
std::memcpy(replacement.data(), api, sizeof(replacement));
|
||||
for (std::size_t index = 0; index < 7; ++index) {
|
||||
if (replacement[index] == nullptr) throw std::invalid_argument("application heap: incomplete allocator API");
|
||||
if (std::all_of(replacement.begin(), replacement.end(), [](const void* entry) { return entry == nullptr; })) {
|
||||
replacement = defaultApi();
|
||||
} else {
|
||||
for (std::size_t index = 0; index < 7; ++index) {
|
||||
if (replacement[index] == nullptr) throw std::invalid_argument("application heap: incomplete allocator API");
|
||||
}
|
||||
}
|
||||
std::lock_guard lock(heapMutex);
|
||||
if (heapFailure) std::rethrow_exception(heapFailure);
|
||||
|
||||
@@ -70,7 +70,7 @@ void write(std::array<std::byte, TSize>& data, std::size_t offset, TValue value)
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char**) {
|
||||
int main(int argc, char** argv) {
|
||||
reject([] { ApplicationHeapAllocate_nid_no_patch(64); });
|
||||
reject([] { ApplicationHeapRegister_nid_no_patch(nullptr); });
|
||||
std::array<std::byte, 0x40> process{};
|
||||
@@ -92,6 +92,40 @@ int main(int argc, char**) {
|
||||
write(replacement, 0x40, &align);
|
||||
write(replacement, 0x48, &reallocate);
|
||||
write(replacement, 0x50, &posixAlign);
|
||||
if (argc > 1 && std::strcmp(argv[1], "default") == 0) {
|
||||
std::array<void*, 10> partial{};
|
||||
partial[0] = reinterpret_cast<void*>(&allocate);
|
||||
reject([&] { ApplicationHeapRegister_nid_no_patch(partial.data()); });
|
||||
std::memset(replacement.data() + 0x20, 0, sizeof(partial));
|
||||
ApplicationHeapInitialize_nid_no_patch(process.data());
|
||||
require(initializes == 1);
|
||||
auto* pointer = static_cast<unsigned char*>(ApplicationHeapCalloc_nid_no_patch(7, 9));
|
||||
for (unsigned i = 0; i < 63; ++i) require(pointer[i] == 0);
|
||||
std::memset(pointer, 0x5a, 63);
|
||||
pointer = static_cast<unsigned char*>(ApplicationHeapReallocate_nid_no_patch(pointer, 150));
|
||||
for (unsigned i = 0; i < 63; ++i) require(pointer[i] == 0x5a);
|
||||
pointer = static_cast<unsigned char*>(ApplicationHeapReallocate_nid_no_patch(pointer, 11));
|
||||
for (unsigned i = 0; i < 11; ++i) require(pointer[i] == 0x5a);
|
||||
require(ApplicationHeapReallocate_nid_no_patch(pointer, 0) == nullptr);
|
||||
pointer = static_cast<unsigned char*>(ApplicationHeapReallocate_nid_no_patch(nullptr, 32));
|
||||
require(pointer != nullptr);
|
||||
ApplicationHeapFree_nid_no_patch(pointer);
|
||||
for (std::size_t alignment : {16, 64, 4096}) {
|
||||
auto* aligned = ApplicationHeapAlign_nid_no_patch(alignment, 37);
|
||||
require(reinterpret_cast<std::uintptr_t>(aligned) % alignment == 0);
|
||||
ApplicationHeapFree_nid_no_patch(aligned);
|
||||
}
|
||||
void* aligned = nullptr;
|
||||
require(ApplicationHeapPosixAlign_nid_no_patch(&aligned, 256, 99) == 0);
|
||||
require(reinterpret_cast<std::uintptr_t>(aligned) % 256 == 0);
|
||||
ApplicationHeapFree_nid_no_patch(aligned);
|
||||
ApplicationHeapFree_nid_no_patch(nullptr);
|
||||
reject([] { ApplicationHeapCalloc_nid_no_patch(SIZE_MAX, 2); });
|
||||
reject([] { ApplicationHeapAlign_nid_no_patch(3, 16); });
|
||||
ApplicationHeapInitialize_nid_no_patch(process.data());
|
||||
require(initializes == 1);
|
||||
return 0;
|
||||
}
|
||||
if (argc > 1) {
|
||||
write(replacement, 8, std::uint64_t{99});
|
||||
reject([&] { ApplicationHeapInitialize_nid_no_patch(process.data()); });
|
||||
|
||||
Reference in New Issue
Block a user