[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensors

オープン 初心者向け
#21,125 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend, compilers

調査の方向性

python/tvm/topi/scan.py の topi.scanop にあるレーンごとの本体から始め、近くにある空テンソルと排他的・包含的動作に関する scan テストを調べます。長さ 0 の scan 軸で再現し、カーネルがバッファに一切アクセスせず、同じ形状の空テンソルを返すことを確認します。

索引モデルが issue の本文から書いたものです。

説明

needs-triage type: bug
Expected behavior

Calling topi.cumsum or topi.cumprod (or relax.op.cumsum / relax.op.cumprod) on a valid 0-element tensor whose scan axis has length 0 (e.g., shape=(0,) or (2, 0, 4)) should perform zero memory accesses and return an empty tensor of the same shape.

Actual behavior

topi.scanop unconditionally writes out_buf[base_idx] (and reads data_buf[base_idx] when exclusive=False) even when cumsum_axis_len == 0, causing an out-of-bounds heap write (and OOB read for exclusive=False) on 0-byte buffers.

In python/tvm/topi/scan.py (lines 120–134 on main 84657c01):

with T.parallel(0, axis_mul_before * axis_mul_after) as fused:
    i = fused // axis_mul_after
    j = fused % axis_mul_after
    base_idx = i * cumsum_axis_len * axis_mul_after + j
    if exclusive:
        T.tensor_store(
            out_buf, T.tensor_indices(out_buf, base_idx), cast(identity_value, dtype)
        )
    else:
        T.tensor_store(
            out_buf,
            T.tensor_indices(out_buf, base_idx),
            maybe_cast(data_buf[T.tensor_indices(data_buf, base_idx)]),
        )
    with T.serial(0, cumsum_axis_len - 1) as _k:
        ...

When N == 0, T.parallel(0, axis_mul_before * axis_mul_after) still executes with base_idx = 0, writing 8 bytes (int64) past the end of the 0-byte out_buf (or axis_mul_after * sizeof(dtype) bytes for multi-dimensional shapes like (2, 0, 4)).

Environment
  • TVM: 0.27.0 (7b5acb32bd271342a24130f4ef439ff6c2e52c6c)
  • Upstream status: verified still unfixed on apache/tvm main (84657c01e5d3672cfd299bd3d5c77bff3888a1a9, 2026-12-02)
  • OS: Linux x86_64
Steps to reproduce
import ctypes
import numpy as np
import tvm
from tvm import te, topi

N = te.var("N", dtype="int64")
A = te.placeholder((N,), name="A", dtype="int64")
B = topi.cumsum(A, axis=0, exclusive=True)
print(tvm.IRModule.from_expr(te.create_prim_func([A, B])).script())
fn = tvm.compile(te.create_prim_func([A, B]), target="llvm")

# Map a PROT_NONE page and pass it as a valid 0-element (0-byte) int64 tensor.
# A sound 0-length kernel performs 0 memory accesses; scanop writes out_buf[0] = 0 -> SIGSEGV.
libc = ctypes.CDLL("libc.so.6")
libc.mmap.restype = ctypes.c_void_p
libc.mmap.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_long]
addr = libc.mmap(None, 4096, 0, 0x22, -1, 0)

class ZeroElemView:
    def __init__(self, ptr):
        self.__array_interface__ = {"shape": (0,), "typestr": "<i8", "data": (ptr, False), "version": 3}

a_np = np.asarray(ZeroElemView(addr))
b_np = np.asarray(ZeroElemView(addr))
fn(tvm.runtime.from_dlpack(a_np), tvm.runtime.from_dlpack(b_np))  # SIGSEGV (-11)

Using topi.nn.relu(A) as a control with the exact same ZeroElemView(addr) succeeds with exit code 0, whereas cumsum(exclusive=True), cumsum(exclusive=False), and cumprod(exclusive=True) all crash with SIGSEGV (-11).

Suggested fix

Guard the per-lane body inside T.parallel in python/tvm/topi/scan.py with with T.If(cumsum_axis_len > 0): with T.Then(): ... (or guard the entire parallel loop with cumsum_axis_len > 0).

主要言語
Python
スター
13.9k
フォーク
4.1k
平均マージ
4日 22時間
マージ済み PR(30日)
22

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/tvm のほかの issue

apache/tvm の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。