From ec43c5ce0551f6c9fb94f9a8aba5ee794a6cae32 Mon Sep 17 00:00:00 2001 From: Madhavendra Rathore Date: Sun, 23 Aug 2026 16:27:18 +0530 Subject: [PATCH] feat(c-abi): add kernel_abi_version() for dynamic-linking consumers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- include/databricks_kernel.h | 10 ++++++++++ src/c_abi/session.rs | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/databricks_kernel.h b/include/databricks_kernel.h index fb766388..a90e71d2 100644 --- a/include/databricks_kernel.h +++ b/include/databricks_kernel.h @@ -350,6 +350,16 @@ KernelStatusCode kernel_session_config_set_custom_header(KernelSessionConfig* co * subscriber was already installed (no kernel subscriber installed). */ KernelStatusCode kernel_init_logging(const char* level, const char* file_path); +/* The C ABI version this library implements. A consumer that loads the + * kernel as a shared library (.so/.dylib/.dll) at run time should call this + * right after loading and refuse to proceed if the value differs from the + * version it was built against — turning an ABI mismatch (a wrong or stale + * shared library) into a clear error instead of silent memory corruption. + * 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); + /* ─── Proxy / TLS (optional) ────────────────────────────────────────── * * All optional; defaults are: no explicit proxy (reqwest honours diff --git a/src/c_abi/session.rs b/src/c_abi/session.rs index 9467c97b..ede1e6db 100644 --- a/src/c_abi/session.rs +++ b/src/c_abi/session.rs @@ -831,6 +831,31 @@ pub unsafe extern "C" fn kernel_init_logging( } } +/// The C ABI version this shared library implements. A consumer that loads the +/// kernel as a shared library (`.so`/`.dylib`/`.dll`) at run time should call +/// this immediately after `dlopen` and refuse to proceed if the value does not +/// match the version it was built against — turning an ABI mismatch (a wrong or +/// stale library dropped next to the binary) into a clear, actionable error +/// instead of silent memory corruption or a crash. +/// +/// This matters most while the C ABI is pre-1.0: per this crate's own docs the +/// struct layouts and function set "may change freely" until v1.0, so a +/// statically-linked consumer is protected by the compiler but a dynamically +/// linked one needs a runtime check. Bump this on every breaking C ABI change +/// (added/removed/renumbered function, changed struct layout or signature). +/// It is intentionally NOT the crate's semver — it tracks only the C ABI shape. +/// +/// # Safety +/// None — takes no pointers and reads no shared state. +#[no_mangle] +pub extern "C" fn kernel_abi_version() -> u32 { + KERNEL_C_ABI_VERSION +} + +/// Current C ABI version. See `kernel_abi_version`. Start at 1; increment on any +/// breaking change to the C ABI (databricks_kernel.h). +pub const KERNEL_C_ABI_VERSION: u32 = 1; + /// Add (or overwrite) one server-bound session-conf entry. Unknown keys /// are filtered against the kernel allowlist at `open()` time, matching /// the Rust-crate path.