Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- **rustler 0.38 -> 0.39** - Blob values are copied with `OwnedBinary::from_slice`. rustler 0.39 makes binary allocation infallible, so running out of memory while copying a blob now aborts the node, as every other allocation in the NIF already did, rather than returning an error. On Linux, rustler 0.39 links the `enif_*` functions directly, so the Rust test and fuzz binaries define stubs for them; the NIF itself is unchanged.

## [0.10.0] - 2026-12-01

### Added
Expand Down
18 changes: 5 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/ecto_libsql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
libsql = { version = "0.10.0", features = ["encryption", "replication"] }
rustler = { version = "0.38", default-features = false, features = ["derive", "nif_version_2_15"] }
rustler = { version = "0.39", default-features = false, features = ["derive", "nif_version_2_15"] }
tokio = "1.54.1"
uuid = "1.26.1"
bytes = "1.12.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use ecto_libsql::detect_query_type;
use libfuzzer_sys::fuzz_target;

#[cfg(target_os = "linux")]
#[path = "../../src/enif_stubs.rs"]
mod enif_stubs;

fuzz_target!(|data: &[u8]| {
// Convert bytes to string (if valid UTF-8)
if let Ok(sql) = std::str::from_utf8(data) {
Expand Down
4 changes: 4 additions & 0 deletions native/ecto_libsql/fuzz/fuzz_targets/fuzz_should_use_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use ecto_libsql::should_use_query;
use libfuzzer_sys::fuzz_target;

#[cfg(target_os = "linux")]
#[path = "../../src/enif_stubs.rs"]
mod enif_stubs;

fuzz_target!(|data: &[u8]| {
// Convert bytes to string (if valid UTF-8)
if let Ok(sql) = std::str::from_utf8(data) {
Expand Down
4 changes: 4 additions & 0 deletions native/ecto_libsql/fuzz/fuzz_targets/fuzz_sql_structured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use arbitrary::Arbitrary;
use ecto_libsql::{detect_query_type, should_use_query};
use libfuzzer_sys::fuzz_target;

#[cfg(target_os = "linux")]
#[path = "../../src/enif_stubs.rs"]
mod enif_stubs;

/// SQL-like input for fuzzing
#[derive(Debug, Arbitrary)]
struct SqlInput<'a> {
Expand Down
14 changes: 4 additions & 10 deletions native/ecto_libsql/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,10 @@ pub fn fetch_cursor<'a>(
Value::Text(s) => Ok(s.encode(env)),
Value::Integer(i) => Ok(i.encode(env)),
Value::Real(f) => Ok(f.encode(env)),
Value::Blob(b) => OwnedBinary::new(b.len())
.ok_or_else(|| {
rustler::Error::Term(Box::new(
"Failed to allocate binary for blob data",
))
})
.map(|mut owned| {
owned.as_mut_slice().copy_from_slice(b);
Binary::from_owned(owned, env).encode(env)
}),
Value::Blob(b) => {
// Infallible since rustler 0.39: out of memory aborts, as every other allocation here does.
Ok(Binary::from_owned(OwnedBinary::from_slice(b), env).encode(env))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
Value::Null => Ok(rustler::types::atom::nil().encode(env)),
})
.collect();
Expand Down
50 changes: 50 additions & 0 deletions native/ecto_libsql/src/enif_stubs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! Stand-ins for the BEAM's `enif_*` functions in Linux test and fuzz binaries.
//!
//! rustler 0.39 references these as plain extern symbols on Linux and leaves the
//! BEAM to supply them when it loads the NIF (rusterlium/rustler#764). A test or
//! fuzz binary has no BEAM, and x86_64 binds every symbol at start-up, so each one
//! the binary references must exist even though the Rust tests never call them.
//! Each stub aborts if it is ever called. The NIF itself never includes this file.
//!
//! The list is every `enif_*` symbol the test and fuzz binaries reference, from
//! `nm -u`. If a rustler upgrade references another, linking fails and names it.

// `no_mangle` counts as unsafe because an exported name can collide with another
// symbol. Supplying these names is the whole point, and only in binaries with no BEAM.
#![allow(unsafe_code)]

fn called_outside_beam(name: &str) -> ! {
eprintln!("{name} was called, but only the BEAM can provide it");
std::process::abort()
}

macro_rules! enif_stubs {
($($name:ident)*) => {
$(
#[no_mangle]
pub extern "C" fn $name() {
called_outside_beam(stringify!($name))
}
)*
};
}

enif_stubs! {
enif_alloc enif_alloc_binary enif_alloc_env enif_binary_to_term enif_clear_env
enif_compare enif_compare_monitors enif_consume_timeslice enif_free enif_free_env
enif_get_atom enif_get_atom_length enif_get_double enif_get_int enif_get_list_cell
enif_get_local_pid enif_get_long enif_get_map_size enif_get_map_value enif_get_tuple
enif_get_uint enif_get_ulong enif_hash enif_inspect_binary
enif_inspect_iolist_as_binary enif_is_atom enif_is_binary enif_is_empty_list
enif_is_exception enif_is_fun enif_is_identical enif_is_list enif_is_map
enif_is_number enif_is_pid enif_is_port enif_is_process_alive enif_is_ref
enif_is_tuple enif_make_atom_len enif_make_badarg enif_make_binary enif_make_copy
enif_make_double enif_make_existing_atom_len enif_make_list_from_array
enif_make_long enif_make_map_from_arrays enif_make_map_put enif_make_map_remove
enif_make_map_update enif_make_new_binary enif_make_new_map enif_make_ref
enif_make_tuple_from_array enif_make_ulong enif_map_iterator_create
enif_map_iterator_destroy enif_map_iterator_get_pair enif_map_iterator_next
enif_map_iterator_prev enif_open_resource_type_x enif_raise_exception
enif_realloc_binary enif_release_binary enif_schedule_nif enif_self enif_snprintf
enif_term_to_binary enif_term_type enif_thread_type
}
3 changes: 3 additions & 0 deletions native/ecto_libsql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ rustler::init!("Elixir.EctoLibSql.Native");

#[cfg(test)]
mod tests;

#[cfg(all(test, target_os = "linux"))]
mod enif_stubs;
18 changes: 4 additions & 14 deletions native/ecto_libsql/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,10 @@ pub async fn collect_rows<'a>(env: Env<'a>, mut rows: Rows) -> Result<Term<'a>,
Ok(Value::Text(val)) => val.encode(env),
Ok(Value::Integer(val)) => val.encode(env),
Ok(Value::Real(val)) => val.encode(env),
Ok(Value::Blob(val)) => OwnedBinary::new(val.len())
.ok_or_else(|| {
let col_name = column_names
.get(i)
.unwrap_or(&"unknown".to_string())
.clone();
rustler::Error::Term(Box::new(format!(
"Failed to allocate binary for column '{col_name}' (index {i})"
)))
})
.map(|mut owned| {
owned.as_mut_slice().copy_from_slice(&val);
Binary::from_owned(owned, env).encode(env)
})?,
Ok(Value::Blob(val)) => {
// Infallible since rustler 0.39: out of memory aborts, as every other allocation here does.
Binary::from_owned(OwnedBinary::from_slice(&val), env).encode(env)
}
Ok(Value::Null) => nil().encode(env),
Err(err) => {
let col_name = column_names
Expand Down
Loading