Skip to content

sqlite: two reentrancy guard gaps let a callback free the object SQLite is still using #65428

Description

@TrevorBurnham

Version

main (03fb384)

Platform

Darwin fcb214bb6048 25.6.0 Darwin Kernel Version 25.6.0: Fri Jul 31 19:19:08 PDT 2026; root:xnu-12377.161.14~5/RELEASE_ARM64_T6050 arm64

Subsystem

sqlite

What steps will reproduce the bug?

Free memory that SQLite is still using from a callback:

import { DatabaseSync, constants } from 'node:sqlite';

const db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE t(a PRIMARY KEY)');
const session = db.createSession();

let once = true;
db.setAuthorizer((action, p1) => {
  if (action === constants.SQLITE_PRAGMA && p1 === 'table_xinfo' && once) {
    once = false;
    session.close();
  }
  return constants.SQLITE_OK;
});

db.exec('INSERT INTO t VALUES (1)');

How often does it reproduce? Is there a required condition?

The fault always occurs, but it's not always visible under the default allocator. Run with
MallocScribble=1 MallocPreScribble=1 MallocNanoZone=0 on macOS and you get exit 139 deterministically; without it, the script can exit 0.

What is the expected behavior? Why is that the expected behavior?

statement.close() should throw ERR_INVALID_STATE. Per doc/api/sqlite.md:

An ERR_INVALID_STATE error is thrown if this statement is currently executing, which happens
when the method is called from a callback that the statement itself triggered, such as a
user-defined function, an aggregate function, or a 'sqlite.db.query' subscriber.

What do you see instead?

A use-after-free. Under MallocScribble, lldb stops in sqlite3VdbeMemSetNull(pMem=0xaaaaaaaaaaaaaaaa) with EXC_BAD_ACCESS.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions