Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(sha256): keep the message schedule out of the transform's frame

Martin Vogel9a460b389ce3
diff --git a/src/foundation/sha256.c b/src/foundation/sha256.c
index 937e363e6..84d37453e 100644
--- a/src/foundation/sha256.c
+++ b/src/foundation/sha256.c
@@ -25,7 +25,11 @@ static const uint32_t K[64] = {
#define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10))
static void sha256_transform(cbm_sha256_ctx *c, const uint8_t *data) {
- uint32_t m[64];
+ /* Scratch comes from the context, not this frame: a 256-byte local here
+ * is fake-stacked by ASan's use-after-return mode on every 64-byte block
+ * (see the note on cbm_sha256_ctx::sched). Identical values, allocated
+ * once per hash instead of once per block. */
+ uint32_t *m = c->sched;
for (int i = 0, j = 0; i < 16; i++, j += 4) {
m[i] = ((uint32_t)data[j] << 24) | ((uint32_t)data[j + 1] << 16) |
((uint32_t)data[j + 2] << 8) | (uint32_t)data[j + 3];
diff --git a/src/foundation/sha256.h b/src/foundation/sha256.h
index a34a741d3..f3c7f9c55 100644
--- a/src/foundation/sha256.h
+++ b/src/foundation/sha256.h
@@ -17,6 +17,14 @@ typedef struct {
uint64_t bitlen;
uint8_t buf[64];
size_t buflen;
+ /* Message-schedule scratch for one compression round. It lives in the
+ * context rather than in sha256_transform's own frame because a 256-byte
+ * local there is large enough for ASan's use-after-return fake stack,
+ * which then heap-allocates it on EVERY 64-byte block — millions of
+ * __asan_stack_malloc calls to fingerprint one large file, turning
+ * sanitized binary fingerprinting into minutes. Here it is allocated once
+ * per hash instead of once per block. */
+ uint32_t sched[64];
} cbm_sha256_ctx;
void cbm_sha256_init(cbm_sha256_ctx *c);