fix(memory): detect double-free of guarded allocations on 64-bit builds#18
Open
Rakdos8 wants to merge 1 commit into
Open
fix(memory): detect double-free of guarded allocations on 64-bit builds#18Rakdos8 wants to merge 1 commit into
Rakdos8 wants to merge 1 commit into
Conversation
The freed-block sentinel was compared against the 32-bit literal 0xdddddddd, but the block is poisoned with memset(.., 0xdd, ..) so the size_t/uintptr_t sentinel is 0xdddddddddddddddd on 64-bit. The comparison never matched, so double-free of guarded allocations went undetected on every 64-bit build. Compare against a size_t-wide pattern. Add a regression test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When memory guards are enabled,
CCPFreeWithGuardpoisons the freed block withmemset(p0, 0xdd, …)and detects asubsequent double-free by reading back the stored size and comparing it to a sentinel:
if( orgSize == 0xdddddddd )(
CCPMemory.cpp), with the same pattern in the aligned path (if( (uintptr_t)allocatedPtr == 0xdddddddd )).orgSizeis asize_tandallocatedPtrauintptr_t, so on a 64-bit build a poisoned slot reads back as0xdddddddddddddddd, while the literal0xddddddddis only 32-bit wide. The comparison can never match, so adouble-free of a guarded allocation is never detected on any 64-bit build — i.e. the feature is silently broken on
every shipping target. Activation is Windows-only (
/memoryGuards), so this is a concrete Windows 64-bit defect.Fix
Introduce a single
size_t-wide sentinelCCP_MEMORY_FREED_PATTERNand compare against it at both detection sites.The constant is defined as
static_cast<size_t>( 0xddddddddddddddddULL ), which is correct on both 64-bit (fullwidth) and 32-bit (truncates to
0xdddddddd) builds.Tests
Added
CanDetectDoubleFreeWithGuardintests/CcpMemory.cpp: allocate + free with guard, then free again, and assertthe
"already freed"error is logged. Before the fix this test fails on 64-bit (no error emitted); after the fix itpasses. It reuses the existing test infrastructure (
LogHelper,SilencedFreeWithGuard) and follows the style of theexisting guard-corruption tests. Reviewed manually; not compiled locally as the vcpkg toolchain is unavailable in
this environment.
Scope
No public API change; only an internal constant and two comparisons. Behaviour on 32-bit builds is unchanged.