Repository navigation
Description
Summary
Passing a JavaScript number (returned from a previous FFI call) as a 'ptr' argument to another FFI function causes a segfault on Linux x64. The crash address is 0xFFFFFFFFFFFFFFFF, suggesting the number is being corrupted during marshaling.
Declaring the same argument as 'u64' and passing a BigInt works correctly. This is not a DuckDB bug — it reproduces with any library that returns an opaque handle and expects it back.
This likely explains many of the crashes in #13910 (DuckDB compatibility).
Versions tested
- Bun v1.2.15, v1.3.12, v1.3.12-canary — all crash
- Linux x64, kernel 6.17.0, glibc 2.39
- macOS ARM64 works fine with the same code
Minimal reproduction
Step 1: Verify FFI works (libc)
// test-libc.mjs — PASSES
import { dlopen } from 'bun:ffi';
const { symbols: libc } = dlopen("libc.so.6", {
getpid: { args: [], returns: 'i32' },
time: { args: ['ptr'], returns: 'i64' },
});
console.log(`PID: ${libc.getpid()}`);
console.log(`Time: ${libc.time(null)}`);
console.log('OK');Step 2: Number as 'ptr' — CRASHES
// test-crash.mjs — dlopen + open works, connect CRASHES
import { dlopen, ptr, read as ffiRead } from 'bun:ffi';
const { symbols: lib } = dlopen("/usr/local/lib/libduckdb.so", {
duckdb_open: { args: ['ptr', 'ptr'], returns: 'i32' },
duckdb_close: { args: ['ptr'], returns: 'void' },
duckdb_connect: { args: ['ptr', 'ptr'], returns: 'i32' },
duckdb_disconnect: { args: ['ptr'], returns: 'void' },
});
const dbBuf = new Uint8Array(8);
lib.duckdb_open(null, ptr(dbBuf));
const dbHandle = ffiRead.ptr(ptr(dbBuf), 0);
console.log(`Open: OK (${dbHandle})`);
// This passes dbHandle (a JS number) as a 'ptr' argument — CRASHES
const connBuf = new Uint8Array(8);
lib.duckdb_connect(dbHandle, ptr(connBuf));
console.log('Connect: OK');
lib.duckdb_disconnect(ptr(connBuf));
lib.duckdb_close(ptr(dbBuf));Output:
Open: OK (548444784)
============================================================
Bun v1.2.15 (df017990) Linux x64
panic(main thread): Segmentation fault at address 0xFFFFFFFFFFFFFFFF
Step 3: BigInt as 'u64' — WORKS
// test-fix.mjs — same code, but 'u64' + BigInt for the handle
import { dlopen, ptr } from 'bun:ffi';
const { symbols: lib } = dlopen("/usr/local/lib/libduckdb.so", {
duckdb_open: { args: ['ptr', 'ptr'], returns: 'i32' },
duckdb_close: { args: ['ptr'], returns: 'void' },
duckdb_connect: { args: ['u64', 'ptr'], returns: 'i32' }, // <-- u64 instead of ptr
duckdb_disconnect: { args: ['ptr'], returns: 'void' },
});
const dbBuf = new Uint8Array(8);
lib.duckdb_open(null, ptr(dbBuf));
const dbHandle = new DataView(dbBuf.buffer).getBigUint64(0, true);
console.log(`Open: OK (0x${dbHandle.toString(16)})`);
const connBuf = new Uint8Array(8);
lib.duckdb_connect(dbHandle, ptr(connBuf)); // BigInt — works!
console.log('Connect: OK');
lib.duckdb_disconnect(ptr(connBuf));
lib.duckdb_close(ptr(dbBuf));Output:
Open: OK (0x421bda90)
Connect: OK
Disconnect: OK
Close: OK
Key observations
| What we pass | Declared as | Result |
|---|---|---|
ptr(typedArray) |
'ptr' |
Works (Bun extracts pointer from buffer) |
null |
'ptr' |
Works (special-cased) |
548444784 (JS number from ffiRead.ptr) |
'ptr' |
Crashes at 0xFFFFFFFFFFFFFFFF |
548444784n (same value as BigInt) |
'u64' |
Works |
The bug is specifically in marshaling a plain JS number into a C pointer argument. ptr(buf) and null work; a number does not. Since void* and uint64_t are the same ABI on x86_64, 'u64' + BigInt is a safe workaround.
Impact
This likely explains the long-standing DuckDB FFI crashes on Linux (#13910, open since Sep 2024). Any FFI usage that reads a handle via ffiRead.ptr() or from a 'ptr' return and passes it to another function as 'ptr' will crash on Linux x64.
Environment
Bun v1.3.12 (700fc117) Linux x64
Linux Kernel v6.17.0 | glibc v2.39
CPU: sse42 popcnt avx avx2 avx512
Activity
github-actions commentedon Apr 15, 2026
Found 1 possible duplicate issue:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
shreeve commentedon Apr 15, 2026
Our fix (working in production)
We hit this while building rip-db, a DuckDB HTTP server using pure Bun FFI. Here's what we did to get it working on Linux x64.
Fix 1:
u64+ BigInt for all opaque handlesEvery DuckDB C API function that takes an opaque handle by value (
duckdb_database,duckdb_connection,duckdb_prepared_statement,duckdb_data_chunk,duckdb_vector,duckdb_logical_type,duckdb_appender) was changed from'ptr'to'u64', and all handles are stored as BigInt.Arguments that receive
ptr(typedArray)(buffer pointers for output params) stay as'ptr'— those work fine.Fix 2: C shim for struct-by-value functions
A second, independent issue:
duckdb_fetch_chunk(duckdb_result result)takes a 48-byte struct by value. On Linux x64 SysV ABI, this is MEMORY class (passed on the stack). Bun FFI has no mechanism to pass structs by value, so this crashes regardless of theptr/u64fix.On macOS ARM64, large structs are passed by hidden pointer, so
ptr(buf)happens to work. On Linux x64, it segfaults.We wrote a 30-line C shim that accepts
duckdb_result*instead:Build:
gcc -shared -fPIC -o libduckdb-shim.so duckdb-shim.c -lduckdbThen in JS:
Full working test (Linux x64, SELECT 42 end-to-end)
Output on Linux x64:
Commit
The full fix (FFI declarations, BigInt handles, shim loader, fallback for macOS): shreeve/rip-lang@5f29011
All 62 DuckDB FFI tests pass on macOS, and the server runs successfully on Linux x64 with DuckDB 1.5.2 + Bun 1.3.12.
ffi: regression test for JS number passed as 'ptr' argument
robobun commentedon Apr 15, 2026
Fixed in v1.3.3 by #25045 — the Int32-encoded JSValue branch was missing from
JSVALUE_TO_PTRinFFI.h, so handles in the low 2 GiB got sign-extended to0xFFFFFFFFxxxxxxxx.Your reproduction runs clean on a fresh v1.3.12 install here (same
700fc117revision you reported) — could you trycurl -fsSL https://bun.sh/install | bash -s "bun-v1.3.12"or upgrading to canary? If it still crashes, please drop a freshbun --revisionand the exact output here.Regression test in #29350. Just rebased onto current main (8 new commits) to run CI again past the earlier snapshot/segfault flakes.
test: move to test/regression/issue/29346.test.ts, self-contained
ffi: regression test for JS number passed as 'ptr' argument
test: move to test/regression/issue/29346.test.ts, self-contained
shreeve commentedon Apr 17, 2026
How can we get this retested with the CI/CD?
shreeve commentedon Apr 18, 2026
See #29464