-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[scudo] Add config option to modify get usable size behavior #158710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cferris1000
wants to merge
1
commit into
llvm:main
Choose a base branch
from
cferris1000:usable_size
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -706,19 +706,24 @@ class Allocator { | |
if (!getChunkFromBlock(Block, &Chunk, &Header) && | ||
!getChunkFromBlock(addHeaderTag(Block), &Chunk, &Header)) | ||
return; | ||
} else { | ||
if (!getChunkFromBlock(addHeaderTag(Block), &Chunk, &Header)) | ||
return; | ||
} | ||
if (Header.State == Chunk::State::Allocated) { | ||
uptr TaggedChunk = Chunk; | ||
if (allocatorSupportsMemoryTagging<AllocatorConfig>()) | ||
TaggedChunk = untagPointer(TaggedChunk); | ||
if (useMemoryTagging<AllocatorConfig>(Primary.Options.load())) | ||
TaggedChunk = loadTag(Chunk); | ||
Callback(TaggedChunk, getSize(reinterpret_cast<void *>(Chunk), &Header), | ||
Arg); | ||
} | ||
} else if (!getChunkFromBlock(addHeaderTag(Block), &Chunk, &Header)) | ||
return; | ||
|
||
if (Header.State != Chunk::State::Allocated) | ||
return; | ||
|
||
uptr TaggedChunk = Chunk; | ||
if (allocatorSupportsMemoryTagging<AllocatorConfig>()) | ||
TaggedChunk = untagPointer(TaggedChunk); | ||
uptr Size; | ||
if (UNLIKELY(useMemoryTagging<AllocatorConfig>(Primary.Options.load()))) { | ||
TaggedChunk = loadTag(Chunk); | ||
Size = getSize(reinterpret_cast<void *>(Chunk), &Header); | ||
} else if (AllocatorConfig::getExactUsableSize()) | ||
Size = getSize(reinterpret_cast<void *>(Chunk), &Header); | ||
else | ||
Size = getUsableSize(reinterpret_cast<void *>(Chunk), &Header); | ||
Callback(TaggedChunk, Size, Arg); | ||
}; | ||
Primary.iterateOverBlocks(Lambda); | ||
Secondary.iterateOverBlocks(Lambda); | ||
|
@@ -759,6 +764,22 @@ class Allocator { | |
return false; | ||
} | ||
|
||
ALWAYS_INLINE uptr getUsableSize(const void *Ptr, | ||
Chunk::UnpackedHeader *Header) { | ||
void *BlockBegin = getBlockBegin(Ptr, Header); | ||
if (LIKELY(Header->ClassId)) { | ||
return SizeClassMap::getSizeByClassId(Header->ClassId) - | ||
(reinterpret_cast<uptr>(Ptr) - reinterpret_cast<uptr>(BlockBegin)); | ||
} | ||
|
||
uptr UntaggedPtr = reinterpret_cast<uptr>(Ptr); | ||
if (allocatorSupportsMemoryTagging<AllocatorConfig>()) { | ||
UntaggedPtr = untagPointer(UntaggedPtr); | ||
BlockBegin = untagPointer(BlockBegin); | ||
} | ||
return SecondaryT::getBlockEnd(BlockBegin) - UntaggedPtr; | ||
} | ||
|
||
// Return the usable size for a given chunk. Technically we lie, as we just | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we also want to revise the comment a little bit? |
||
// report the actual size of a chunk. This is done to counteract code actively | ||
// writing past the end of a chunk (like sqlite3) when the usable size allows | ||
|
@@ -768,7 +789,26 @@ class Allocator { | |
if (UNLIKELY(!Ptr)) | ||
return 0; | ||
|
||
return getAllocSize(Ptr); | ||
if (AllocatorConfig::getExactUsableSize() || | ||
UNLIKELY(useMemoryTagging<AllocatorConfig>(Primary.Options.load()))) | ||
return getAllocSize(Ptr); | ||
|
||
initThreadMaybe(); | ||
|
||
#ifdef GWP_ASAN_HOOKS | ||
if (UNLIKELY(GuardedAlloc.pointerIsMine(Ptr))) | ||
return GuardedAlloc.getSize(Ptr); | ||
#endif // GWP_ASAN_HOOKS | ||
|
||
Ptr = getHeaderTaggedPointer(const_cast<void *>(Ptr)); | ||
Chunk::UnpackedHeader Header; | ||
Chunk::loadHeader(Cookie, Ptr, &Header); | ||
|
||
// Getting the alloc size of a chunk only makes sense if it's allocated. | ||
if (UNLIKELY(Header.State != Chunk::State::Allocated)) | ||
reportInvalidChunkState(AllocatorAction::Sizing, Ptr); | ||
|
||
return getUsableSize(Ptr, &Header); | ||
} | ||
|
||
uptr getAllocSize(const void *Ptr) { | ||
|
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking if we want to make this enabled when it's MTE, disabled otherwise. And make this configurable when we get more evidence that this is useful. what do you think?