diff --git a/CHANGELOG.md b/CHANGELOG.md index 3926a334..03487382 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Cargo.lock b/Cargo.lock index d3ec2eb2..cc06db30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1375,12 +1375,6 @@ dependencies = [ "regex-syntax", ] -[[package]] -name = "regex-lite" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cab834c73d247e67f4fae452806d17d3c7501756d98c8808d7c9c7aa7d18f973" - [[package]] name = "regex-syntax" version = "0.8.11" @@ -1435,28 +1429,26 @@ dependencies = [ [[package]] name = "rustler" -version = "0.38.0" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60ac8495cb6091c1f8bd0c3bb816cd4e6f0a073f396d77eb602cb7f0615212de" +checksum = "5256cd1465ad8c5450ee3392263375108cb70382be09dac76ae7da521446bdde" dependencies = [ "inventory", "libc", "libloading 0.9.0", - "regex-lite", "rustler_codegen", ] [[package]] name = "rustler_codegen" -version = "0.38.0" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "780a2a6b7e3cfac35820473f85cec1cd84d1bdd37ebcb4e24fb126ef0f074838" +checksum = "976aaa45a28908eb2433b1afc70bf8c39eabd0ee45e8de24b2ae53dfb8876b84" dependencies = [ "heck", - "inventory", "proc-macro2", "quote", - "syn 2.0.119", + "syn 3.0.5", ] [[package]] diff --git a/native/ecto_libsql/Cargo.toml b/native/ecto_libsql/Cargo.toml index 0ef93c02..665b3166 100644 --- a/native/ecto_libsql/Cargo.toml +++ b/native/ecto_libsql/Cargo.toml @@ -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" diff --git a/native/ecto_libsql/fuzz/fuzz_targets/fuzz_detect_query_type.rs b/native/ecto_libsql/fuzz/fuzz_targets/fuzz_detect_query_type.rs index e4b73873..d22e7186 100644 --- a/native/ecto_libsql/fuzz/fuzz_targets/fuzz_detect_query_type.rs +++ b/native/ecto_libsql/fuzz/fuzz_targets/fuzz_detect_query_type.rs @@ -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) { diff --git a/native/ecto_libsql/fuzz/fuzz_targets/fuzz_should_use_query.rs b/native/ecto_libsql/fuzz/fuzz_targets/fuzz_should_use_query.rs index ffbc184e..bb59cfb5 100644 --- a/native/ecto_libsql/fuzz/fuzz_targets/fuzz_should_use_query.rs +++ b/native/ecto_libsql/fuzz/fuzz_targets/fuzz_should_use_query.rs @@ -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) { diff --git a/native/ecto_libsql/fuzz/fuzz_targets/fuzz_sql_structured.rs b/native/ecto_libsql/fuzz/fuzz_targets/fuzz_sql_structured.rs index f120941c..ba5fdba8 100644 --- a/native/ecto_libsql/fuzz/fuzz_targets/fuzz_sql_structured.rs +++ b/native/ecto_libsql/fuzz/fuzz_targets/fuzz_sql_structured.rs @@ -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> { diff --git a/native/ecto_libsql/src/cursor.rs b/native/ecto_libsql/src/cursor.rs index 798d06a3..bdab3f8a 100644 --- a/native/ecto_libsql/src/cursor.rs +++ b/native/ecto_libsql/src/cursor.rs @@ -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)) + } Value::Null => Ok(rustler::types::atom::nil().encode(env)), }) .collect(); diff --git a/native/ecto_libsql/src/enif_stubs.rs b/native/ecto_libsql/src/enif_stubs.rs new file mode 100644 index 00000000..cbf0b57c --- /dev/null +++ b/native/ecto_libsql/src/enif_stubs.rs @@ -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 +} diff --git a/native/ecto_libsql/src/lib.rs b/native/ecto_libsql/src/lib.rs index ffa35d2e..c92cba9a 100644 --- a/native/ecto_libsql/src/lib.rs +++ b/native/ecto_libsql/src/lib.rs @@ -28,3 +28,6 @@ rustler::init!("Elixir.EctoLibSql.Native"); #[cfg(test)] mod tests; + +#[cfg(all(test, target_os = "linux"))] +mod enif_stubs; diff --git a/native/ecto_libsql/src/utils.rs b/native/ecto_libsql/src/utils.rs index 85ff409d..b8b873f0 100644 --- a/native/ecto_libsql/src/utils.rs +++ b/native/ecto_libsql/src/utils.rs @@ -218,20 +218,10 @@ pub async fn collect_rows<'a>(env: Env<'a>, mut rows: Rows) -> Result, 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