Skip to content

fix: end a running statement before clearing its bindings - #25

Merged
pasichDev merged 7 commits into
mainfrom
test/sanitizers-and-boundaries
Nov 10, 2026
Merged

pasichDev merged 7 commits into
mainfrom
test/sanitizers-and-boundaries

Conversation

@pasichDev

Copy link
Copy Markdown
Owner

What and why

Bug fix. clearBindings() on a statement whose row was still current freed a bound value that the row could point at without a copy, for example SELECT ?. getText/getBlob then read freed memory: garbage text on the JVM, a heap-use-after-free under AddressSanitizer. A busy statement is now reset first, in both the JNI glue and Kotlin/Native. A read of the row after that throws "no row" instead of returning freed memory.

Behaviour change: clearBindings() during an iteration now ends it.

Tests (commonTest, so every target runs them):

  • BoundaryInputTest:

    • invalid SQL, SQL that fails only at step, SQL cut at U+0000, lone UTF-16 surrogates, 3 MB SQL;
    • U+0000 and lone surrogates in bound text, empty to 4 MiB text and blobs;
    • bad indexes, reads before/after a row, bind mid-row, reset in every state;
    • long, empty and NUL-containing paths;
    • seeded random statement and connection operations.

    The regression test for the fix is clearingBindingsWhileARowIsCurrentEndsTheRunInsteadOfLeavingTheRowDangling.

  • CloseRaceTest: close against prepare/bind/step/reset on several threads, against a long query and against a statement close. Fixed seeds, bounded by runTest timeouts.

Sanitizers:

  • jvmTestAsan now also builds on Linux, with clang and the system OpenSSL via LD_PRELOAD.
  • UBSan covers the C glue only, with -fno-sanitize-recover=all. Over the whole build it trips a benign signed overflow inside SQLite, so SQLite is left out.
  • sanitizers.yml runs it weekly and on demand, and fails on any finding.

Fuzzing:

  • ClusterFuzzLite config: .clusterfuzzlite/ and a libFuzzer harness fuzz/sealite_fuzz.c over the SQLCipher open/key/prepare/bind/step path, with a dictionary and seed corpus.
  • fuzz-pr.yml: 300 s per sanitizer on PRs that touch native or fuzz code.
  • fuzz-batch.yml: a weekly run.

CONTRIBUTING has a new "Sanitizers and fuzzing" section with the scope and the blind spots.

How it was tested

  • Full jvmTestAsan (ASan and UBSan) on Linux arm64 in Docker (Ubuntu 24.04, clang 18): 164 passed, 0 failed. With the fix reverted, ASan reports a heap-use-after-free and the task fails.
  • jvmTestAsan on macOS. jvmTest for the new and related classes on macOS.
  • The fuzz harness built with build.sh in Docker, run for 120 s under ASan and UBSan with no findings (~109k inputs).
  • zizmor and actionlint clean on the new workflows.
  • Platforms tested: jvm (macOS, Linux arm64)

Not verified:

  • The new workflows have not run on GitHub yet: the weekly sanitizer job, PR and batch fuzzing, and the ClusterFuzzLite Docker image itself.
  • The Kotlin/Native fix was not compiled or run here. This PR's Apple job covers it.
  • Windows and Android natives are not sanitized.

Checklist

  • Public API unchanged
  • No key material is logged, copied into a String or left unwiped
  • Docs and CHANGELOG-relevant behaviour described above
  • New source files carry the SPDX header

Closes #13

A row of a running statement can point at a bound value without copying
it, so clearBindings freed memory that getText and getBlob then read.
Reset the statement first (JNI and Kotlin/Native): the row is refused
with 'no row' instead. Adds boundary tests (invalid SQL, U+0000, lone
surrogates, empty and large values, long paths, statement misuse) and
close-versus-prepare/step race tests.
jvmTestAsan now also builds on Linux (clang, system OpenSSL, runtime
preloaded with LD_PRELOAD) and compiles our own C with UBSan and
-fno-sanitize-recover. The sanitizers workflow runs it weekly and on
demand on ubuntu and fails on any finding. CONTRIBUTING lists what it
covers and what it does not.
A libFuzzer harness keys a temporary or in-memory database the way the
library does and runs fuzzed SQL with fuzzed text, text16 and blob
parameters. Pull requests that touch the native code fuzz for five
minutes with ASan and UBSan, a weekly batch runs longer. Small seed
corpus and dictionary included.
@pasichDev
pasichDev enabled auto-merge (squash) November 10, 2026 12:44
The base builder image ships openssl 1.1, which sqlcipher's provider does
not build against. build.sh builds the pinned openssl from source,
statically and with the same instrumentation as the harness.
…r runs

A test function written as an expression returned the helper's result,
which Kotlin/Native and JUnit 4 on Android reject for the whole class.
The close race blocked both threads of Default on a two-core emulator,
so the closer only ran after the query finished.
@pasichDev
pasichDev enabled auto-merge (squash) November 10, 2026 14:19
@pasichDev
pasichDev merged commit 7c7391c into main Nov 10, 2026
21 checks passed
@pasichDev
pasichDev deleted the test/sanitizers-and-boundaries branch November 10, 2026 14:41
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.

test: run native sanitizers regularly and add boundary fuzz coverage

1 participant