Skip to content

Commit cdbc347

Browse files
asg017claude
andcommitted
Fix fuzzer-found bugs and CI build issues
- fuzz.yaml: embed rpath to Homebrew LLVM's libc++ so macOS binaries can find the right C++ runtime at load time (fixes dyld weak-def crash) - fuzz.yaml: add `make sqlite-vec.h` step on all platforms before building fuzz targets (the header is generated from a template, not checked in) - fuzz.yaml: drop llvm version pin on Windows so choco succeeds when a newer LLVM is already installed on the runner - sqlite-vec.c: change fvec_cleanup / fvec_cleanup_noop to take void* instead of f32* so they are ABI-compatible with vector_cleanup; removes UBSAN indirect-call errors at many call sites - sqlite-vec.c: copy BLOB data into sqlite3_malloc'd buffer in fvec_from_value instead of aliasing the raw blob pointer, fixing UBSAN misaligned-load errors when SQLite hands us an unaligned blob - sqlite-vec.c: guard npy_token_next string scan with ptr < end check before the closing-quote dereference (heap-buffer-overflow) - sqlite-vec.c: clamp vec_quantize_int8 intermediate value to [-128, 127] before casting to i8 (UBSAN out-of-range conversion) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b1a0219 commit cdbc347

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

.github/workflows/fuzz.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ jobs:
6969
- name: Generate sqlite-vec.h
7070
run: make sqlite-vec.h
7171
- name: Build fuzz targets
72-
run: make -C tests/fuzz all FUZZ_CC=/opt/homebrew/opt/llvm/bin/clang
72+
run: |
73+
LLVM=/opt/homebrew/opt/llvm
74+
make -C tests/fuzz all \
75+
FUZZ_CC=$LLVM/bin/clang \
76+
FUZZ_LDFLAGS="-Wl,-ld_classic -L$LLVM/lib/c++ -Wl,-rpath,$LLVM/lib/c++"
7377
- name: Run fuzz targets
7478
env:
7579
DYLD_LIBRARY_PATH: "/opt/homebrew/opt/llvm/lib/c++:${{ env.DYLD_LIBRARY_PATH }}"

sqlite-vec.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,9 @@ char *type_name(int type) {
692692
return "";
693693
}
694694

695-
typedef void (*fvec_cleanup)(f32 *vector);
695+
typedef void (*fvec_cleanup)(void *vector);
696696

697-
void fvec_cleanup_noop(f32 *_) { UNUSED_PARAMETER(_); }
697+
void fvec_cleanup_noop(void *_) { UNUSED_PARAMETER(_); }
698698

699699
static int fvec_from_value(sqlite3_value *value, f32 **vector,
700700
size_t *dimensions, fvec_cleanup *cleanup,
@@ -714,9 +714,15 @@ static int fvec_from_value(sqlite3_value *value, f32 **vector,
714714
sizeof(f32), bytes);
715715
return SQLITE_ERROR;
716716
}
717-
*vector = (f32 *)blob;
717+
f32 *buf = sqlite3_malloc(bytes);
718+
if (!buf) {
719+
*pzErr = sqlite3_mprintf("out of memory");
720+
return SQLITE_NOMEM;
721+
}
722+
memcpy(buf, blob, bytes);
723+
*vector = buf;
718724
*dimensions = bytes / sizeof(f32);
719-
*cleanup = fvec_cleanup_noop;
725+
*cleanup = sqlite3_free;
720726
return SQLITE_OK;
721727
}
722728

@@ -806,7 +812,7 @@ static int fvec_from_value(sqlite3_value *value, f32 **vector,
806812
if (x.length > 0) {
807813
*vector = (f32 *)x.z;
808814
*dimensions = x.length;
809-
*cleanup = (fvec_cleanup)sqlite3_free;
815+
*cleanup = sqlite3_free;
810816
return SQLITE_OK;
811817
}
812818
sqlite3_free(x.z);
@@ -1458,7 +1464,10 @@ static void vec_quantize_int8(sqlite3_context *context, int argc,
14581464
}
14591465
f32 step = (1.0 - (-1.0)) / 255;
14601466
for (size_t i = 0; i < dimensions; i++) {
1461-
out[i] = ((srcVector[i] - (-1.0)) / step) - 128;
1467+
double val = ((srcVector[i] - (-1.0)) / step) - 128;
1468+
if (val > 127.0) val = 127.0;
1469+
if (val < -128.0) val = -128.0;
1470+
out[i] = (i8)val;
14621471
}
14631472

14641473
sqlite3_result_blob(context, out, dimensions * sizeof(i8), sqlite3_free);
@@ -2718,7 +2727,7 @@ int npy_token_next(unsigned char *start, unsigned char *end,
27182727
}
27192728
ptr++;
27202729
}
2721-
if ((*ptr) != '\'') {
2730+
if (ptr >= end || (*ptr) != '\'') {
27222731
return VEC0_TOKEN_RESULT_ERROR;
27232732
}
27242733
out->start = start;

0 commit comments

Comments
 (0)