Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/analyze.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
run: |
make test-unit CC=clang-18 \
EXTRA_CFLAGS="$SAN -fsanitize-ignorelist=$PWD/tests/sqlite3-asan-ignorelist.txt"
- name: Leak and use-after-free scenarios (ASan/LSan)
run: |
make test-leaks CC=clang-18 \
EXTRA_CFLAGS="$SAN -fsanitize-ignorelist=$PWD/tests/sqlite3-asan-ignorelist.txt"
asan-loadable:
runs-on: ubuntu-22.04
steps:
Expand Down
30 changes: 30 additions & 0 deletions CHANGELOG-paperless.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@ Changes carried by the `paperless` branch of `stumpylog/sqlite-vec` beyond upstr

Each upstream PR is carried as one squash commit. Fixes or follow-ups to it are separate commits listed beneath it. Entries read: upstream PR, fork commit, summary.

## v0.1.10-paperless.3 (unreleased)

NEON on Linux arm64, plus memory-safety and leak fixes.

NEON on Linux arm64, ported from photostructure/sqlite-vec. Not yet built on aarch64: the aarch64 CI job is the first real build.

- [`96b0b59`](https://github.com/stumpylog/sqlite-vec/commit/96b0b59) Make `l1_int8_neon` compile with GCC, which rejects the signed/unsigned vector conversions Clang accepts. The release build on Linux arm64 uses GCC. From the GCC part of [`a5aec74`](https://github.com/photostructure/sqlite-vec/commit/a5aec74).
- [`556abed`](https://github.com/stumpylog/sqlite-vec/commit/556abed) Enable NEON on Linux arm64. The Makefile enabled it only on Darwin arm64, so Linux aarch64 builds ran the scalar loop for every distance, including the cosine float kernel paperless-ngx uses. The target comes from `$(CC) -dumpmachine`, not `uname`, so 32-bit Raspberry Pi OS is not mistaken for aarch64. Float32 distances can differ from the scalar results in the last digits. From [`5fb0b3d`](https://github.com/photostructure/sqlite-vec/commit/5fb0b3d) and [`350c67a`](https://github.com/photostructure/sqlite-vec/commit/350c67a).

Memory-safety and leak fixes. Most are ported from [photostructure/sqlite-vec](https://github.com/photostructure/sqlite-vec); they have no upstream PR, so each of those entries names the PhotoStructure commit it came from. They are hand-adapted to this tree and credit their author in the commit message. Three more leaks were found by the ASan/LeakSanitizer scenarios added here.

Ported from photostructure/sqlite-vec:

- [`6fb75ab`](https://github.com/stumpylog/sqlite-vec/commit/6fb75ab) Keep a vec0 table whose `DROP TABLE` fails. `vec0Destroy` freed the table even when SQLite kept it, so the next query, `DROP` or close used freed memory. From [`9a01d16`](https://github.com/photostructure/sqlite-vec/commit/9a01d16).
- [`0279e28`](https://github.com/stumpylog/sqlite-vec/commit/0279e28) Don't crash when vec0 cannot prepare its chunk lookup: `sqlite3_clear_bindings()` was called on a NULL statement. From [`fdc3282`](https://github.com/photostructure/sqlite-vec/commit/fdc3282).
- From [`7c03b26`](https://github.com/photostructure/sqlite-vec/commit/7c03b26), in three commits:
- [`5a7c1c5`](https://github.com/stumpylog/sqlite-vec/commit/5a7c1c5) Free the SQL string of every auxiliary column `UPDATE`.
- [`454cc85`](https://github.com/stumpylog/sqlite-vec/commit/454cc85) Close the blob handle when a long-text metadata write fails, which made `sqlite3_close()` return `SQLITE_BUSY`. Also frees the long-text statements' SQL strings and keeps the write error of the integer, float and boolean kinds.
- [`7cb658f`](https://github.com/stumpylog/sqlite-vec/commit/7cb658f) Free the rowids buffer leaked when a metadata text filter's blob read fails. The rest of `7c03b26` (`vec0_init` counters, top-k ownership) does not apply to this tree.

Fork-only, found with the ASan harness:

- [`48766d2`](https://github.com/stumpylog/sqlite-vec/commit/48766d2) Free the `_metadatachunksNN` shadow table names when a vec0 table is freed. Only `xRename` freed them, so every table with a metadata column leaked one string per column each time it was freed.
- [`7845862`](https://github.com/stumpylog/sqlite-vec/commit/7845862) Free the SQL strings of the auxiliary column `CREATE TABLE` (once per create or connect) and `INSERT` (once per insert).

Tests:

- [`4757c5a`](https://github.com/stumpylog/sqlite-vec/commit/4757c5a) Regression tests for the `DROP TABLE` and chunk-lookup crashes, from the same PhotoStructure commits. Both crashed before.
- [`64057ce`](https://github.com/stumpylog/sqlite-vec/commit/64057ce) `tests/test-leaks.c` and `make test-leaks`: leak and use-after-free scenarios, run by the `asan-unit` job. Not covered: the failed blob write behind `454cc85` and the failed blob read behind `7cb658f`, which I found no way to trigger.

## v0.1.10-paperless.2

Performance for filtered KNN (one-document chat queries no longer read the whole index), plus delete and scan improvements.
Expand Down
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ ifndef OMIT_SIMD
ifeq ($(shell uname -sm),Darwin arm64)
CFLAGS += -mcpu=apple-m1 -DSQLITE_VEC_ENABLE_NEON
endif
# NEON is in the arm64 baseline (GCC and Clang define __ARM_NEON for aarch64
# with no -m flag), so Linux needs only the define. Ask the compiler for its
# target rather than uname: 32-bit Raspberry Pi OS runs a 64-bit kernel, so
# uname says aarch64 while gcc targets 32-bit ARM, which lacks the AArch64-only
# intrinsics the NEON kernels use.
ifeq ($(shell uname -s),Linux)
ifneq ($(filter aarch64-%,$(shell $(CC) -dumpmachine)),)
CFLAGS += -DSQLITE_VEC_ENABLE_NEON
endif
endif
ifeq ($(shell uname -sm),Linux x86_64)
ifeq ($(findstring android,$(CC)),)
CFLAGS += -DSQLITE_VEC_ENABLE_AVX
Expand Down Expand Up @@ -224,6 +234,11 @@ test-loadable-watch:
test-unit: sqlite-vec.h $(prefix)
$(CC) -DSQLITE_CORE -DSQLITE_VEC_TEST -DSQLITE_VEC_ENABLE_RESCORE -DSQLITE_VEC_ENABLE_DISKANN=1 tests/test-unit.c sqlite-vec.c vendor/sqlite3.c -I./ -Ivendor $(CFLAGS) $(EXTRA_CFLAGS) $(LINK_LIBS) -o $(prefix)/test-unit && $(prefix)/test-unit

# Leak and use-after-free scenarios; meaningful when built with
# EXTRA_CFLAGS=-fsanitize=address (see .github/workflows/analyze.yaml).
test-leaks: sqlite-vec.h $(prefix)
$(CC) -DSQLITE_CORE -DSQLITE_VEC_STATIC tests/test-leaks.c sqlite-vec.c vendor/sqlite3.c -I./ -Ivendor $(CFLAGS) $(EXTRA_CFLAGS) $(LINK_LIBS) -o $(prefix)/test-leaks && $(prefix)/test-leaks

# Standalone sqlite3 CLI with vec0 compiled in. Useful for benchmarking,
# profiling (has debug symbols), and scripting without .load_extension.
# make cli
Expand Down
67 changes: 40 additions & 27 deletions sqlite-vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,23 +432,23 @@ static i32 l1_int8_neon(const void *pVect1v, const void *pVect2v,
while (pVect1 < pEnd1 - 63) {
int8x16_t v1 = vld1q_s8(pVect1);
int8x16_t v2 = vld1q_s8(pVect2);
int8x16_t diff1 = vabdq_s8(v1, v2);
acc1 = vaddq_s32(acc1, vpaddlq_u16(vpaddlq_u8(diff1)));
uint8x16_t diff1 = vreinterpretq_u8_s8(vabdq_s8(v1, v2));
acc1 = vaddq_s32(acc1, vreinterpretq_s32_u32(vpaddlq_u16(vpaddlq_u8(diff1))));

v1 = vld1q_s8(pVect1 + 16);
v2 = vld1q_s8(pVect2 + 16);
int8x16_t diff2 = vabdq_s8(v1, v2);
acc2 = vaddq_s32(acc2, vpaddlq_u16(vpaddlq_u8(diff2)));
uint8x16_t diff2 = vreinterpretq_u8_s8(vabdq_s8(v1, v2));
acc2 = vaddq_s32(acc2, vreinterpretq_s32_u32(vpaddlq_u16(vpaddlq_u8(diff2))));

v1 = vld1q_s8(pVect1 + 32);
v2 = vld1q_s8(pVect2 + 32);
int8x16_t diff3 = vabdq_s8(v1, v2);
acc3 = vaddq_s32(acc3, vpaddlq_u16(vpaddlq_u8(diff3)));
uint8x16_t diff3 = vreinterpretq_u8_s8(vabdq_s8(v1, v2));
acc3 = vaddq_s32(acc3, vreinterpretq_s32_u32(vpaddlq_u16(vpaddlq_u8(diff3))));

v1 = vld1q_s8(pVect1 + 48);
v2 = vld1q_s8(pVect2 + 48);
int8x16_t diff4 = vabdq_s8(v1, v2);
acc4 = vaddq_s32(acc4, vpaddlq_u16(vpaddlq_u8(diff4)));
uint8x16_t diff4 = vreinterpretq_u8_s8(vabdq_s8(v1, v2));
acc4 = vaddq_s32(acc4, vreinterpretq_s32_u32(vpaddlq_u16(vpaddlq_u8(diff4))));

pVect1 += 64;
pVect2 += 64;
Expand All @@ -457,8 +457,8 @@ static i32 l1_int8_neon(const void *pVect1v, const void *pVect2v,
while (pVect1 < pEnd1 - 15) {
int8x16_t v1 = vld1q_s8(pVect1);
int8x16_t v2 = vld1q_s8(pVect2);
int8x16_t diff = vabdq_s8(v1, v2);
acc1 = vaddq_s32(acc1, vpaddlq_u16(vpaddlq_u8(diff)));
uint8x16_t diff = vreinterpretq_u8_s8(vabdq_s8(v1, v2));
acc1 = vaddq_s32(acc1, vreinterpretq_s32_u32(vpaddlq_u16(vpaddlq_u8(diff))));
pVect1 += 16;
pVect2 += 16;
}
Expand Down Expand Up @@ -3924,6 +3924,8 @@ void vec0_free(vec0_vtab *p) {
for (int i = 0; i < p->numMetadataColumns; i++) {
sqlite3_free(p->metadata_columns[i].name);
p->metadata_columns[i].name = NULL;
sqlite3_free(p->shadowMetadataChunksNames[i]);
p->shadowMetadataChunksNames[i] = NULL;
}
}

Expand Down Expand Up @@ -4101,8 +4103,10 @@ int vec0_get_chunk_position(vec0_vtab *p, i64 rowid, sqlite3_value **id,
rc = SQLITE_OK;

cleanup:
sqlite3_reset(p->stmtRowidsGetChunkPosition);
sqlite3_clear_bindings(p->stmtRowidsGetChunkPosition);
if (p->stmtRowidsGetChunkPosition) {
sqlite3_reset(p->stmtRowidsGetChunkPosition);
sqlite3_clear_bindings(p->stmtRowidsGetChunkPosition);
}
return rc;
}

Expand Down Expand Up @@ -5992,6 +5996,7 @@ static int vec0_init(sqlite3 *db, void *pAux, int argc, const char *const *argv,
goto error;
}
rc = sqlite3_prepare_v2(db, zSql, -1, &stmt, NULL);
sqlite3_free(zSql);
if ((rc != SQLITE_OK) || (sqlite3_step(stmt) != SQLITE_DONE)) {
sqlite3_finalize(stmt);
*pzErr = sqlite3_mprintf(
Expand Down Expand Up @@ -6039,15 +6044,15 @@ static int vec0Destroy(sqlite3_vtab *pVtab) {
// Free up any sqlite3_stmt, otherwise DROPs on those tables will fail
vec0_free_resources(p);

// TODO(test) later: can't evidence-of here, bc always gives "SQL logic error" instead of
// provided error
// SQLite never reports a message set here (OP_VDestroy ignores zErrMsg), and
// on a table that survives a failed xDestroy the message could leak, so set
// none.
zSql = sqlite3_mprintf("DROP TABLE " VEC0_SHADOW_CHUNKS_NAME, p->schemaName,
p->tableName);
rc = sqlite3_prepare_v2(p->db, zSql, -1, &stmt, 0);
sqlite3_free((void *)zSql);
if ((rc != SQLITE_OK) || (sqlite3_step(stmt) != SQLITE_DONE)) {
rc = SQLITE_ERROR;
vtab_set_error(pVtab, "could not drop chunks shadow table");
goto done;
}
sqlite3_finalize(stmt);
Expand All @@ -6058,7 +6063,6 @@ static int vec0Destroy(sqlite3_vtab *pVtab) {
sqlite3_free((void *)zSql);
if ((rc != SQLITE_OK) || (sqlite3_step(stmt) != SQLITE_DONE)) {
rc = SQLITE_ERROR;
vtab_set_error(pVtab, "could not drop info shadow table");
goto done;
}
sqlite3_finalize(stmt);
Expand Down Expand Up @@ -6181,9 +6185,10 @@ static int vec0Destroy(sqlite3_vtab *pVtab) {

done:
sqlite3_finalize(stmt);
vec0_free(p);
// If there was an error
// SQLite keeps a table whose xDestroy fails, and later calls its other
// methods and xDisconnect, so free it only on success.
if (rc == SQLITE_OK) {
vec0_free(p);
sqlite3_free(p);
}
return rc;
Expand Down Expand Up @@ -7433,6 +7438,7 @@ int vec0_metadata_filter_text(vec0_vtab * p, sqlite3_value * value, const void *
rc = sqlite3_blob_read(rowidsBlob, rowids, sqlite3_blob_bytes(rowidsBlob), 0);
if(rc != SQLITE_OK) {
sqlite3_blob_close(rowidsBlob);
sqlite3_free(rowids);
return rc;
}
sqlite3_blob_close(rowidsBlob);
Expand Down Expand Up @@ -9590,6 +9596,7 @@ int vec0Update_InsertWriteFinalStep(vec0_vtab *p, i64 chunk_rowid,

int vec0_write_metadata_value(vec0_vtab *p, int metadata_column_idx, i64 rowid, i64 chunk_id, i64 chunk_offset, sqlite3_value * v, int isupdate) {
int rc;
sqlite3_blob * blobValue = NULL;
struct Vec0MetadataColumnDefinition * metadata_column = &p->metadata_columns[metadata_column_idx];
vec0_metadata_column_kind kind = metadata_column->kind;

Expand Down Expand Up @@ -9629,7 +9636,6 @@ int vec0_write_metadata_value(vec0_vtab *p, int metadata_column_idx, i64 rowid,
}
}

sqlite3_blob * blobValue = NULL;
rc = sqlite3_blob_open(p->db, p->schemaName, p->shadowMetadataChunksNames[metadata_column_idx], "data", chunk_id, 1, &blobValue);
if(rc != SQLITE_OK) {
goto done;
Expand Down Expand Up @@ -9678,6 +9684,9 @@ int vec0_write_metadata_value(vec0_vtab *p, int metadata_column_idx, i64 rowid,
memcpy(view+4, s, min(n, VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH-4));

rc = sqlite3_blob_write(blobValue, &view, VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH, chunk_offset * VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH);
if(rc != SQLITE_OK) {
goto done;
}
if(n > VEC0_METADATA_TEXT_VIEW_DATA_LENGTH) {
const char * zSql;

Expand All @@ -9692,6 +9701,7 @@ int vec0_write_metadata_value(vec0_vtab *p, int metadata_column_idx, i64 rowid,
}
sqlite3_stmt * stmt;
rc = sqlite3_prepare_v2(p->db, zSql, -1, &stmt, NULL);
sqlite3_free((void *)zSql);
if(rc != SQLITE_OK) {
goto done;
}
Expand All @@ -9704,6 +9714,7 @@ int vec0_write_metadata_value(vec0_vtab *p, int metadata_column_idx, i64 rowid,
rc = SQLITE_ERROR;
goto done;
}
rc = SQLITE_OK;
}
else if(prev_n > VEC0_METADATA_TEXT_VIEW_DATA_LENGTH) {
const char * zSql = sqlite3_mprintf("DELETE FROM " VEC0_SHADOW_METADATA_TEXT_DATA_NAME " WHERE rowid = ?", p->schemaName, p->tableName, metadata_column_idx);
Expand All @@ -9713,6 +9724,7 @@ int vec0_write_metadata_value(vec0_vtab *p, int metadata_column_idx, i64 rowid,
}
sqlite3_stmt * stmt;
rc = sqlite3_prepare_v2(p->db, zSql, -1, &stmt, NULL);
sqlite3_free((void *)zSql);
if(rc != SQLITE_OK) {
goto done;
}
Expand All @@ -9724,20 +9736,19 @@ int vec0_write_metadata_value(vec0_vtab *p, int metadata_column_idx, i64 rowid,
rc = SQLITE_ERROR;
goto done;
}
rc = SQLITE_OK;
}
break;
}
}

if(rc != SQLITE_OK) {

}
rc = sqlite3_blob_close(blobValue);
if(rc != SQLITE_OK) {
goto done;
}

done:
if(blobValue) {
int closeRc = sqlite3_blob_close(blobValue);
if(rc == SQLITE_OK) {
rc = closeRc;
}
}
return rc;
}

Expand Down Expand Up @@ -9984,6 +9995,7 @@ int vec0Update_Insert(sqlite3_vtab *pVTab, int argc, sqlite3_value **argv,
goto cleanup;
}
rc = sqlite3_prepare_v2(p->db, zSql, -1, &stmt, NULL);
sqlite3_free(zSql);
if(rc != SQLITE_OK) {
goto cleanup;
}
Expand Down Expand Up @@ -10567,6 +10579,7 @@ int vec0Update_UpdateAuxColumn(vec0_vtab *p, int auxiliary_column_idx, sqlite3_v
return SQLITE_NOMEM;
}
rc = sqlite3_prepare_v2(p->db, zSql, -1, &stmt, NULL);
sqlite3_free((void *)zSql);
if(rc != SQLITE_OK) {
return rc;
}
Expand Down
71 changes: 71 additions & 0 deletions tests/test-error-paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Regression tests for vec0 error paths.

Each test makes a vec0 shadow table disappear or fail, then checks that the
extension reports an error instead of crashing or using freed memory. Ported
from photostructure/sqlite-vec (tests/test-error-paths.py).
"""

import re
import sqlite3
import subprocess
import sys

import pytest

EXTENSION = "dist/vec0"


def _raises(message, error=sqlite3.OperationalError):
return pytest.raises(error, match=re.escape(message))


def test_point_query_reports_unpreparable_rowid_lookup(db):
# vec0 prepares its rowid-to-chunk lookup on first use, and called
# sqlite3_clear_bindings() on the NULL statement when that prepare failed.
db.execute("CREATE VIRTUAL TABLE test USING vec0(v float[1])")
db.execute("INSERT INTO test(rowid, v) VALUES (1, '[1]')")
db.execute("DROP TABLE test_rowids")
with _raises("could not initialize 'rowids get chunk position' statement"):
db.execute("SELECT * FROM test WHERE rowid = 1").fetchall()


_FAILED_DROP_SCRIPT = """
import sqlite3, sys
ext, shadow, then = sys.argv[1:]
db = sqlite3.connect(":memory:", isolation_level=None)
db.enable_load_extension(True)
db.load_extension(ext)
db.execute("CREATE VIRTUAL TABLE v USING vec0(x float[1], m integer, +a text)")
db.execute("INSERT INTO v(rowid, x, m, a) VALUES (1, '[1]', 2, 'a')")
db.execute(f"DROP TABLE v_{shadow}")
try:
db.execute("DROP TABLE v")
except sqlite3.OperationalError as e:
print("drop failed:", e)
if then == "drop-again":
print(db.execute("SELECT rowid, x, m, a FROM v").fetchall())
db.execute(f"CREATE TABLE v_{shadow}(x)")
db.execute("DROP TABLE v")
print(db.execute("SELECT name FROM sqlite_master WHERE name LIKE 'v%'").fetchall())
db.close()
print("closed")
"""


@pytest.mark.parametrize("then", ["close", "drop-again"])
@pytest.mark.parametrize("shadow", ["chunks", "info"])
def test_failed_drop_keeps_table_usable(shadow, then):
# vec0Destroy freed the table even when a DROP TABLE of a shadow table
# failed, but SQLite keeps a virtual table whose xDestroy fails, so the
# failed DROP TABLE itself, or the next query, DROP, or close, used freed
# memory. The script runs in a child process because the bug crashed it.
result = subprocess.run(
[sys.executable, "-c", _FAILED_DROP_SCRIPT, EXTENSION, shadow, then],
capture_output=True,
text=True,
)
output = ["drop failed: SQL logic error"]
if then == "drop-again":
output += ["[(1, b'\\x00\\x00\\x80?', 2, 'a')]", "[]"]
output += ["closed"]
assert (result.returncode, result.stdout.splitlines()) == (0, output), result.stderr
Loading
Loading