mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #166 from asimplestray/feat/libc-tail
feat(libc): implement RaiseEv, catchReturnFromMain and reallocalign
This commit is contained in:
@@ -227,10 +227,11 @@ if(BUILD_TESTING)
|
||||
add_test(NAME guest_system_exit_parameters COMMAND guest_system_exit_tests)
|
||||
add_test(NAME guest_system_exit COMMAND guest_system_exit_tests --exit)
|
||||
add_test(NAME guest_immediate_exit COMMAND guest_system_exit_tests --immediate)
|
||||
add_test(NAME guest_return_from_main COMMAND guest_system_exit_tests --return-from-main)
|
||||
set_tests_properties(guest_system_exit PROPERTIES
|
||||
PASS_REGULAR_EXPRESSION "Guest shutdown and atexit completed")
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_system_exit guest_system_exit_parameters guest_immediate_exit PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
set_tests_properties(guest_system_exit guest_system_exit_parameters guest_immediate_exit guest_return_from_main PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_executable(guest_system_service_tests tests/GuestSystemService.cpp)
|
||||
@@ -341,6 +342,15 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(guest_lifecycle PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_executable(guest_reallocalign_tests tests/GuestReallocalign.cpp)
|
||||
target_include_directories(guest_reallocalign_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_reallocalign_tests PRIVATE libc)
|
||||
configure_windows_unwind(guest_reallocalign_tests)
|
||||
add_test(NAME guest_reallocalign COMMAND guest_reallocalign_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_reallocalign PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_executable(guest_standard_stream_tests tests/GuestStandardStreams.cpp)
|
||||
target_include_directories(guest_standard_stream_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_standard_stream_tests PRIVATE libc)
|
||||
|
||||
@@ -8,11 +8,6 @@ uint32_t Need_sceLibc = 1;
|
||||
|
||||
extern "C" {
|
||||
|
||||
void APS5_VABI catchReturnFromMain_nid_postfix(int status) {
|
||||
(void)status;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
}
|
||||
|
||||
int APS5_VABI std_execute_once_nid_postfix(int* flag, int (*func)(void*, void*, void**), void* arg) {
|
||||
(void)flag;
|
||||
(void)func;
|
||||
@@ -35,11 +30,6 @@ extern "C" {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI _ZNKSt9exception6_RaiseEv_nid_postfix() {
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
APS5_EXPORT("Pu0Ecyk-7FU", libcUnknown_Pu0Ecyk_M7FU);
|
||||
int APS5_VABI libcUnknown_Pu0Ecyk_M7FU() {
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
|
||||
@@ -12,6 +12,7 @@ void* ApplicationHeapAllocate_nid_no_patch(std::size_t bytes);
|
||||
void ApplicationHeapFree_nid_no_patch(void* pointer);
|
||||
void* ApplicationHeapReallocate_nid_no_patch(void* pointer, std::size_t bytes);
|
||||
void* ApplicationHeapAlign_nid_no_patch(std::size_t alignment, std::size_t bytes);
|
||||
void* ApplicationHeapRealign_nid_no_patch(void* pointer, std::size_t bytes, std::size_t alignment);
|
||||
void* ApplicationHeapCalloc_nid_no_patch(std::size_t count, std::size_t bytes);
|
||||
int ApplicationHeapPosixAlign_nid_no_patch(void** pointer, std::size_t alignment, std::size_t bytes);
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ void* GuestHeapAllocate_nid_postfix(std::size_t bytes);
|
||||
void GuestHeapFree_nid_postfix(void* pointer);
|
||||
void* GuestHeapReallocate_nid_postfix(void* pointer, std::size_t bytes);
|
||||
void* GuestHeapAlign_nid_postfix(std::size_t alignment, std::size_t bytes);
|
||||
void* GuestHeapRealign_nid_postfix(void* pointer, std::size_t bytes, std::size_t alignment);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ using Free = void (APS5_VABI *)(void*);
|
||||
using Reallocate = void* (APS5_VABI *)(void*, std::size_t);
|
||||
using Calloc = void* (APS5_VABI *)(std::size_t, std::size_t);
|
||||
using Align = void* (APS5_VABI *)(std::size_t, std::size_t);
|
||||
using Realign = void* (APS5_VABI *)(void*, std::size_t, std::size_t);
|
||||
using PosixAlign = int (APS5_VABI *)(void**, std::size_t, std::size_t);
|
||||
using Initialize = void (APS5_VABI *)();
|
||||
|
||||
@@ -40,6 +41,7 @@ void* APS5_VABI defaultCalloc(std::size_t count, std::size_t bytes) {
|
||||
return pointer;
|
||||
}
|
||||
void* APS5_VABI defaultAlign(std::size_t alignment, std::size_t bytes) { return GuestHeap::GuestHeapAlign_nid_postfix(alignment, bytes); }
|
||||
void* APS5_VABI defaultRealign(void* pointer, std::size_t bytes, std::size_t alignment) { return GuestHeap::GuestHeapRealign_nid_postfix(pointer, bytes, alignment); }
|
||||
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; }
|
||||
@@ -184,6 +186,27 @@ void* ApplicationHeapAlign_nid_no_patch(std::size_t alignment, std::size_t bytes
|
||||
return pointer;
|
||||
}
|
||||
|
||||
void* ApplicationHeapRealign_nid_no_patch(void* pointer, std::size_t bytes, std::size_t alignment) {
|
||||
if (bytes == 0) {
|
||||
ApplicationHeapFree_nid_no_patch(pointer);
|
||||
return nullptr;
|
||||
}
|
||||
requireAlignment(alignment);
|
||||
Realign realign;
|
||||
{
|
||||
std::lock_guard lock(heapMutex);
|
||||
if (heapFailure) std::rethrow_exception(heapFailure);
|
||||
if (heapFinalized) throw std::runtime_error("application heap: allocator has been finalized");
|
||||
if (heapApi[0] == nullptr) throw std::runtime_error("application heap: allocator API is not registered");
|
||||
if (heapApi[5] != nullptr) std::memcpy(&realign, &heapApi[5], sizeof(realign));
|
||||
else realign = defaultRealign;
|
||||
}
|
||||
CallbackScope scope;
|
||||
void* result = requireAllocation(realign(pointer, bytes, alignment));
|
||||
if (reinterpret_cast<std::uintptr_t>(result) % alignment != 0) throw std::runtime_error("application heap: allocator returned a misaligned pointer");
|
||||
return result;
|
||||
}
|
||||
|
||||
void* ApplicationHeapCalloc_nid_no_patch(std::size_t count, std::size_t bytes) {
|
||||
if (bytes != 0 && count > std::numeric_limits<std::size_t>::max() / bytes) throw std::length_error("application heap: calloc size overflow");
|
||||
const auto calloc = callback<Calloc>(2);
|
||||
|
||||
@@ -173,8 +173,7 @@ void* APS5_VABI aligned_alloc_nid_postfix(size_t alignment, size_t size) {
|
||||
}
|
||||
|
||||
void* APS5_VABI reallocalign_nid_postfix(void* ptr, size_t size, size_t alignment) {
|
||||
if (ptr != nullptr) NotImplemented_nid_no_patch("reallocalign of an existing block");
|
||||
return ApplicationHeapAlign_nid_no_patch(alignment, size);
|
||||
return ApplicationHeapRealign_nid_no_patch(ptr, size, alignment);
|
||||
}
|
||||
|
||||
void* APS5_VABI calloc_nid_postfix(size_t count, size_t size) {
|
||||
|
||||
@@ -249,6 +249,21 @@ void* GuestHeapReallocate_nid_postfix(void* pointer, std::size_t bytes) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void* GuestHeapRealign_nid_postfix(void* pointer, std::size_t bytes, std::size_t alignment) {
|
||||
if (pointer == nullptr) return GuestHeapAlign_nid_postfix(alignment, bytes);
|
||||
GuestAllocations::Mutation mutation;
|
||||
const auto range = mutation.Find(pointer);
|
||||
mutation.RequireUnpinned(pointer, range.bytes);
|
||||
if (bytes == 0) {
|
||||
free(mutation, pointer);
|
||||
return nullptr;
|
||||
}
|
||||
void* result = allocate(mutation, alignment, bytes);
|
||||
std::memcpy(result, pointer, std::min(bytes, range.bytes));
|
||||
free(mutation, pointer);
|
||||
return result;
|
||||
}
|
||||
|
||||
void* GuestHeapAlign_nid_postfix(std::size_t alignment, std::size_t bytes) {
|
||||
GuestAllocations::Mutation mutation;
|
||||
return allocate(mutation, alignment, bytes);
|
||||
|
||||
@@ -112,6 +112,10 @@ void APS5_VABI exit_nid_postfix(int code) {
|
||||
LibcExit_nid_no_patch(code);
|
||||
}
|
||||
|
||||
[[noreturn]] void APS5_VABI catchReturnFromMain_nid_postfix(int status) {
|
||||
LibcExit_nid_no_patch(status);
|
||||
}
|
||||
|
||||
[[noreturn]] void abort_nid_postfix(
|
||||
uint64_t arg0, uint64_t arg1, uint64_t arg2,
|
||||
uint64_t arg3, uint64_t arg4, uint64_t arg5
|
||||
|
||||
@@ -122,6 +122,11 @@ const char* APS5_VABI _ZNKSt9exception4whatEv_nid_postfix(const LibcException::E
|
||||
if (!self) throw std::invalid_argument("exception object is null");
|
||||
LibcException::ThrowPlain(_ZTVSt9exception_nid_postfix);
|
||||
}
|
||||
|
||||
[[noreturn]] void APS5_VABI _ZNKSt9exception6_RaiseEv_nid_postfix(const LibcException::ExceptionObject* self) {
|
||||
if (!self) throw std::invalid_argument("exception object is null");
|
||||
LibcException::ThrowPlain(_ZTVSt9exception_nid_postfix);
|
||||
}
|
||||
void APS5_VABI _ZNSt9exceptionC1Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt9exception_nid_postfix.destroy; }
|
||||
void APS5_VABI _ZNSt9exceptionC2Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt9exception_nid_postfix.destroy; }
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ void* APS5_VABI _Znwm_nid_postfix(std::size_t);
|
||||
void* APS5_VABI _ZnamRKSt9nothrow_t_nid_postfix(std::size_t, const void*);
|
||||
void APS5_VABI _ZdlPv_nid_postfix(void*);
|
||||
void APS5_VABI _ZdaPv_nid_postfix(void*);
|
||||
void* ApplicationHeapRealign_nid_no_patch(void*, std::size_t, std::size_t);
|
||||
}
|
||||
|
||||
namespace {
|
||||
@@ -63,6 +64,12 @@ void* APS5_VABI align(std::size_t alignment, std::size_t bytes) {
|
||||
return allocate(bytes);
|
||||
}
|
||||
|
||||
void* APS5_VABI realign(void* pointer, std::size_t bytes, std::size_t alignment) {
|
||||
require(pointer == storage.data());
|
||||
lastAlignment = alignment;
|
||||
return allocate(bytes);
|
||||
}
|
||||
|
||||
int APS5_VABI posixAlign(void** pointer, std::size_t alignment, std::size_t bytes) {
|
||||
if (fail) return 12;
|
||||
*pointer = align(alignment, bytes);
|
||||
@@ -97,7 +104,7 @@ int main(int argc, char** argv) {
|
||||
write(replacement, 0x30, &allocateZeroed);
|
||||
write(replacement, 0x38, &reallocate);
|
||||
write(replacement, 0x40, &align);
|
||||
write(replacement, 0x48, &reallocate);
|
||||
write(replacement, 0x48, &realign);
|
||||
write(replacement, 0x50, &posixAlign);
|
||||
if (argc > 1 && std::strcmp(argv[1], "default") == 0) {
|
||||
std::array<void*, 10> partial{};
|
||||
@@ -165,6 +172,8 @@ int main(int argc, char** argv) {
|
||||
require(_ZnamRKSt9nothrow_t_nid_postfix(24, nullptr) == storage.data() && lastSize == 24);
|
||||
_ZdaPv_nid_postfix(storage.data());
|
||||
require(frees == 4);
|
||||
require(ApplicationHeapRealign_nid_no_patch(storage.data(), 48, 32) == storage.data() && lastSize == 48 && lastAlignment == 32);
|
||||
reject([] { ApplicationHeapRealign_nid_no_patch(storage.data(), 16, 4096); });
|
||||
fail = true;
|
||||
reject([] { _Znwm_nid_postfix(8); });
|
||||
reject([] { ApplicationHeapAlign_nid_no_patch(4, 64); });
|
||||
|
||||
@@ -18,6 +18,7 @@ extern "C" void __cxa_rethrow_primary_exception_nid_postfix(void*);
|
||||
|
||||
extern "C" [[noreturn]] void _ZSt14_Xout_of_rangePKc_nid_postfix(const char*);
|
||||
extern "C" [[noreturn]] void __cxa_bad_cast_nid_postfix();
|
||||
extern "C" [[noreturn]] void _ZNKSt9exception6_RaiseEv_nid_postfix(const void*);
|
||||
extern "C" void* __cxa_vec_new3_nid_postfix(std::size_t, std::size_t, std::size_t, void(*)(void*), void(*)(void*), void*(*)(std::size_t), void(*)(void*, std::size_t));
|
||||
extern "C" void __cxa_vec_delete3_nid_postfix(void*, std::size_t, std::size_t, void(*)(void*), void(*)(void*, std::size_t));
|
||||
extern "C" _Unwind_Reason_Code _Unwind_Backtrace_nid_postfix(_Unwind_Trace_Fn, void*);
|
||||
@@ -191,6 +192,10 @@ int main() {
|
||||
catch (const std::logic_error& value) { assert(std::strcmp(value.what(), "test message") == 0); }
|
||||
try { __cxa_bad_cast_nid_postfix(); }
|
||||
catch (const std::exception& value) { assert(value.what() != nullptr); }
|
||||
try { _ZNKSt9exception6_RaiseEv_nid_postfix(nullptr); assert(false); }
|
||||
catch (const std::invalid_argument&) {}
|
||||
try { _ZNKSt9exception6_RaiseEv_nid_postfix(&object); assert(false); }
|
||||
catch (const std::exception& value) { assert(std::strcmp(value.what(), "std::exception") == 0); }
|
||||
CheckRtti();
|
||||
CheckForeign();
|
||||
CheckStaticInitialization();
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "prx/libc/include/ApplicationHeap.hpp"
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
extern "C" {
|
||||
void* APS5_VABI reallocalign_nid_postfix(void*, std::size_t, std::size_t);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
void Require(bool value) {
|
||||
if (!value) throw std::runtime_error("reallocalign check failed");
|
||||
}
|
||||
|
||||
template<typename TAction>
|
||||
void Reject(TAction action) {
|
||||
bool rejected = false;
|
||||
try { action(); } catch (const std::exception&) { rejected = true; }
|
||||
Require(rejected);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
void* api[10]{};
|
||||
ApplicationHeapRegister_nid_no_patch(api);
|
||||
auto* block = static_cast<unsigned char*>(reallocalign_nid_postfix(nullptr, 64, 64));
|
||||
Require(block != nullptr && (reinterpret_cast<std::uintptr_t>(block) % 64) == 0);
|
||||
std::memset(block, 0x5a, 64);
|
||||
auto* grown = static_cast<unsigned char*>(reallocalign_nid_postfix(block, 256, 64));
|
||||
Require(grown != nullptr && (reinterpret_cast<std::uintptr_t>(grown) % 64) == 0);
|
||||
for (int index = 0; index < 64; ++index) Require(grown[index] == 0x5a);
|
||||
auto* shrunk = static_cast<unsigned char*>(reallocalign_nid_postfix(grown, 16, 16));
|
||||
Require(shrunk != nullptr && (reinterpret_cast<std::uintptr_t>(shrunk) % 16) == 0);
|
||||
for (int index = 0; index < 16; ++index) Require(shrunk[index] == 0x5a);
|
||||
Reject([&] { reallocalign_nid_postfix(shrunk, 16, 0); });
|
||||
Reject([&] { reallocalign_nid_postfix(shrunk, 16, 3); });
|
||||
Require(reallocalign_nid_postfix(shrunk, 0, 16) == nullptr);
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <cstring>
|
||||
extern "C" int APS5_VABI sceSystemServiceLoadExec(const char*, const char* const*);
|
||||
extern "C" void APS5_VABI _Exit_nid_postfix(int);
|
||||
extern "C" void APS5_VABI catchReturnFromMain_nid_postfix(int);
|
||||
namespace {
|
||||
bool cleaned = false;
|
||||
void Cleanup() { cleaned = true; }
|
||||
@@ -24,6 +25,10 @@ int main(int argc, char** argv) {
|
||||
_Exit_nid_postfix(0);
|
||||
return 2;
|
||||
}
|
||||
if (argc > 1 && std::strcmp(argv[1], "--return-from-main") == 0) {
|
||||
catchReturnFromMain_nid_postfix(0);
|
||||
return 2;
|
||||
}
|
||||
if (argc > 1) {
|
||||
LibcRegisterShutdown_nid_postfix(Cleanup);
|
||||
Require(std::atexit(VerifyExit) == 0);
|
||||
|
||||
Reference in New Issue
Block a user