From 15fdde425b27925ced61f883a9d876d372f27a14 Mon Sep 17 00:00:00 2001 From: Colin McDonnell <3084745+colinhacks@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:51:56 -0800 Subject: [PATCH 1/2] libsql-ffi: Fix sqlean sha256() under gcc 11 and later sha256_final stored the message length through a sha2_word64 pointer into the byte buffer, a strict-aliasing violation that gcc 11 and later reorder at -O2 and -O3, so sha256() returned the wrong hash. Store it with memcpy, and do the same for the two length writes in sha512's last step. --- libsql-ffi/bundled/sqlean/crypto/sha2.c | 6 ++--- libsql-server/tests/standalone/mod.rs | 32 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/libsql-ffi/bundled/sqlean/crypto/sha2.c b/libsql-ffi/bundled/sqlean/crypto/sha2.c index 0f4dc0ad06..cefb981e2c 100644 --- a/libsql-ffi/bundled/sqlean/crypto/sha2.c +++ b/libsql-ffi/bundled/sqlean/crypto/sha2.c @@ -571,7 +571,7 @@ int sha256_final(SHA256_CTX* context, sha2_byte digest[SHA256_DIGEST_LENGTH]) { *context->buffer = 0x80; } /* Set the bit count: */ - *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount; + MEMCPY_BCOPY(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], &context->bitcount, sizeof(context->bitcount)); /* Final transform: */ SHA256_Transform(context, (sha2_word32*)context->buffer); @@ -854,8 +854,8 @@ static void SHA512_Last(SHA512_CTX* context) { *context->buffer = 0x80; } /* Store the length of input data (in bits): */ - *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1]; - *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8] = context->bitcount[0]; + MEMCPY_BCOPY(&context->buffer[SHA512_SHORT_BLOCK_LENGTH], &context->bitcount[1], sizeof(context->bitcount[1])); + MEMCPY_BCOPY(&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8], &context->bitcount[0], sizeof(context->bitcount[0])); /* Final transform: */ SHA512_Transform(context, (sha2_word64*)context->buffer); diff --git a/libsql-server/tests/standalone/mod.rs b/libsql-server/tests/standalone/mod.rs index ad3fae958b..90bf0466b7 100644 --- a/libsql-server/tests/standalone/mod.rs +++ b/libsql-server/tests/standalone/mod.rs @@ -73,6 +73,38 @@ fn basic_query() { sim.run().unwrap(); } +#[test] +fn sqlean_crypto_hashes() { + let mut sim = turmoil::Builder::new() + .simulation_duration(Duration::from_secs(1000)) + .build(); + + sim.host("primary", make_standalone_server); + + sim.client("test", async { + let db = Database::open_remote_with_connector("http://primary:8080", "", TurmoilConnector)?; + let conn = db.connect()?; + + let mut rows = conn + .query("select hex(sha256('a')), hex(sha512('a'))", ()) + .await?; + let row = rows.next().await?.unwrap(); + + assert_eq!( + row.get::(0)?, + "CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB" + ); + assert_eq!( + row.get::(1)?, + "1F40FC92DA241694750979EE6CF582F2D5D7D28E18335DE05ABC54D0560E0F5302860C652BF08D560252AA5E74210546F369FBBBCE8C12CFC7957B2652FE9A75" + ); + + Ok(()) + }); + + sim.run().unwrap(); +} + #[test] fn basic_metrics() { let mut sim = turmoil::Builder::new() From e2fb608fc416a0dc1f8f3e010cb0b1fbc733f111 Mon Sep 17 00:00:00 2001 From: Colin McDonnell <3084745+colinhacks@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:39:03 -0800 Subject: [PATCH 2/2] libsql-server: Drop sqlean_crypto_hashes test The workspace test run builds libsql-ffi with multiple-ciphers, which doesn't compile sqlean, so the test can't find sha256(). --- libsql-server/tests/standalone/mod.rs | 32 --------------------------- 1 file changed, 32 deletions(-) diff --git a/libsql-server/tests/standalone/mod.rs b/libsql-server/tests/standalone/mod.rs index 90bf0466b7..ad3fae958b 100644 --- a/libsql-server/tests/standalone/mod.rs +++ b/libsql-server/tests/standalone/mod.rs @@ -73,38 +73,6 @@ fn basic_query() { sim.run().unwrap(); } -#[test] -fn sqlean_crypto_hashes() { - let mut sim = turmoil::Builder::new() - .simulation_duration(Duration::from_secs(1000)) - .build(); - - sim.host("primary", make_standalone_server); - - sim.client("test", async { - let db = Database::open_remote_with_connector("http://primary:8080", "", TurmoilConnector)?; - let conn = db.connect()?; - - let mut rows = conn - .query("select hex(sha256('a')), hex(sha512('a'))", ()) - .await?; - let row = rows.next().await?.unwrap(); - - assert_eq!( - row.get::(0)?, - "CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB" - ); - assert_eq!( - row.get::(1)?, - "1F40FC92DA241694750979EE6CF582F2D5D7D28E18335DE05ABC54D0560E0F5302860C652BF08D560252AA5E74210546F369FBBBCE8C12CFC7957B2652FE9A75" - ); - - Ok(()) - }); - - sim.run().unwrap(); -} - #[test] fn basic_metrics() { let mut sim = turmoil::Builder::new()