Skip to content

fix: avoid unaligned hnsw link access#1989

Open
jac0626 wants to merge 3 commits into
antgroup:mainfrom
jac0626:codex/fix-hnsw-unaligned-access
Open

fix: avoid unaligned hnsw link access#1989
jac0626 wants to merge 3 commits into
antgroup:mainfrom
jac0626:codex/fix-hnsw-unaligned-access

Conversation

@jac0626
Copy link
Copy Markdown
Collaborator

@jac0626 jac0626 commented May 7, 2026

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::memcpy accessors.

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

  • Use inline unaligned scalar load/store helpers for list count access.
  • Use the same helpers for direct neighbor id reads/writes on packed link-list memory.
  • Parse serialized HNSW header fields from char buffers via std::memcpy instead of typed pointer casts.
  • Preserve the existing in-memory and serialized layout.

Validation

  • Ran clang-format 15 on the changed HNSW files.
  • Ran git diff --check.
  • Did not add new tests; the existing ASan/UBSan functest path already exposes this issue, so CI should validate that the HNSW unaligned runtime errors disappear.

Signed-off-by: JiangChao <jacllovey@qq.com>
Copilot AI review requested due to automatic review settings May 7, 2026 08:13
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 7, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Require kind label

Wonderful, this rule succeeded.
  • label~=^kind/

🟢 Require version label

Wonderful, this rule succeeded.
  • label~=^version/

🟢 Require linked issue for feature/bug PRs

Wonderful, this rule succeeded.
  • body~=(?im)(?:^|[\s\-\*])(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s*:?\s+(?:#\d+|[\w.\-]+/[\w.\-]+#\d+|https?://github\.com/[\w.\-]+/[\w.\-]+/issues/\d+)

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/algorithm/hnswlib/hnswalg.h
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@jac0626 jac0626 requested a review from Copilot May 7, 2026 08:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

Comment thread src/algorithm/hnswlib/hnswalg.h
Comment thread src/algorithm/hnswlib/hnswalg.h
Comment thread src/algorithm/hnswlib/hnswalg.h
Comment thread src/algorithm/hnswlib/hnswalg.cpp Outdated
Comment thread src/algorithm/hnswlib/hnswalg.cpp
Signed-off-by: JiangChao <jacllovey@qq.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Signed-off-by: JiangChao <jacllovey@qq.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix HNSW unaligned memory access reported by UBSan

2 participants