Skip to content

Pointer arithmetic bug in first_matrix__bitonic_topk_kernel causes shared memory out-of-bounds write #4181

Description

@hjldegit

🐛 Describe the bug

Bug

There is a pointer arithmetic bug in src/libtorchaudio/cuctc/src/ctc_prefix_decoder_kernel_v2.cu (line 298) that causes out-of-bounds shared memory writes.
The bug is latent on most GPU architectures but produces incorrect results or crashes on NVIDIA H100 due to stricter shared memory bounds checking.

Location

float* block_topk_key =
    reinterpret_cast<float*>(smem_buf_bytes + smem_result_byte_offset);
int* block_topk_value =
    reinterpret_cast<int*>(block_topk_key + sizeof(float) * beam);  // ← BUG

impact

  • A10 / A100: The out-of-bounds write silently lands on physically present but unallocated shared memory regions. Tests pass, but results may be subtly incorrect.
  • H100: Stricter shared memory bounds checking causes data corruption, leading to incorrect CTC decoding results or kernel failures.

Root Cause

block_topk_key is a float*. Pointer arithmetic operates in units of the pointed-to type, not bytes. Adding sizeof(float) * beam to a float* advances by sizeof(float) × sizeof(float) × beam = 16 × beam bytes, instead of the intended sizeof(float) × beam = 4 × beam bytes.

Solution

int* block_topk_value =
    reinterpret_cast<int*>(block_topk_key + beam);

Versions

main

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions