[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensors
メンテナーはふだん 3 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 82/100
調査の方向性
python/tvm/topi/scan.py の topi.scanop にあるレーンごとの本体から始め、近くにある空テンソルと排他的・包含的動作に関する scan テストを調べます。長さ 0 の scan 軸で再現し、カーネルがバッファに一切アクセスせず、同じ形状の空テンソルを返すことを確認します。
索引モデルが issue の本文から書いたものです。
説明
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/tvmmain(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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/tvm のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 3 日以内に返信
-
[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradients対応中かも @pierceluu10 が 4 日前に担当しました。 オープンneeds-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 3 日以内に返信
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputs対応中かも @tandede が 8 日前に担当しました。 オープンneeds-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 3 日以内に返信
-
needs-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 3 日以内に返信
-
[Bug][Relax][TOPI] RoiAlign mode=max returns the average of the sample points対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンneeds-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 3 日以内に返信
似ている issue
-
area:print-dispatch
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
area/geometry good first issue help wanted kind/doc-mismatch severity/4-minor
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
Apich-Organization/rssn#295 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
chore claimed
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
schuyler/corvidae#1424 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信