Skip to content

Fix null pointer deref in KVCache::ToPtr() - missing .cache = this#940

Merged
copybara-service[bot] merged 1 commit into
google:devfrom
qiuku30:fix/kvcache-topt-missing-cache-ptr
Jun 23, 2026
Merged

Fix null pointer deref in KVCache::ToPtr() - missing .cache = this#940
copybara-service[bot] merged 1 commit into
google:devfrom
qiuku30:fix/kvcache-topt-missing-cache-ptr

Conversation

@qiuku30

@qiuku30 qiuku30 commented Jun 23, 2026

Copy link
Copy Markdown

Problem

Gemma 2B model crashes with a segfault during the prefill phase on a 6GB VM:

[ Reading prompt ] [ BEGIN PHASE: prefill ]
...............Segmentation fault (core dumped)

Root Cause

KVCache::ToPtr() constructs a KVCachePtr but omits the cache back-reference field, leaving it as the default nullptr. When attention.cc:183 calls cache->KOrVDefaultCols(), it dereferences a null pointer.

The bug was introduced during the tiled KV cache refactoring that added the cache field to KVCachePtr for accessing tiled_seq_len.

Debugging Process

  1. Confirmed the crash is not purely memory-related (reproduced with seq_len=128, num_threads=1)
  2. Confirmed the crash is not SIMD-specific (reproduced on both SSE2 and AVX3_ZEN4 code paths)
  3. Compiled with -fsanitize=address and got the exact crash point:

==206353==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000
#0 gcpp::KVCache::KOrVDefaultCols() const /gemma/kv_cache.h:97:12
#1 gcpp::N_AVX3_ZEN4::ComputeQKV /gemma/attention.cc:183:44

Fix

Added the missing .cache = this field initialization in KVCache::ToPtr():

KVCachePtr ToPtr() {
    return KVCachePtr{
        .kv_cache = kv_cache,
        .k_cache = k_cache,
        .v_cache = v_cache,
        .cache = this,   // ← was missing
    };
}
Verification
The fix was tested on a 6GB VM running Gemma 2B Instruct (SFP-compressed):


./gemma --weights 2.0-2b-it-sfp.sbs --tokenizer tokenizer.spm \
  --prompt "Hello" --max_generated_tokens 30

[ Reading prompt ] .........
Hello! My name is Gemma. How can I help you today?

[ Timing info ] Prefill: 583 ms for 15 prompt tokens
[ Timing info ] Generate: 5653 ms for 19 tokens (3.36 tokens/sec)

@google-cla

google-cla Bot commented Jun 23, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@jan-wassenberg

Copy link
Copy Markdown
Member

Hi, thanks for pointing this out. We're unable to merge to the main branch, please retarget to the dev branch and make sure the PR diff only includes the desired change.

When KVCache::ToPtr() constructs a KVCachePtr, it assigns kv_cache,
k_cache, and v_cache but omits the 'cache' back-reference. This causes
a segfault at attention.cc:183 where cache->KOrVDefaultCols() is
dereferenced through the null pointer.

Root cause identified via AddressSanitizer (SEGV on address 0x0 at
kv_cache.h:97). Fix adds the missing .cache = this field.

Tested: Gemma 2B model runs successfully on 6GB VM after fix.

Co-Authored-By: Claude <noreply@anthropic.com>
@qiuku30 qiuku30 force-pushed the fix/kvcache-topt-missing-cache-ptr branch from ee4124b to 6abb6b1 Compare June 23, 2026 16:11
@qiuku30 qiuku30 changed the base branch from main to dev June 23, 2026 16:24

@jan-wassenberg jan-wassenberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating!

@jan-wassenberg jan-wassenberg added the copybara-import Trigger Copybara for merging pull requests label Jun 23, 2026
@copybara-service copybara-service Bot merged commit 7e93ba5 into google:dev Jun 23, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

copybara-import Trigger Copybara for merging pull requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants