Files
AnyPS5/core/libs/tests/MimallocRestart.cpp
T
Cesar MoraandClaude Opus 5.5 468c07712c feat(libs): let relinked Linux games opt into mimalloc
With APS5_MIMALLOC set, libkernel restarts the title with
libmimalloc.so from libs/ first in LD_PRELOAD, as it does on macOS
with DYLD_INSERT_LIBRARIES. The macOS restart becomes one
implementation with platform pieces (MimallocRestart.cpp): on Linux
the executable is the target of /proc/self/exe and the arguments
those of /proc/self/cmdline. The restart is a priority 101
constructor, so it runs before libkernel's other initializers. It
gives the standard streams back from an ANYPS5_LOG_FILE copy before
execv, which also fixes the macOS restart under that variable, and
refuses a path the preload list cannot hold.

The build copies libmimalloc.so beside the libraries except in ASan
and TSan builds, the Linux release archives ship it, and the restart
tests run on Linux too, with a new case for ANYPS5_LOG_FILE.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015W3M6NbYpmwWkzkL8YNLaF
2026-11-03 23:18:20 -06:00

37 lines
1.3 KiB
C++

#include <dlfcn.h>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <string_view>
namespace {
void Require(bool condition, const char* message) {
if (condition) return;
std::fprintf(stderr, "%s\n", message);
std::exit(1);
}
}
int main(int argc, char** argv) {
Require(argc == 2, "usage: mimalloc_restart_tests off|on|once|log");
const std::string_view mode = argv[1];
Require(std::getenv("APS5_MIMALLOC_RESTARTED") == nullptr, "the restart marker stayed in the environment");
using InHeap = bool (*)(const void*);
const auto inHeap = reinterpret_cast<InHeap>(dlsym(RTLD_DEFAULT, "mi_is_in_heap_region"));
if (mode != "on" && mode != "log") {
Require(inHeap == nullptr, "mimalloc was loaded without APS5_MIMALLOC or after a restart");
return 0;
}
Require(inHeap != nullptr, "APS5_MIMALLOC did not load mimalloc");
void* block = std::malloc(64);
const auto object = std::make_unique<int>(1);
const auto array = std::make_unique<int[]>(64);
Require(inHeap(block), "malloc did not allocate through mimalloc");
Require(inHeap(object.get()) && inHeap(array.get()), "operator new did not allocate through mimalloc");
std::free(block);
if (mode == "log") std::printf("mimalloc_restart_tests: the restarted process writes to its standard output\n");
return 0;
}