Commit 9f255e3b for whisper.cpp
commit 9f255e3bcee6300e98c70815cb2b6ff95b30135a
Author: compiler-segfault <2671715549@qq.com>
Date: Mon Nov 30 16:13:23 2026 +0800
Fix the use-after-free when the KV cache re-allocation fails (#4218)
* Keep the caller's whisper_state when the KV cache re-allocation fails
`whisper_full_with_state` recreates the KV cache when the decoder count grows and
freed the state on failure. That state belongs to the caller (`ctx->state` for
`whisper_full`), so after one failed request every later call read freed memory:
AddressSanitizer reported a heap-use-after-free in `whisper_state_clear_vad` on the
next `whisper_full`, and a caller that passes its own state would double free it.
The failure path now leaves the state alone, puts the cache back at its default
size so the context stays usable (a hard failure there still fails the call
cleanly), and reports -7 for the request. `whisper_kv_cache_init` also frees its
ggml context when the buffer allocation fails instead of leaking it, and
`whisper_kv_cache_free` clears the buffer pointer it just freed, so a later free of
the same cache cannot double free.
Reproduced on master with an AddressSanitizer build by forcing the re-allocation to
fail, as in the report: the first `whisper_full` with `beam_size = 8` returns -7 and
the second one aborts under ASan. With the change both requests fail cleanly and
`whisper_free` releases the context; without the forced failure, two normal
beam-search requests still return 0.
Assisted-by: DeepSeek
Signed-off-by: compiler-segfault <238626475+compiler-segfault@users.noreply.github.com>
* Shorten the comment about restoring the default KV cache
---------
Signed-off-by: compiler-segfault <238626475+compiler-segfault@users.noreply.github.com>
Co-authored-by: compiler-segfault <238626475+compiler-segfault@users.noreply.github.com>
diff --git a/src/whisper.cpp b/src/whisper.cpp
index c07f4853..1ad9e262 100644
--- a/src/whisper.cpp
+++ b/src/whisper.cpp
@@ -990,6 +990,7 @@ static bool whisper_kv_cache_init(
cache.buffer = ggml_backend_alloc_ctx_tensors(ctx, backend);
if (!cache.buffer) {
WHISPER_LOG_ERROR("%s: failed to allocate memory for the kv cache\n", __func__);
+ ggml_free(ctx);
return false;
}
@@ -1002,6 +1003,7 @@ static bool whisper_kv_cache_init(
static void whisper_kv_cache_free(struct whisper_kv_cache & cache) {
ggml_backend_buffer_free(cache.buffer);
+ cache.buffer = nullptr;
}
static bool whisper_kv_cache_find_slot(
@@ -7319,13 +7321,20 @@ int whisper_full_with_state(
// overallocate to workaround KV cache fragmentation issues
const int factor = n_decoders_cur > 1 ? n_decoders_cur + 2 : 1;
+ const int n_ctx = GGML_PAD(ctx->model.hparams.n_text_ctx, 256);
if (!whisper_kv_cache_init(state->kv_self, state->backends[0], ctx->itype,
ctx->model.hparams.n_text_state,
ctx->model.hparams.n_text_layer,
- GGML_PAD(ctx->model.hparams.n_text_ctx, 256)*factor)) {
+ n_ctx*factor)) {
WHISPER_LOG_ERROR("%s: whisper_kv_cache_init() failed for self-attention cache\n", __func__);
- whisper_free_state(state);
+ // Re-init the kv cache to its initial state as we freed it above.
+ if (!whisper_kv_cache_init(state->kv_self, state->backends[0], ctx->itype,
+ ctx->model.hparams.n_text_state,
+ ctx->model.hparams.n_text_layer,
+ n_ctx)) {
+ WHISPER_LOG_ERROR("%s: failed to restore the default KV cache\n", __func__);
+ }
return -7;
}