[Bug][TIR] `s_tir.Schedule.cache_read` omits domain bounds predicate on shifted conditional reads, causing negative-index heap buffer underflow (`SIGABRT`/`SIGSEGV`)

未关闭
#21,124 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
57/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp, python
领域
compilers

调研方向

Start in src/s_tir/schedule/primitive/cache_read_write.cc, in MakeCacheStage, and compare its cache block predicate with predicate generation in ComputeAt. Use the supplied reproduction to inspect the scheduled B_local block and confirm the missing bounds predicate causes the negative index. Done means the cache copy preserves bounds safety and the reproduction exits successfully with the expected results.

由索引模型根据 Issue 内容生成。

描述

needs-triage type: bug
Expected behavior

Applying valid S-TIR primitives (split, compute_at, cache_read) to a producer-consumer PrimFunc where the consumer conditionally reads a shifted index (T.if_then_else(4 <= v0, B[v0 - 4, v1], A[v0, v1])) must preserve bounds safety and produce identical results (sum=12288.0, exit code 0).

Actual behavior

s_tir.Schedule.cache_read generates an unguarded B_local cache-copy block with v0 = T.axis.spatial(2, ax0 - 4 + ax0_1) and predicate = true (missing T.where(4 <= ax0)), even though the adjacent compute_at producer block in the same loop correctly carries T.where(4 <= ax0).
After CompactBufferAllocation, B and B_local are compacted to (1024,) and unconditionally accessed at negative indices -4096..-1 for ax0 in 0..3:

B_local[ax0 * 1024 + ax1 - T.max(0, ax0 - 4) * 1024 - 4096] = B[...]

At runtime this underflows the heap allocation, corrupting TVM's WorkspacePool and glibc heap metadata and crashing with SIGABRT (corrupted double-linked list / double free or corruption (out)) or SIGSEGV depending on heap layout.

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 numpy as np
import tvm
from tvm import s_tir
from tvm.script import tirx as T
@T.prim_func(s_tir=True)
def slice_then_concat(
    X: T.Buffer((T.int64(5), T.int64(2048)), "float32"),
    A: T.Buffer((T.int64(4), T.int64(2048)), "float32"),
    C: T.Buffer((T.int64(6), T.int64(2048)), "float32"),
):
    B = T.sblock_alloc_buffer((T.int64(2), T.int64(2048)), "float32")
    for ax0, ax1 in T.grid(T.int64(2), T.int64(2048)):
        with T.sblock("slice"):
            v0, v1 = T.axis.remap("SS", [ax0, ax1])
            B[v0, v1] = X[v0 + T.int64(3), v1]
    for ax0, ax1 in T.grid(T.int64(6), T.int64(2048)):
        with T.sblock("concat"):
            v0, v1 = T.axis.remap("SS", [ax0, ax1])
            C[v0, v1] = T.if_then_else(T.int64(4) <= v0, B[v0 - T.int64(4), v1], A[v0, v1])
sch = s_tir.Schedule(slice_then_concat)
blk_s = sch.get_sblock("slice")
blk_c = sch.get_sblock("concat")
_, l1 = sch.get_loops(blk_c)
l1_o, _ = sch.split(l1, factors=[None, 1024])
sch.compute_at(blk_s, l1_o, preserve_unit_loops=True)
sch.cache_read(blk_c, 0, "local")
f = tvm.compile(sch.mod, target="llvm")
f(
    tvm.runtime.tensor(np.ones((5, 2048), "float32")),
    tvm.runtime.tensor(np.ones((4, 2048), "float32")),
    tvm.runtime.tensor(np.zeros((6, 2048), "float32")),
)
# Crashes with SIGABRT (e.g. corrupted double-linked list / double free or corruption) or SIGSEGV
Suggested fix

In src/s_tir/schedule/primitive/cache_read_write.cc (MakeCacheStage), derive the SBlockRealize predicate from the domain bounds of each cache block iter var (0 <= iter_value < dom->extent, matching compute_at / ComputeAt predicate generation) rather than hardcoding IntImm::Bool(true).

主要语言
Python
星标
13.9k
派生
4.1k
平均合并
4 天 22 小时
30 天内合并 PR
22

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/tvm 的其他 Issue

查看 apache/tvm 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。