`sha256()` returns a wrong hash when libsql-ffi is built with gcc 11 or later

未关闭 适合新手
#2,333 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 11 天内回复

@colinhacks 已经在做这个了。

开始于 2026年11月19日。

  • #2334 来自 @colinhacks —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c, sqlite
领域
databases

调研方向

从libsql-ffi/bundled/sqlean/crypto/sha2.c开始,检查sha256_final;issue指出其中存在对消息长度的 strict aliasing 违规写入。使用 issue 中描述的标志,通过 gcc 11 或更高版本构建打包的代码,然后运行select hex(sha256('a'));确认输出。在这些编译器设置下,查询返回 issue 中显示的标准 SHA-256 哈希即表示完成。

由索引模型根据 Issue 内容生成。

描述

With the sqlean crypto extension (sqlean-extension-crypto, which libsql-server turns on through sqlean-extensions), sha256() returns the wrong hash when the C code is built with gcc 11 or later:

select hex(sha256('a'));
-- 767DD50DA1B5B88651E238AA239F5FE9F934BEA61F909E70785EED84FD2331FC   gcc 11 to 16, -O2 or -O3
-- CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB   gcc 10, clang, or gcc with -fno-strict-aliasing

That's from libsql-ffi/bundled/src/sqlite3.c and libsql-ffi/bundled/sqlean/crypto/*.c on main, compiled with -O3 -DSQLITE_CORE as build.rs does, calling crypto_init and running the query. The bundled sha2.c alone shows the same thing with gcc 11, 12, 13, 14, 15 and 16. sha512() was right with gcc 16 and clang.

The cause is the write of the message length in sha256_final, a strict-aliasing violation that gcc 11 and later reorder:

*(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;

A memcpy there gives the right hash with gcc 16 at -O2 and -O3:

memcpy(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], &context->bitcount, sizeof(context->bitcount));

The sqld image on ghcr.io (0.24.33) returns the right hash today because it builds on rust:slim-bullseye, whose gcc is 10. A release build of sqld from source on a current distro, or moving the image to bookworm (gcc 12), gives the wrong one. Upstream sqlean has since replaced this sha2.c (nalgeon/sqlean@db85bbb, and again in October 2026), so updating the bundled crypto would also fix it.

主要语言
C
星标
17.3k
派生
562
平均合并
10 天 15 小时
30 天内合并 PR
1

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tursodatabase/libsql 的其他 Issue

查看 tursodatabase/libsql 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。