Skip to content

Validate vector lengths in native addInPlace/subInPlace/minInPlace - #792

Open
r-devulap wants to merge 1 commit into
mainfrom
760-fix
Open

r-devulap wants to merge 1 commit into
mainfrom
760-fix

Conversation

@r-devulap

Copy link
Copy Markdown
Contributor

Fixes #760.

The native paths passed v1.length() to native code without checking v2, so a shorter v2 could cause out-of-bounds native reads. Reject unequal lengths at the Java/native boundary, matching the Panama implementations.

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Before you submit for review:

  • Does your PR follow guidelines from CONTRIBUTIONS.md?
  • Did you summarize what this PR does clearly and concisely?
  • Did you include performance data for changes which may be performance impacting?
  • Did you include useful docs for any user-facing changes or features?
  • Did you include useful javadocs for developer oriented changes, explaining new concepts or key changes?
  • Did you rebase your branch onto the latest main for regression testing and PR submission?
  • Did you trigger regression testing via Run Bench Main and review results?
  • Did you adhere to the code formatting guidelines (TBD)
  • Did you group your changes for easy review, providing meaningful descriptions for each commit?
  • Did you ensure that all files contain the correct copyright header?
  • Did you add documentation for this feature to the release notes directory?

If you did not complete any of these, then please explain below.

The native paths passed v1.length() to native code without checking v2,
so a shorter v2 could cause out-of-bounds native reads. Reject unequal
lengths at the Java/native boundary, matching the Panama implementations.

@MarkWolters MarkWolters left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM


import static org.junit.jupiter.api.Assertions.*;

class InPlaceLengthCheckTest {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All these tests should probably be in NativeVectorUtilSupportTest.java instead of creating a new test class. The existing test class already has methods like subtractionRejectsShorterRightHandSide, so these would fit in nicely there.

return NativeSimdOps.assemble_and_sum_f32(((MemorySegmentVectorFloat) data).get(), dataBase, ((MemorySegmentByteSequence) baseOffsets).get(), baseOffsetsOffset, (long) baseOffsetsLength);
}

private static void checkSameLength(VectorFloat<?> v1, VectorFloat<?> v2) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There are other operations in this class which look like they might cause UB if called with unexpected vector sizes, but which themselves have no checks. For example pq_decoded_cosine_similarity. I haven't scrutinized the native code to verify this, but is this something we should be concerned about?

The difference is that those other functions are on the critical path for graph search, so even if we do think checks are warranted there it's probably out of scope for this PR.


@Override
public void addInPlace(VectorFloat<?> v1, VectorFloat<?> v2) {
checkSameLength(v1, v2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The native functions have HWY_RESTRICT on them. Should we also check for seg1.address() != seg2.address()?

@MarkWolters MarkWolters added the 4.1.0 Release Label label Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.1.0 Release Label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate lengths in existing native in-place vector operations

3 participants