Commit Graph
51 Commits
Author SHA1 Message Date
Henrik RydgårdandClaude Opus 5 879baa6412 Add unit tests for BlockAllocator
BlockAllocator backs sceKernelAllocPartitionMemory and the various partition
allocators, and had no coverage at all. It's pure address bookkeeping - no real
memory - so it can be checked hard: after any sequence of operations the blocks
must still tile the range exactly, with no gaps, overlaps or strays, and
GetTotalFreeBytes/GetLargestFreeBlockSize must agree with what's really in the
list. ValidateAllocator() rebuilds the list through the public accessors and
asserts all of that, and it runs after every step of the randomised sections.

Covers bottom-up and top-down allocation, grain rounding, rejection of zero and
oversized requests, splitting and re-merging on free, double free and free of an
address that was never allocated, AllocAt including unaligned positions and
collisions, AllocAligned with alignments coarser than the allocator's grain,
filling the range completely and draining it, a range whose size isn't a
multiple of the grain, and two randomised churn loops - one plain alloc/free,
one mixing in aligned allocations and AllocAt to reach block layouts the simple
loop never produces. Fixed seeds, so a failure reproduces.

No bugs found - the allocator holds up. Verified the tests aren't vacuous by
injecting two plausible bugs: dropping the forward merge in MergeFreeBlocks, and
an off-by-one in the bottom-up fit check. Both are caught, the second pinpointed
to a specific churn iteration.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GZq8ZtJmFY7bkX5FVkr3P9
2026-08-17 14:50:51 +02:00
Henrik Rydgård a3e0f915af Claude being too cautious. Let's just reject bad values fully. 2026-08-11 20:08:01 +02:00
Henrik Rydgård eb68bdba0b BlockAllocator: fix null deref in GetBlockTag, bound DoState's block count
GetBlockTag() dereferenced GetBlockFromAddress()'s result without a
null check, unlike every other accessor in this file. Callers
(NetAdhocCommon.cpp, sceNet.cpp, sceNetAdhocMatching.cpp) pass the
result straight into strcmp() while recovering from a stale address
left over from an old/corrupted savestate - precisely the situation
where the address may no longer resolve to a block. Return "" instead
of dereferencing a null block, so strcmp() simply reports a mismatch
(correctly triggering those callers' recovery path) instead of
crashing.

DoState() read the saved block count directly from the savestate with
no validation before looping that many times allocating Blocks - a
corrupt/malicious savestate claiming an enormous count would drive an
effectively unbounded allocation loop. Clamp it to how many block
records could plausibly still fit in the remaining stream data.
2026-08-11 08:43:52 +02:00
Henrik Rydgård abb57c620f Adjust some log levels 2026-08-09 22:10:54 +02:00
Ren 32a1b80977 Savestate: compact BlockAllocator serialization (section v2)
Serializing the kernel memory block lists (userMemory, kernelMemory,
volatileMemory) wrote a full Section header per block and re-zeroed
each block's tag padding with strlen+memset on every save. Games keep
on the order of a thousand blocks alive, so the per-block overhead is
both measurable save time and wasted payload bytes.

Zero-pad tags once at write time instead (Block constructor and
SetAllocated), so the v2 form can store blocks raw: start, size, taken,
tag - no per-block section machinery, no per-save tag scrubbing.
Uninitialized padding still never reaches the stream, since every path
that writes a tag now clears it first.

v1 states still load through the old per-block-Section form, which is
kept unchanged.
2026-07-27 16:21:34 +02:00
Henrik Rydgård 1ca63161b3 More minor fixes 2025-02-04 10:19:34 -06:00
Henrik Rydgård 5d0316db6f Tune some log verbosity, remove timestamp on outputdebugstring logs 2024-12-03 22:11:01 +01:00
Henrik Rydgård 2e85eb5128 More static code analysis fixes 2024-10-28 17:59:06 +01:00
Henrik Rydgård e01ca5b057 Logging API change (refactor) (#19324)
* Rename LogType to Log

* Explicitly use the Log:: enum when logging. Allows for autocomplete when editing.

* Mac/ARM64 buildfix

* Do the same with the hle result log macros

* Rename the log names to mixed case while at it.

* iOS buildfix

* Qt buildfix attempt, ARM32 buildfix
2024-07-14 14:42:59 +02:00
Unknown W. Brackets 9cfcbc46e6 Global: Cleanup initialization/pointer checks.
Cleaning up a lot of cases of uninitialized data, unchecked return values
for failures, and similar.
2022-12-10 21:13:36 -08:00
Unknown W. Brackets cc1b4e695d Debugger: Correct crash with no alloc tag. 2021-02-15 15:01:23 -08:00
Unknown W. Brackets f7740edc6d Debugger: Add more metadata for memory usage. 2021-02-15 15:01:21 -08:00
Henrik Rydgård 2d06730762 Fix regression where Gran Turismo ended up recreating the framebuffer on every frame in the menu.
Should help #13458 slightly (but there's more to fix).
2020-09-21 09:56:52 +02:00
Unknown W. Brackets b8342fb8ec SaveState: Rename ChunkFile files to Serialize.
Makes more sense and less weird than ChunkFileDoMap, etc.
2020-08-10 08:04:05 +00:00
Unknown W. Brackets 4b4e3432cd SaveState: Split Do() into a separate header. 2020-08-10 08:03:41 +00:00
shenweip 77da51c418 Also correct the allocated size. 2020-08-05 19:01:00 +08:00
shenweip acc952d839 Correct the alignment size when allocate memory at a position. 2020-08-05 18:43:45 +08:00
Unknown W. Brackets fd8a0612fa SaveState: Initialize some memory that is saved.
At the very least, will help them compress better.  Also good not to leak
random memory.
2018-06-22 21:29:18 -07:00
Henrik Rydgård fdfc572b69 Misc logging improvments, minor safety check 2017-03-20 13:07:22 +01:00
Henrik Rydgard fcbc20f240 Small changes to make Common and Core build under my preliminary UWP setup. 2017-02-24 18:59:41 +01:00
Unknown W. Brackets ecd5062617 Leave free space for alignment "holes" in mem.
This allows other, small allocates to fit in these spaces, which happens
in some games (like Gods Eater Burst.)
2015-10-09 20:08:02 +08:00
Henrik Rydgård 04ce8a63e5 Workaround issue where some homebrew (like moppi-flower) would erroneously load into kernel ram where it won't fit 2015-07-02 15:55:09 +02:00
Unknown W. Brackets afe52db366 Fix some issues in sceNetAdhoc with old savestates.
This was causing the PPGe dlist to be shared with the thread, causing
weirdness and a double-free on exit.
2015-04-27 07:53:19 -07:00
Unknown W. Brackets f14361c3b8 Add a bunch more missing cstring includes. 2013-12-30 21:37:19 -08:00
Henrik Rydgard 32f479b0a6 Assorted log spam reductions.
Also let Java know of some important events. Not used for anything yet.
2013-12-04 17:43:00 +01:00
Unknown W. Brackets 50e9e45d65 Check version in each DoState() func.
They bail on PointerWrap error or bad version.
2013-09-14 20:23:03 -07:00
Unknown W. Brackets 2c763365b4 Check size is within bounds in AllocAt(). 2013-09-11 22:52:22 -07:00
Unknown W. Brackets dc3de4dbff For an off-grain alloc, properly align size. 2013-08-20 00:58:38 -07:00
Unknown W. Brackets 69abe041b7 Properly handle off-grain allocations.
This was causing sceKernelMaxFreeMemSize() to report a size that wasn't
allocatable, since sizes are upaligned to grain.
2013-08-20 00:46:13 -07:00
Unknown W. Brackets 98f99e1e3c Fix some initialization order warnings. 2013-05-31 23:14:28 -07:00
Unknown W. Brackets 90715c2ec2 Avoid a double lookup in FreeExact(). 2013-05-26 23:48:55 -07:00
Unknown W. Brackets 776c8e8533 Rewrite BlockAllocator in attempt to free faster.
And more importantly try to find that weird heap corruption, but no
luck...
2013-05-26 23:09:27 -07:00
Henrik Rydgard 43df3c9071 Lazy font loading (to speed up running unit tests...) 2013-03-06 22:26:54 +01:00
Unknown W. Brackets f2bdea74be Don't align size to grain for PMB. 2013-03-03 20:26:57 -08:00
Unknown W. Brackets 20bb472b66 Simplify reverse_iterator usage in block alloc. 2013-03-03 16:17:05 -08:00
Unknown W. Brackets 255cbec57c Fix alignment in block allocator. 2013-03-03 16:17:05 -08:00
Henrik Rydgard ba0362d817 PGF font support. Mostly an adaptation and cleanup of JPCSP's font system. 2013-02-27 17:41:39 +01:00
Unknown W. Brackets a4032c5170 Simplify savestate code so it's all automagical.
If you can't beat 'em, join 'em?
2013-02-04 08:27:01 -08:00
Unknown W. Brackets 07a5dca98e Expand out some savestate code saving non-pod. 2013-02-04 08:26:59 -08:00
Unknown W. Brackets 2a6457b6ab Cut down on h files including PointerWrap.
This makes changes to it a bit faster to build.
2013-02-04 08:26:59 -08:00
Unknown W. Brackets 4c510b5649 Clean up sceKernelFreeVpl(), not waking yet. 2013-01-18 01:38:13 -08:00
Unknown W. Brackets 592a83b081 Make sure BlockAllocator doesn't get off grain.
This should fix spurious warnings about AllocAt() when loading a game.
2013-01-18 01:38:12 -08:00
Unknown W. Brackets dcd3045cd5 Always use -1 for failed allocs in block allocator.
Before it used 0 and -1, confusing.
2013-01-18 01:38:12 -08:00
Henrik Rydgård 95c4e0b9c4 Quick build fix for gcc 2012-12-28 23:33:00 +01:00
Unknown W. Brackets bcecd881b1 Savestate cleanup + be more careful with vectors.
For some reason I debugged it going into std::string so I just made
them all more explicit which fixed everything.
2012-12-28 13:55:31 -08:00
Unknown W. Brackets d9efdf548b Add basic state for the memory kernel objects.
Not the actual memory partitions quite yet.
2012-12-28 13:52:52 -08:00
Henrik Rydgard 897bed8072 Fix crash bug in blockallocator when tag strings were too long 2012-11-07 19:10:34 +01:00
Henrik Rydgard a6a6453c60 Add custom "grain" for block allocator. Set the big block allocs to grain 256. 2012-11-07 17:35:22 +01:00
Henrik Rydgard c61d10363a Rewrite callback handling, part 1. All sorts of other fixes too. 2012-11-06 15:46:46 +01:00
Henrik Rydgard 0f04d108de Sanity checks and cleanup 2012-11-05 13:36:12 +01:00