Repository navigation
test(libc): pin that posix_memalign accepts a size of 0 - #17664
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… firmware 11.60 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Probe source (built with ps5-payload-sdk v0.45; probe.c#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ps5/kernel.h> /* only for kernel_get_fw_version() */
#ifndef PROBE_NAME
#define PROBE_NAME "unnamed"
#endif
#ifndef PROBE_BUILD
#define PROBE_BUILD "unbuilt"
#endif
/* Self limit: on timeout the runner can only close the connection, not end the process on the console.
The probe ends itself after at most PROBE_MAX_S seconds (the build script sets timeout - 10 s). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif
static void probe_alarm(int sig) {
static const char msg[] = "TIMEOUT\tprobe self limit\n";
(void)sig;
(void)!write(1, msg, sizeof msg - 1);
_exit(124);
}
static int cases = 0;
static void probe_header(void) {
char utc[32];
time_t now = time(0);
struct tm tmv;
gmtime_r(&now, &tmv);
strftime(utc, sizeof utc, "%Y-%m-%dT%H:%M:%SZ", &tmv);
printf("PROBE\t%s\tfw=0x%08x\tbuild=%s\tpid=%d\tutc=%s\n", PROBE_NAME, kernel_get_fw_version(), PROBE_BUILD,
getpid(), utc);
fflush(stdout);
}
/* CASE: id, description of the call, return value as long, errno (0 if not set), extra (hex/bytes). */
static void probe_case(const char* id, const char* call, long ret, int err, const char* extra) {
printf("CASE\t%s\t%s\t%ld\t%d\t%s\n", id, call, ret, err, extra ? extra : "-");
fflush(stdout);
cases++;
}
static void probe_end(void) {
printf("END\t%d\n", cases);
fflush(stdout);
}
/* Probe F: memalign, posix_memalign, reallocalign, malloc_usable_size and the basic malloc, calloc, realloc, free of the
system module libSceLibcInternal on firmware 11.60 (docs/dev/debt/libc.md: heap entries). Blocks come from and go back
to that module's own functions. No address is printed: only success, the number of trailing zero bits of the address
(capped at 20), the usable size relation and whether content or an output variable was kept. A symbol that is not
exported is itself the result. The measurement is of the system module in a payload process, not of a title's heap. */
#include <dlfcn.h>
#include <setjmp.h>
#include <stdint.h>
static sigjmp_buf guard_env;
static volatile int guard_sig = 0;
static void guard_handler(int s) {
guard_sig = s;
siglongjmp(guard_env, 1);
}
typedef void* (*malloc_fn)(size_t);
typedef void* (*calloc_fn)(size_t, size_t);
typedef void* (*realloc_fn)(void*, size_t);
typedef void (*free_fn)(void*);
typedef void* (*memalign_fn)(size_t, size_t);
typedef int (*pmemalign_fn)(void**, size_t, size_t);
typedef void* (*reallocalign_fn)(void*, size_t, size_t);
typedef size_t (*usable_fn)(void*);
static malloc_fn f_malloc;
static calloc_fn f_calloc;
static realloc_fn f_realloc;
static free_fn f_free;
static memalign_fn f_memalign;
static pmemalign_fn f_pmemalign;
static reallocalign_fn f_realloca;
static usable_fn f_usable;
static int ctz_addr(const void* p) {
uintptr_t v = (uintptr_t)p;
if (!v) return -1;
int n = 0;
while (!(v & 1) && n < 20) v >>= 1, n++;
return n;
}
#define RUN(stmt) \
do { \
errno = 0; \
guard_sig = 0; \
if (sigsetjmp(guard_env, 1) == 0) { \
stmt; \
} \
e = errno; \
} while (0)
static const size_t ma_align[] = {4, 8, 16, 64, 65536, 65536, 16, 0, 3, 24, (size_t)1 << 63};
static const size_t ma_size[] = {16, 16, 16, 16, 16, 65536, 0, 100, 100, 100, 100};
/* memalign cases 7 to 10 (alignment 0, 3, 24, 2^63) run last: the system allocator might abort on them */
static void memalign_cases(int from, int to) {
char extra[200], id[24], call[96];
int e;
if (!f_memalign) return;
for (int i = from; i < to; i++) {
void* p = (void*)(long)-1;
RUN(p = f_memalign(ma_align[i], ma_size[i]));
unsigned long us = (p && p != (void*)(long)-1 && f_usable && !guard_sig) ? (unsigned long)f_usable(p) : 0;
snprintf(extra, sizeof extra, "nonnull=%d addr_ctz=%d usable_ge_size=%d signal=%d", p != 0 && p != (void*)(long)-1,
ctz_addr(p == (void*)(long)-1 ? 0 : p), us >= ma_size[i], (int)guard_sig);
if (p && p != (void*)(long)-1) f_free(p);
snprintf(id, sizeof id, "1.%d", i + 1);
snprintf(call, sizeof call, "memalign(0x%zx, %zu)", ma_align[i], ma_size[i]);
probe_case(id, call, 0, e, extra);
}
}
int main(void) { signal(SIGALRM, probe_alarm);
alarm(PROBE_MAX_S);
probe_header();
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = guard_handler;
sigaction(SIGSEGV, &sa, 0);
sigaction(SIGBUS, &sa, 0);
sigaction(SIGABRT, &sa, 0);
void* h = dlopen("libSceLibcInternal.sprx", RTLD_LAZY);
if (!h) {
probe_case("0", "dlopen(libSceLibcInternal.sprx)", 0, 0, "failed");
probe_end();
return 0;
}
f_malloc = (malloc_fn)dlsym(h, "malloc");
f_calloc = (calloc_fn)dlsym(h, "calloc");
f_realloc = (realloc_fn)dlsym(h, "realloc");
f_free = (free_fn)dlsym(h, "free");
f_memalign = (memalign_fn)dlsym(h, "memalign");
f_pmemalign = (pmemalign_fn)dlsym(h, "posix_memalign");
f_realloca = (reallocalign_fn)dlsym(h, "reallocalign");
f_usable = (usable_fn)dlsym(h, "malloc_usable_size");
char line[160], extra[200], id[24], call[96];
snprintf(line, sizeof line, "malloc=%d calloc=%d realloc=%d free=%d memalign=%d posix_memalign=%d reallocalign=%d malloc_usable_size=%d",
!!f_malloc, !!f_calloc, !!f_realloc, !!f_free, !!f_memalign, !!f_pmemalign, !!f_realloca, !!f_usable);
probe_case("0", "dlsym exported (1 yes, 0 no)", 0, 0, line);
if (!f_malloc || !f_free) {
probe_end();
return 0;
}
int e;
memalign_cases(0, 7);
static const size_t pm_align[] = {8, 16, 64, 256, 4, 0, 24, 16};
static const size_t pm_size[] = {64, 64, 64, 64, 64, 64, 64, 0};
if (f_pmemalign) {
for (int i = 0; i < 8; i++) {
void* out = (void*)1;
int r = -99;
RUN(r = f_pmemalign(&out, pm_align[i], pm_size[i]));
snprintf(extra, sizeof extra, "out_unchanged=%d out_null=%d addr_ctz=%d signal=%d", out == (void*)1, out == 0, ctz_addr(out == (void*)1 ? 0 : out), (int)guard_sig);
if (out && out != (void*)1) f_free(out);
snprintf(id, sizeof id, "2.%d", i + 1);
snprintf(call, sizeof call, "posix_memalign(&out, %zu, %zu)", pm_align[i], pm_size[i]);
probe_case(id, call, r, e, extra);
}
}
if (f_realloca) {
unsigned char* p = 0;
void* q = 0;
RUN(p = (unsigned char*)f_realloca(0, 100, 16));
snprintf(extra, sizeof extra, "nonnull=%d addr_ctz=%d signal=%d", p != 0, ctz_addr(p), (int)guard_sig);
probe_case("3.1", "reallocalign(NULL, 100, 16)", 0, e, extra);
if (p) {
memset(p, 0x5a, 100);
RUN(q = f_realloca(p, 4000, 16));
int kept = q != 0;
for (int i = 0; q && i < 100; i++) kept &= ((unsigned char*)q)[i] == 0x5a;
snprintf(extra, sizeof extra, "nonnull=%d first100_kept=%d same_block=%d signal=%d", q != 0, kept, q == (void*)p, (int)guard_sig);
probe_case("3.2", "reallocalign(p, 4000, 16) grow", 0, e, extra);
if (q) p = (unsigned char*)q;
q = 0;
RUN(q = f_realloca(p, 10, 16));
kept = q != 0;
for (int i = 0; q && i < 10; i++) kept &= ((unsigned char*)q)[i] == 0x5a;
snprintf(extra, sizeof extra, "nonnull=%d first10_kept=%d same_block=%d signal=%d", q != 0, kept, q == (void*)p, (int)guard_sig);
probe_case("3.3", "reallocalign(p, 10, 16) shrink", 0, e, extra);
if (q) p = (unsigned char*)q;
q = 0;
RUN(q = f_realloca(p, 10, 256));
snprintf(extra, sizeof extra, "nonnull=%d addr_ctz=%d signal=%d", q != 0, ctz_addr(q), (int)guard_sig);
probe_case("3.4", "reallocalign(p, 10, 256) stricter alignment", 0, e, extra);
if (q) p = (unsigned char*)q;
q = (void*)(long)-1;
RUN(q = f_realloca(p, 0, 16));
snprintf(extra, sizeof extra, "result=%s same_block=%d signal=%d", q == 0 ? "NULL" : q == (void*)(long)-1 ? "none" : "nonnull", q == (void*)p, (int)guard_sig);
probe_case("3.5", "reallocalign(p, 0, 16)", 0, e, extra);
/* free only a non-null result; with a NULL result the old block may already be gone, so it is not freed again */
if (q && q != (void*)(long)-1) f_free(q);
}
}
if (f_usable) {
void* a = f_malloc(1);
void* b = f_malloc(100);
void* c = f_memalign ? f_memalign(64, 100) : 0;
void* d = 0;
if (f_pmemalign) f_pmemalign(&d, 64, 100);
void* arr[4] = {a, b, c, d};
static const size_t want[4] = {1, 100, 100, 100};
static const char* const lab[4] = {"malloc(1)", "malloc(100)", "memalign(64, 100)", "posix_memalign(64, 100)"};
for (int i = 0; i < 4; i++) {
size_t u = 0;
RUN(u = arr[i] ? f_usable(arr[i]) : 0);
snprintf(extra, sizeof extra, "usable=%zu ge_request=%d signal=%d", u, u >= want[i], (int)guard_sig);
snprintf(id, sizeof id, "4.%d", i + 1);
snprintf(call, sizeof call, "malloc_usable_size(%s)", lab[i]);
probe_case(id, call, 0, e, extra);
}
for (int i = 0; i < 4; i++)
if (arr[i]) f_free(arr[i]);
size_t u = 99;
RUN(u = f_usable(0));
snprintf(extra, sizeof extra, "usable=%zu signal=%d", u, (int)guard_sig);
probe_case("4.5", "malloc_usable_size(NULL)", 0, e, extra);
}
if (f_calloc && f_realloc) {
void* p = (void*)(long)-1;
RUN(p = f_malloc(0));
snprintf(extra, sizeof extra, "nonnull=%d signal=%d", p != 0 && p != (void*)(long)-1, (int)guard_sig);
probe_case("5.1", "malloc(0)", 0, e, extra);
if (p && p != (void*)(long)-1) {
void* q = (void*)(long)-1;
RUN(q = f_realloc(p, 0));
snprintf(extra, sizeof extra, "result=%s same_block=%d signal=%d", q == 0 ? "NULL" : q == (void*)(long)-1 ? "none" : "nonnull", q == p, (int)guard_sig);
probe_case("5.2", "realloc(malloc(0), 0)", 0, e, extra);
if (q && q != (void*)(long)-1) f_free(q);
}
p = (void*)(long)-1;
RUN(p = f_calloc((size_t)-1 / 2, 3));
snprintf(extra, sizeof extra, "nonnull=%d signal=%d", p != 0 && p != (void*)(long)-1, (int)guard_sig);
probe_case("5.3", "calloc(SIZE_MAX/2, 3) overflow", 0, e, extra);
p = (void*)(long)-1;
RUN(p = f_malloc((size_t)-1));
snprintf(extra, sizeof extra, "nonnull=%d signal=%d", p != 0 && p != (void*)(long)-1, (int)guard_sig);
probe_case("5.4", "malloc(SIZE_MAX)", 0, e, extra);
}
memalign_cases(7, 11);
probe_end();
return 0;
}stdoutThe process ended with exit value 0. |
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
Windows check of head Setup: Windows 11 x64, WinLibs GCC 15.2.0 (r7), Ninja, Release,
AI-assisted: yes. |
What
posix_memalign(&out, 16, 0)returned 0 and set the output on a PS5, andmemalign(2^63, 100)returned NULL without touchingerrno(cases 2.8 and 1.11, lines 17 and 59 of the raw output). The allocator tests coveredposix_memalignwith invalid alignments and failures, but not a size of 0; this adds that check: the result is 0, the block is not null,errnois unchanged, and the block is freed. Thememalign(2^63, 100)case is already pinned inGuestAllocatorFailures.cpp. The code already behaves this way, so no code changes; the debt entry for the allocator indocs/dev/debt/libc.mdgets one sentence with both measurements. A title process was not measured, and the alignment of the returned block is not pinned.Tested
Measured on a PS5 with firmware 11.60 (
kernel_get_fw_version0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr), on 2026-11-07 at 07:55 UTC, in the system modulelibSceLibcInternal. A title process may differ. The cases are 2.8 and 1.11, lines 17 and 59 of the raw output in the first comment.mainposix_memalign(&out, 16, 0)outset to a blockmemalign(2^63, 100)errnountouchedOn this branch, Linux (WSL, Release) on
maindf088d6df50:guest_allocator_failurespasses in 3 of 3 runs without the new check and in 3 of 3 runs with it, as it only adds an expectation for code that already behaves this way. With the code changed to refuse a size of 0 inposix_memalignthe new check fails in 3 of 3 runs, so it can fail.tools/check_conventions.py --base upstream/mainpasses. The probe source and its raw output are in the first comment. Not tested: Windows, macOS, the CI runner, a title process and any other firmware.Overlap: checked at 08:06 UTC against
maina82dcd06dband the 116 open pull requests that touchGuestAllocatorFailures.cpporlibc.md. 115 merge cleanly with this head or conflict withmainalready. #11848 (freezero) merges withmainbut conflicts with this head inlibc.md; it adds a different function, so only neighbouring debt lines collide, and whichever lands second rebases.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)AI-assisted (Claude Code).
🤖 Generated with Claude Code