Avoid UB in LRUHandle key storage#1327
Open
qingketsing wants to merge 1 commit into
Open
Conversation
Store cache entry keys in tail storage after LRUHandle instead of using a one-byte trailing array. Treating key_data[1] as variable-length storage and copying keys longer than one byte into it writes past the declared array subobject, which is undefined behavior in C++. Add a cache test covering insertion, lookup, and deletion of a key longer than one byte.
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.
Potentially addresses #1325.
Fixes C++ undefined behavior in the LRU cache entry key storage.
LRUHandleusedchar key_data[1]as variable-length trailing storage and copied arbitrary-length keys into it(In cache.cc). For keys longer than one byte, this writes past the declared array subobject even though it remains within the malloc allocation.This patch stores keys in tail storage after
LRUHandleinstead, preserving the single-allocation layout.I do not have access to the custom HWASan runtime from the issue report, so I cannot directly verify the original sanitizer failure. The change removes the fixed trailing array pattern identified in the report and adds a cache test for keys longer than one byte.
Tested:
clang-format -i --style=file util/cache.cc util/cache_test.ccbuild/tools-venv/bin/ctest --test-dir build/cmake --output-on-failure