[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensors
I maintainer di solito rispondono entro 3 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 82/100
Direzione di ricerca
Inizia in python/tvm/topi/scan.py, nel body per lane di topi.scanop, poi esamina i test di scan vicini per i tensori vuoti e il comportamento esclusivo rispetto a quello inclusivo. Riproduci il caso con un asse di scan di lunghezza zero e verifica che il kernel non acceda ad alcun buffer e restituisca un tensore vuoto della stessa forma.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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).
- Lingua principale
- Python
- Stelle
- 13.9k
- Fork
- 4.1k
- Merge medio
- 4g 22h
- PR unite (30g)
- 22
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di apache/tvm
-
[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is NoneForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 3 giorni
-
[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradientsForse già presa @pierceluu10 l’ha presa 4 giorni fa. Apertaneeds-triage type: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 3 giorni
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputsForse già presa @tandede l’ha presa 9 giorni fa. Apertaneeds-triage type: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 3 giorni
-
needs-triage type: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
apache/tvm#21043 · 1 commento ·
I maintainer di solito rispondono entro 3 giorni
-
[Bug][Relax][TOPI] RoiAlign mode=max returns the average of the sample pointsForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertaneeds-triage type: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 3 giorni
Issue simili
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 78/100
web-push-libs/pywebpush#216 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
AgriciDaniel/claude-seo#572 ·
I maintainer di solito rispondono entro 5 giorni
-
area:print-dispatch
Difficoltà 2/5 1-3 ore Idoneità per principianti 92/100
I maintainer di solito rispondono entro 1 giorno
-
area: docs domain: technical-writing good first issue type: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
Deepak3699/Ai_Mentor#919 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno