mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-21 16:12:47 -05:00
A commit failure inside the guest arena threw and left the reserved range in use. The heap now resets and releases the range, and returns null with ENOMEM when the host is out of memory. reallocalign returns null with EINVAL or ENOMEM like the other allocator entry points.
42 lines
1.7 KiB
C++
42 lines
1.7 KiB
C++
#include "prx/libc/include/ApplicationHeap.hpp"
|
|
#include "prx/libc/include/general/VabiMacros.hpp"
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <limits>
|
|
#include <stdexcept>
|
|
|
|
extern "C" {
|
|
void* APS5_VABI reallocalign_nid_postfix(void*, std::size_t, std::size_t);
|
|
int* APS5_VABI __error_nid_postfix();
|
|
}
|
|
|
|
namespace {
|
|
|
|
void Require(bool value) {
|
|
if (!value) throw std::runtime_error("reallocalign check failed");
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
*__error_nid_postfix() = 0;
|
|
Require(reallocalign_nid_postfix(shrunk, 16, 0) == nullptr && *__error_nid_postfix() == 22);
|
|
*__error_nid_postfix() = 0;
|
|
Require(reallocalign_nid_postfix(shrunk, 16, 3) == nullptr && *__error_nid_postfix() == 22);
|
|
*__error_nid_postfix() = 0;
|
|
Require(reallocalign_nid_postfix(shrunk, std::numeric_limits<std::size_t>::max(), 16) == nullptr && *__error_nid_postfix() == 12);
|
|
for (int index = 0; index < 16; ++index) Require(shrunk[index] == 0x5a);
|
|
Require(reallocalign_nid_postfix(shrunk, 0, 16) == nullptr);
|
|
}
|