Skip to content

Commit 9a460b3

Browse files
committed
fix(sha256): keep the message schedule out of the transform's frame
sha256_transform held its 64-word message schedule as a 256-byte local. That is large enough for ASan's use-after-return fake stack, so an instrumented build heap-allocated it through __asan_stack_malloc on every 64-byte block -- millions of allocations to hash one large file. Under the sanitized CI settings (detect_stack_use_after_return=1) fingerprinting a freshly installed binary therefore took minutes, and the cli suite blew its 900s wall-clock budget and was killed as hung on the Linux arm64 leg. The suite passes on macOS, where the fake stack is not engaged the same way, which is why this only ever surfaced on one leg. Move the schedule into cbm_sha256_ctx so it is allocated once per hash instead of once per block. The computed values and the resulting digest are identical; no sanitizer option is relaxed and use-after-return detection stays enabled everywhere. Verified in the Linux arm64 sanitized container under the exact CI ASAN_OPTIONS (detect_stack_use_after_return=1:strict_string_checks=1: detect_stack_use_after_scope=1): cli 316 passed in 356s, previously killed at the 900s budget; daemon_version 10 passed, covering the build-fingerprint digests that would break first on a wrong hash. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 78a6d19 commit 9a460b3

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎src/foundation/sha256.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ static const uint32_t K[64] = {
2525
#define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10))
2626

2727
static void sha256_transform(cbm_sha256_ctx *c, const uint8_t *data) {
28-
uint32_t m[64];
28+
/* Scratch comes from the context, not this frame: a 256-byte local here
29+
* is fake-stacked by ASan's use-after-return mode on every 64-byte block
30+
* (see the note on cbm_sha256_ctx::sched). Identical values, allocated
31+
* once per hash instead of once per block. */
32+
uint32_t *m = c->sched;
2933
for (int i = 0, j = 0; i < 16; i++, j += 4) {
3034
m[i] = ((uint32_t)data[j] << 24) | ((uint32_t)data[j + 1] << 16) |
3135
((uint32_t)data[j + 2] << 8) | (uint32_t)data[j + 3];

‎src/foundation/sha256.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ typedef struct {
1717
uint64_t bitlen;
1818
uint8_t buf[64];
1919
size_t buflen;
20+
/* Message-schedule scratch for one compression round. It lives in the
21+
* context rather than in sha256_transform's own frame because a 256-byte
22+
* local there is large enough for ASan's use-after-return fake stack,
23+
* which then heap-allocates it on EVERY 64-byte block — millions of
24+
* __asan_stack_malloc calls to fingerprint one large file, turning
25+
* sanitized binary fingerprinting into minutes. Here it is allocated once
26+
* per hash instead of once per block. */
27+
uint32_t sched[64];
2028
} cbm_sha256_ctx;
2129

2230
void cbm_sha256_init(cbm_sha256_ctx *c);

0 commit comments

Comments
 (0)