`sha256()` returns a wrong hash when libsql-ffi is built with gcc 11 or later
维护者通常 11 天内回复
评估
调研方向
从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 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tursodatabase/libsql 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
tursodatabase/libsql#2320 ·
维护者通常 11 天内回复
-
`cloudflare` feature: hrana streams are never closed可能已有人在做 @pcbeingused333 于 7 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
tursodatabase/libsql#2312 ·
维护者通常 11 天内回复
-
bottomless: finished S3 upload tasks are never reaped, so sqld memory grows by ~10 KB per WAL batch可能已有人在做 @pavanmanishd 于 28 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 83/100
tursodatabase/libsql#2298 · 1 条评论 ·
维护者通常 11 天内回复
-
Rust local Row::get_value truncates embedded NUL TEXT and panics on malformed UTF-8 (0.9.30)可能已有人在做 @Sanjays2402 于 38 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
tursodatabase/libsql#2285 ·
维护者通常 11 天内回复
-
libsql-ffi: broken builds under Nix on macOS可能已有人在做 @frectonz 于 88 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
tursodatabase/libsql#2275 ·
维护者通常 11 天内回复
查看 tursodatabase/libsql 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
-
难度 2/5 1-3 小时 新手友好度 72/100
OpenPrinting/cups#1826 ·
维护者通常 1 天内回复
-
Backport to 3.0 advertises a suffix that does not exist可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
389ds/389-ds-base#8152 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
RetroAchievements/rcheevos#580 · 1 条评论 ·
维护者通常 5 天内回复
-
meta:feature-request
难度 2/5 1-3 小时 新手友好度 66/100
mpv-player/mpv#18855 · 2 条评论 ·
维护者通常 1 天内回复