diff --git a/CLAUDE.md b/CLAUDE.md index 462cf7d..c5a643d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -54,6 +54,7 @@ Tier-2 hardening landed in v0.1.0; the iron rule is **opt-in / default-off so ol 3. **Opt-in file-access gate.** `NewModuleWithFileAccess(false)` sets `restrictFileAccess`; `connect` and `attach` then reject any on-disk path except the configured `database`, allowing only in-memory DSNs. `isInMemoryDSN` must parse `mode=memory` as a real query parameter (via `net/url`), **not** as a substring — a loose `strings.Contains(s, ":memory:")` was a real bypass (`file:/etc/passwd?x=:memory:`). When touching DSN classification, keep it parse-based. 4. **Backward compatibility.** `NewModule()` == `NewModuleWithFileAccess(true)`, `max_rows` defaults to unlimited. Any new safety lever must default to the historical behavior. 5. **PRAGMA config.** `journal_mode` / `synchronous` / `foreign_keys` / `busy_timeout` / `cache_size` are standard SQLite PRAGMAs applied at connect — they are not custom invented defaults. +6. **UDF register/connect serialization (PKG-29).** modernc's `Driver.Open` reads its process-global UDF map (`d.udfs`) with no lock, while `RegisterScalarFunction` (called by `register_function`) writes it. `connect` opens through `localDriverName` — a `funcMutex`-serialized wrapper (`ensureLocalDriver` / `udfSerializedDriver`) whose `Open` holds `funcMutex.RLock` while modernc reads the map, mutually exclusive with `register_function`'s `Lock`/write. This is required because a connection's `Open` is called lazily by `database/sql` (and from parallel host machines), so the "register before connect" convention doesn't hold. **Keep `connect` on `localDriverName`, never raw `"sqlite"`.** Scope/limits (documented in `ensureLocalDriver`): it protects this module's own register vs connect, not a different process-wide user of raw `"sqlite"`, nor collation/connection-hook registration (this module registers neither). ## Test organization diff --git a/example_test.go b/example_test.go index 7cc98fc..bd9f1c6 100644 --- a/example_test.go +++ b/example_test.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "strings" + "sync" "sync/atomic" "testing" "time" @@ -2633,3 +2634,68 @@ func TestNestedBeginHonorsTimeout(t *testing.T) { t.Error("second begin blocked >5s — the per-operation timeout was not honored") } } + +// raceTestRun uniquifies registered-function names across test invocations (the +// registry is process-global, so -count=2 would otherwise hit "already +// registered"). +var raceTestRun int64 + +// TestConcurrentRegisterAndConnectNoRace exercises PKG-29: register_function +// writes modernc's process-global UDF map while a connection Open reads it +// (unlocked, inside the driver). Opening through the funcMutex-serialized driver +// (ensureLocalDriver) makes the two mutually exclusive. Hammering both +// concurrently under -race must report no data race. +func TestConcurrentRegisterAndConnectNoRace(t *testing.T) { + ensureLocalDriver() + run := atomic.AddInt64(&raceTestRun, 1) + + noop := starlark.NewBuiltin("noop", func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) { + return starlark.None, nil + }) + + var wg sync.WaitGroup + stop := make(chan struct{}) + + // Openers: repeatedly open a memory DB and Ping (forces a real Driver.Open, + // which reads the global UDF map) until the registrars finish. + for i := 0; i < 4; i++ { + wg.Add(1) + go func() { + defer wg.Done() + for { + select { + case <-stop: + return + default: + } + db, err := sql.Open(localDriverName, ":memory:") + if err != nil { + t.Errorf("open: %v", err) + return + } + _ = db.Ping() + _ = db.Close() + } + }() + } + + // Registrars: register uniquely-named scalar functions (writes the map). + var regWG sync.WaitGroup + for i := 0; i < 4; i++ { + regWG.Add(1) + go func(base int) { + defer regWG.Done() + for j := 0; j < 40; j++ { + name := fmt.Sprintf("udf_race_%d_%d_%d", run, base, j) + if err := doRegisterFunction(name, noop, 0, false); err != nil { + t.Errorf("register %s: %v", name, err) + return + } + } + }(i) + } + + regWG.Wait() + close(stop) + wg.Wait() +} diff --git a/function.go b/function.go index 7a2ee7b..b7908f1 100644 --- a/function.go +++ b/function.go @@ -1,6 +1,7 @@ package sqlite import ( + "database/sql" "database/sql/driver" "fmt" "sync" @@ -29,6 +30,59 @@ var ( funcMutex sync.RWMutex ) +// localDriverName is the database/sql driver name connect() opens through: a +// funcMutex-serialized wrapper over modernc's "sqlite" driver (see below). +const localDriverName = "sqlite-udf-serialized" + +var registerLocalDriverOnce sync.Once + +// ensureLocalDriver registers the funcMutex-serialized wrapper over modernc's +// "sqlite" driver, exactly once. +// +// modernc's Driver.Open reads its process-global user-defined-function map +// (d.udfs) with NO lock, while RegisterScalarFunction writes it. +// register_function serializes writes under funcMutex, but a connection's Open — +// called lazily by database/sql, possibly from another host machine running in +// parallel — reads d.udfs unlocked, so a concurrent register_function + connect +// is a data race (potentially a fatal concurrent-map access). Opening through +// this wrapper makes every Open hold funcMutex for reading, serialized against +// register_function's write. +// +// Scope: this serializes THIS module's register_function against THIS module's +// own connections (connect opens through localDriverName). It does not, and +// cannot, protect a different component in the same process that opens the raw +// "sqlite" driver directly, nor concurrent registration of modernc collations / +// connection hooks — this module registers neither, and those globals are +// modernc's own unlocked state. A modernc connection hook that reenters +// register_function while a wrapped Open holds the read lock would deadlock; +// this module registers no connection hooks, so that path is unreachable here. +func ensureLocalDriver() { + registerLocalDriverOnce.Do(func() { + // A lazily-opened handle purely to obtain the registered driver instance + // (Open is not called here, so the empty DSN is never used). + db, err := sql.Open("sqlite", "") + if err != nil { + return // "sqlite" is always registered by the modernc import + } + inner := db.Driver() + _ = db.Close() + sql.Register(localDriverName, &udfSerializedDriver{inner: inner}) + }) +} + +// udfSerializedDriver wraps modernc's driver so a connection Open (which reads +// the global UDF map) is serialized against register_function (which writes it). +type udfSerializedDriver struct{ inner driver.Driver } + +// Open holds funcMutex for reading while the wrapped driver reads its global UDF +// map. register_function holds it for writing, so the two never run concurrently; +// concurrent Opens still proceed in parallel (a read lock). +func (d *udfSerializedDriver) Open(name string) (driver.Conn, error) { + funcMutex.RLock() + defer funcMutex.RUnlock() + return d.inner.Open(name) +} + // ============================================================================ // Function Registration // ============================================================================ diff --git a/go.mod b/go.mod index acc80a6..2e40a15 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/1set/starlet v0.2.3 - github.com/starpkg/base v0.1.2 + github.com/starpkg/base v0.1.3 github.com/tursodatabase/libsql-client-go v0.0.0-20260528064733-9d5d30a29a60 go.starlark.net v0.0.0-20260324133313-ffb3f39dd27a modernc.org/sqlite v1.31.1 diff --git a/go.sum b/go.sum index 7c4906c..05e5d14 100644 --- a/go.sum +++ b/go.sum @@ -37,8 +37,8 @@ github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6Ng github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY= github.com/spyzhov/ajson v0.9.6 h1:iJRDaLa+GjhCDAt1yFtU/LKMtLtsNVKkxqlpvrHHlpQ= github.com/spyzhov/ajson v0.9.6/go.mod h1:a6oSw0MMb7Z5aD2tPoPO+jq11ETKgXUr2XktHdT8Wt8= -github.com/starpkg/base v0.1.2 h1:LUPfBAj5JMGsau6p3uXqsEDRm7Eh5gBtsWno36QyMfw= -github.com/starpkg/base v0.1.2/go.mod h1:A8xGHH1ijQ6raz8w7MhC+4zaJwVz7XzlJjyjky2Q2dA= +github.com/starpkg/base v0.1.3 h1:hm5GQheDAfYZQbDZeOYtUc7u3fyuv3L24TBDTJUiUC0= +github.com/starpkg/base v0.1.3/go.mod h1:A8xGHH1ijQ6raz8w7MhC+4zaJwVz7XzlJjyjky2Q2dA= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tursodatabase/libsql-client-go v0.0.0-20260528064733-9d5d30a29a60 h1:TfQEwhr0Q9t+Bgs0TNk2eHZ9EGD107Mimic0kcoGS1M= diff --git a/sqlite.go b/sqlite.go index 6d7444b..5854c8e 100644 --- a/sqlite.go +++ b/sqlite.go @@ -326,7 +326,11 @@ func openDatabase(connStr string, busyTimeout float64, foreignKeys bool, journal // the DSN so every connection the pool opens is configured identically. dsn = appendPragmas(connStr, settings) } - db, err := sql.Open("sqlite", dsn) + // Open through the funcMutex-serialized driver so a lazy connection Open + // (which reads modernc's global UDF map) can't race a concurrent + // register_function call (see ensureLocalDriver). + ensureLocalDriver() + db, err := sql.Open(localDriverName, dsn) if err != nil { return nil, err }