Skip to content

feat(c-abi): add kernel_abi_version() for dynamic-linking consumers - #285

Closed
msrathore-db wants to merge 1 commit into
mainfrom
abi-version-pr
Closed

msrathore-db wants to merge 1 commit into
mainfrom
abi-version-pr

Conversation

@msrathore-db

Copy link
Copy Markdown
Collaborator

What

Adds a single C-ABI function, kernel_abi_version(), to the kernel's C ABI:

uint32_t kernel_abi_version(void);   // returns 1; bumped on any breaking C-ABI change

Exported from both the cdylib and staticlib, declared in include/databricks_kernel.h. ~35 lines, purely additive — no existing symbol, struct, or signature changes.

Why

Consumers that link the kernel from a separately-distributed prebuilt archive — the Go SQL driver (via the new databricks-sql-kernel-bindings per-platform Go modules) and eventually the OSS ODBC driver — need to detect an ABI mismatch (a wrong or stale archive) at connect time rather than crashing on the first mismatched struct/function call.

  • A statically-linked, build-from-source consumer is protected by the compiler — the archive always matches the header it was built against.
  • A consumer that links a prebuilt archive shipped independently of the app is not: the archive and the app binary can drift. Without a version check, a mismatch is silent memory corruption; with it, it's a clean "wrong kernel version" error at connect.

kernel_abi_version() is a monotonic C-ABI version (starts at 1, bumped on any breaking C-ABI change — added/removed/renumbered function, changed struct layout/signature). It is intentionally not the crate semver; it tracks only the C-ABI shape. It takes no pointers and reads no shared state, so it's always safe to call immediately after load.

Consumer

The Go driver's kernel backend calls it once at OpenSession (checkKernelABIVersion()), failing with a clear error on mismatch. This is the missing piece that lets the driver ship the kernel as prebuilt per-platform archives (see databricks/databricks-sql-go PR #440 and the databricks-sql-kernel-bindings repo).

Testing

Compiles cleanly on latest main (cargo check --no-default-features --features tls-rustls). The function has been exercised end-to-end: the Go driver links archives built with this symbol and runs real queries (SELECT 1 + full data-type suite) against a live warehouse on all 5 target platforms (darwin arm64/amd64, linux amd64/arm64, windows amd64) — the ABI check returns 1 and connect proceeds.

This pull request and its description were written by Isaac.

Consumers that load the kernel as a shared library (.so/.dylib/.dll) at run
time — the Go SQL driver and ODBC — need to detect an ABI mismatch (a wrong or
stale shared library dropped next to the binary) at load rather than crashing on
the first mismatched struct/function. A statically-linked consumer is protected
by the compiler; a dynamically linked one is not.

kernel_abi_version() returns a monotonic C-ABI version (starts at 1, bumped on
any breaking C ABI change — added/removed/renumbered function, changed struct
layout or signature). It is intentionally NOT the crate semver; it tracks only
the C ABI shape. This matters most while the C ABI is pre-1.0, where the header
itself states the ABI 'may change freely.'

Takes no pointers and reads no shared state, so it is always safe to call
immediately after dlopen. Exported in both the cdylib and staticlib; declared in
include/databricks_kernel.h next to kernel_init_logging.

Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 2 Low

Looks good — clean, purely-additive C-ABI accessor with correct extern "C"/#[no_mangle] and no unsafe (takes no pointers). Two low-severity notes: the header exposes no compile-time constant for consumers to compare the runtime value against, and no unit test accompanies the new symbol.

* Bumped on every breaking C ABI change (added/removed/renumbered function,
* changed struct layout or signature). Not the crate semver — tracks only
* the C ABI shape. Starts at 1. Takes no pointers; always safe to call. */
uint32_t kernel_abi_version(void);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — The header declares the runtime accessor kernel_abi_version() but exposes no companion compile-time constant (e.g. #define DATABRICKS_KERNEL_ABI_VERSION 1). The documented usage is to compare the runtime value against "the version it was built against", but a C/ODBC consumer building against this header has nothing in the header to compare against — it must hardcode the literal 1 itself, which is exactly the drift the check is meant to prevent (a stale header would keep comparing against a stale hardcoded constant).

Consider emitting the constant into the header alongside the declaration so consumers can write if (kernel_abi_version() != DATABRICKS_KERNEL_ABI_VERSION) { ... }. The Rust side already has KERNEL_C_ABI_VERSION; keeping the header macro in lockstep with it (the same way the KernelStatusCode enum is kept in lockstep) closes the loop for the header's own consumers.

Comment thread src/c_abi/session.rs
/// # Safety
/// None — takes no pointers and reads no shared state.
#[no_mangle]
pub extern "C" fn kernel_abi_version() -> u32 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — No test accompanies the new symbol. A one-line unit test asserting kernel_abi_version() == KERNEL_C_ABI_VERSION (and == 1) is cheap and guards against an accidental value/type regression and confirms the symbol links — mirroring the napi version() test at napi/src/lib.rs:72. Very minor given the function just returns a const, but the repo convention is that new src/ behavior carries coverage.

@msrathore-db

Copy link
Copy Markdown
Collaborator Author

Closing: the Go driver dropped the ABI-version check (databricks/databricks-sql-go#440), so kernel_abi_version() is not needed right now. The per-platform bindings archive is version-pinned via go.mod. Can revisit if a dynamic-linking consumer later needs a load-time ABI guard.

@msrathore-db
msrathore-db deleted the abi-version-pr branch August 23, 2026 14:37

This branch was previously deployed

1 inactive deployment
azure-prod — ec43c5ce Deployed Aug 23, 2026 by oss-reviewer-bot[bot] via followup #957
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.

1 participant