fix: avoid unaligned hnsw link access#1989
Open
jac0626 wants to merge 3 commits into
Open
Conversation
Signed-off-by: JiangChao <jacllovey@qq.com>
Contributor
Merge ProtectionsYour pull request matches the following merge protections and will not be merged until they are valid. 🟢 Require kind labelWonderful, this rule succeeded.
🟢 Require version labelWonderful, this rule succeeded.
🟢 Require linked issue for feature/bug PRsWonderful, this rule succeeded.
|
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors memory access within the HNSW algorithm to safely handle unaligned data by replacing direct pointer casting and arithmetic with specialized helper functions. New templates readUnaligned and writeUnaligned utilize std::memcpy to prevent undefined behavior, and higher-level accessors like getLinkAt and setLinkAt have been implemented to standardize link list manipulations. Feedback suggests strengthening the safety of the unaligned access templates by adding a static_assert to verify that the types being copied are trivially copyable.
Signed-off-by: JiangChao <jacllovey@qq.com>
Signed-off-by: JiangChao <jacllovey@qq.com>
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.
Summary
Fixes #1962.
This PR removes undefined behavior from HNSW packed link-list access by replacing direct typed loads/stores on potentially unaligned graph memory with small inline
std::memcpyaccessors.Root Cause
HNSW stores link-list counts and neighbor ids in a packed byte layout. Some access paths cast those byte addresses to
linklistsizeint*,unsigned short*, or integer pointers and dereference them directly. UBSan reports this as misaligned load/store even when the code happens to work on x86 hardware.Changes
charbuffers viastd::memcpyinstead of typed pointer casts.Validation
clang-format15 on the changed HNSW files.git diff --check.