[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensors
Los mantenedores suelen responder en 3 días
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 82/100
Línea de trabajo
Empieza en python/tvm/topi/scan.py, en el cuerpo por lane de topi.scanop, y después revisa las pruebas de scan cercanas para tensores vacíos y el comportamiento exclusivo frente al inclusivo. Reproduce el caso con un eje de scan de longitud cero y verifica que el kernel no acceda a ningún búfer y devuelva un tensor vacío con la misma forma.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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).
- Lenguaje dominante
- Python
- Estrellas
- 13.9k
- Forks
- 4.1k
- Merge medio
- 4 d 22 h
- PR fusionados (30 d)
- 22
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de apache/tvm
-
[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is NonePosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 3 días
-
[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradientsPosiblemente ocupada @pierceluu10 la tomó hace 4 días. Abiertoneeds-triage type: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 3 días
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputsPosiblemente ocupada @tandede la tomó hace 8 días. Abiertoneeds-triage type: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 3 días
-
needs-triage type: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
apache/tvm#21043 · 1 comentario ·
Los mantenedores suelen responder en 3 días
-
[Bug][Relax][TOPI] RoiAlign mode=max returns the average of the sample pointsPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abiertoneeds-triage type: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 3 días
Todos los issues de apache/tvm
Issues similares
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 78/100
web-push-libs/pywebpush#216 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
AgriciDaniel/claude-seo#572 ·
Los mantenedores suelen responder en 5 días
-
area:print-dispatch
Dificultad 2/5 1-3 horas Aptitud para principiantes 92/100
Los mantenedores suelen responder en 1 día
-
area: docs domain: technical-writing good first issue type: documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 80/100
Deepak3699/Ai_Mentor#919 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día