Skip to content

fix(clear): preserve capacity contract to avoid use-after-clear segv - #759

Merged
ashvardanian merged 1 commit into
unum-cloud:main-devfrom
desertfury:fix/fuzz-clear-then-add-segv
May 24, 2026
Merged

ashvardanian merged 1 commit into
unum-cloud:main-devfrom
desertfury:fix/fuzz-clear-then-add-segv

Conversation

@desertfury

Copy link
Copy Markdown
Contributor

index_dense_gt::clear() documents that it zeroes size() while keeping capacity(). In practice it called vectors_lookup_.reset(), freeing the per-slot pointer table entirely. The very next add() then dereferenced a null vectors_lookup_[slot] (UBSan: type mismatch on a null pointer) before any caller had a chance to re-reserve.

Replaces the reset() with memset over the existing buffer: the pointers it held are stale because vectors_tape_allocator_ is being reset on the next line, but the buffer itself stays the right size so subsequent add() calls land in valid memory.

`index_dense_gt::clear()` documents that it zeros `size()` while
keeping `capacity()`. The previous implementation called
`vectors_lookup_.reset()`, freeing the per-slot pointer table entirely,
so a subsequent `add()` would dereference a null entry and segfault
before any caller could re-reserve.

Zero the entries in place instead - the pointers they held were stale
the moment `vectors_tape_allocator_.reset()` ran on the next line, but
the buffer itself stays sized so future `add()` calls land in valid
memory.

Co-Authored-By: Ash Vardanian <[email protected]>
Co-Authored-By: Mikhail Chichvarin <[email protected]>
Co-Authored-By: Mikhail Chichvarin <[email protected]>
@ashvardanian
ashvardanian force-pushed the fix/fuzz-clear-then-add-segv branch from 624c09e to acf454d Compare May 23, 2026 23:14
@ashvardanian
ashvardanian changed the base branch from main to main-dev May 23, 2026 23:17
@ashvardanian
ashvardanian merged commit 9d77be5 into unum-cloud:main-dev May 24, 2026
ashvardanian pushed a commit that referenced this pull request May 24, 2026
### Patch

- Fix: Refuse operations without reserved thread contexts (#757) (b8d3403)
- Fix: Checked arithmetic for allocation sizes (#763) (89da7c5)
- Fix: Preserve hash lookup capacity across thread reserves (#765) (3cf843b)
- Fix: Guard quantized casts against zero-magnitude inputs (#758) (0c903f4)
- Fix: Refuse missing metrics in C change-metric API (#760) (490c1b2)
- Fix: Short-circuit self-renames (#761) (830f31a)
- Fix: Keep `vectors_lookup_` capacity after `clear()` #759 (9d77be5)
- Improve: Serialize concurrent same-`Index` Python access with a mutex (47528b5)
- Improve: Test GIL-release contract and progress-callback path (a598493)
- Improve: Release Python GIL during long index operations (d8be67d)
- Fix: Restore `ring_gt::try_push` return value (18c44ee)
- Fix: Stop JavaScript `Remove` loop after exception throw (f3e1052)
- Fix: Stop `usearch_init` on `make` failure (2aa0070)
- Fix: Stop C-ABI metadata readers on failure (34889ee)
- Fix: Report OOM from C-ABI thread-limit changers (ad24056)
- Fix: Bounded probe in `equal_iterator_gt::operator++` (b779c47)
- Fix: Resize cast buffer in `change_metric` for new bytes-per-vector (d544745)
- Fix: Eager-reserve thread contexts in `index_dense_gt::make` (#755) (b296566)
titusz pushed a commit to iscc/usearch that referenced this pull request Aug 3, 2026
`index_dense_gt::clear()` documents that it zeros `size()` while
keeping `capacity()`. The previous implementation called
`vectors_lookup_.reset()`, freeing the per-slot pointer table entirely,
so a subsequent `add()` would dereference a null entry and segfault
before any caller could re-reserve.

Zero the entries in place instead - the pointers they held were stale
the moment `vectors_tape_allocator_.reset()` ran on the next line, but
the buffer itself stays sized so future `add()` calls land in valid
memory.

Co-authored-by: Mikhail Chichvarin <[email protected]>
Co-authored-by: Ash Vardanian <[email protected]>
Co-authored-by: Mikhail Chichvarin <[email protected]>
(cherry picked from commit 9d77be5)
titusz added a commit to iscc/usearch that referenced this pull request Aug 3, 2026
…() fixes

Cherry-picks from upstream unum-cloud/usearch:
- Mix integer key hashes with SplitMix64 (unum-cloud#773)
- Bounded probe in equal_iterator_gt::operator++
- Preserve hash lookup capacity across thread reserves (unum-cloud#765)
- Keep vectors_lookup_ capacity after clear() (unum-cloud#759)
- Reclaim slot_lookup_ tombstones under churn (unum-cloud#769)

Fork fixes:
- Rebuild key lookups after compact(), preserve the nodes buffer
  capacity, and abort cleanly on progress cancellation (#9)
xet7 pushed a commit to xet7/usearch that referenced this pull request Nov 12, 2026
The 2.x line released v2.25.2 through v2.26.6 after 3.0 branched at
v2.25.1, and its public main ends at 33d3ab6. Installs place headers
under GNUInstallDirs' include directory (14f9807, unum-cloud#800), the README
lists the integrations 2.x added (0fd2f32, unum-cloud#795), and CONTRIBUTING
states the commit conventions in place of stale links (06ad060, unum-cloud#748).

Its correctness fixes already hold on 3.0's code:

- Keys move with their entries when `compact` permutes slots
  (0a4baa6, unum-cloud#795), and the insert-only key table never rehashes
  (1633e25, unum-cloud#822; 9d77be5, unum-cloud#759)
- File loads bound every column slice from the header (9c25176, unum-cloud#824;
  64d10d8, unum-cloud#776), and allocation sizes are checked (89da7c5, unum-cloud#763)
- JavaScript keys are range-checked (b65a420, unum-cloud#812), Go filters ride
  `cgo.Handle`s (ad77fe7, unum-cloud#825), `splitmix64_t` survives `windows.h`
  (0f1c5f2, unum-cloud#806), and integer key hashes mix with SplitMix64
  (9fc3500, unum-cloud#773)
- C install targets and relocatable pkg-config (4160e47, 679e190,
  unum-cloud#800), the GIL released in long calls (d8be67d, unum-cloud#764), and graph
  cursors in C and Rust (55c43f7, unum-cloud#748)

Thread-context reserves, tombstones, renames, `change_metric`, OpenMP
metrics and hand-written BF16 casts are absent from 3.0, so their
fixes have nothing to land on. The Rust `compact()` binding (749d03b,
unum-cloud#771) waits for all bindings to wrap `usearch_dense_compact`.

The README's performance lead now quotes the SIFT 100M rows at 99%
recall on a B300 and on 192 Graviton5 cores beside FAISS HNSW, and
the verification notes link ForkUnion's `main`.

The sibling pins move to the libraries' own merges with their released
lines, so this line builds against them.

Co-Authored-By: Jonathan Ringer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants