Closed
[Bug-Report|缺陷反馈]: aclnnGemmaRmsNorm 的 rstdOut 元素数校验缺失:声明 1 个元素时内核按行数写入并越界写 28 字节 #7333
liuhanwen8Add on  27 days agoClosed at  17 days ago
liuhanwen8
liuhanwen8
27 days ago Create

现象

aclnnGemmaRmsNorm 对 rstdOut 只校验「元素数是否为 1 / 是否等于 y 的元素数」,不校验是否等于行数 rows = x.numel / gamma.numel。调用方把 rstdOut 声明成 1 个元素时,两段接口都返回 0,内核仍按 rows 写入:rows=8、只声明 4 字节时实测越界写 28 字节。

输入 两段返回 声明边界外写入
rstdOut=fp32[8,1](合法,8 元素) 0 / 0 0 B(对照组)
rstdOut=fp32[1,1](只声明 1 元素) 0 / 0 28 B(缺陷组)

期望

资料《aclnnGemmaRmsNorm》参数表 rstdOut 行:「shape与入参 x 的 shape 前几维保持一致,前几维指 x 的维度减去 gamma 的维度」,即元素数必须等于 x.numel / gamma.numel(本例 8)。因此只声明 1 个元素时应返回 561002,不应返回 0 并越界写。

环境

  • 硬件:Atlas A2 训练系列(Ascend 910B2),npu-smi 26.1.1
  • CANN:9.2.0(toolkit + 910b ops 包),Linux aarch64;编译器 g++ 11.4.0
  • 仓库与提交:cann/ops-nn master 1706758a5bba769cde832b0f3a0b123fe684ac7f(2026-10-26 fetch)
  • 复现日期:2026-10-26

复现步骤

  1. 保存下方脚本为 repro.cpp
  2. g++ -std=c++17 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi
  3. ./repro
  4. 观察 DEFECT ... written_beyond = 28(应为 0;对照组恒为 0)

复现脚本

// aclnnGemmaRmsNorm rstdOut 越界写最小复现(Ascend 910B2 / CANN 9.2.0)
// 编译: g++ -std=c++17 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include \
//         -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi
// 运行: ./repro  (退出码 0 = 未检出越界,1 = 检出越过声明边界的写入)
#include <cstdio>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_gemma_rms_norm.h"

static aclTensor* mk(const std::vector<int64_t>& s, void* d)
{
    std::vector<int64_t> st(s.size(), 1);
    for (int64_t i = (int64_t)s.size() - 2; i >= 0; --i) st[i] = s[i + 1] * st[i + 1];
    return aclCreateTensor(s.data(), s.size(), ACL_FLOAT, st.data(), 0,
                           ACL_FORMAT_ND, s.data(), s.size(), d);
}

// declared:调用方给 rstdOut 声明的元素数(合法值为 8);缓冲区固定 256B 守卫
static int run(const char* tag, int64_t declared)
{
    const int64_t rows = 8, K = 128, GUARD = 256;
    std::vector<float> xh(rows * K, 0.5f), gh(K, 0.5f);
    void *xd = nullptr, *gd = nullptr, *yd = nullptr, *rd = nullptr;
    aclrtMalloc(&xd, rows * K * 4, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&gd, K * 4, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&yd, rows * K * 4, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&rd, GUARD, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(xd, rows * K * 4, xh.data(), rows * K * 4, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(gd, K * 4, gh.data(), K * 4, ACL_MEMCPY_HOST_TO_DEVICE);
    std::vector<unsigned char> fill(GUARD, 0x7F);
    aclrtMemcpy(rd, GUARD, fill.data(), GUARD, ACL_MEMCPY_HOST_TO_DEVICE);
    aclTensor* x = mk({rows, K}, xd);
    aclTensor* g = mk({K}, gd);
    aclTensor* y = mk({rows, K}, yd);
    aclTensor* r = mk({declared, 1}, rd);
    aclrtStream s = nullptr;
    aclrtCreateStream(&s);
    uint64_t ws = 0;
    aclOpExecutor* ex = nullptr;
    aclnnStatus s1 = aclnnGemmaRmsNormGetWorkspaceSize(x, g, 1e-6, y, r, &ws, &ex);
    int s2 = -1, s3 = -1;
    if (s1 == 0 && ex != nullptr) {
        void* w = nullptr;
        if (ws > 0) aclrtMalloc(&w, ws, ACL_MEM_MALLOC_HUGE_FIRST);
        s2 = (int)aclnnGemmaRmsNorm(w, ws, ex, s);
        s3 = (int)aclrtSynchronizeStream(s);
        if (w) aclrtFree(w);
    }
    std::vector<unsigned char> got(GUARD, 0);
    aclrtMemcpy(got.data(), GUARD, rd, GUARD, ACL_MEMCPY_DEVICE_TO_HOST);
    size_t legal = (size_t)declared * 4, beyond = 0;
    for (size_t i = legal; i < GUARD; ++i) if (got[i] != 0x7F) ++beyond;
    int rc = (s1 == 0 && s2 == 0 && s3 == 0) ? 0 : 1;
    if (beyond > 0) rc = 1;
    std::printf("%s return code = %d\n", tag, rc);
    std::printf("%s aclnn_ret = %d/%d/%d, declared_rstd_bytes = %zu, written_beyond = %zu\n",
                tag, (int)s1, s2, s3, legal, beyond);
    aclDestroyTensor(x); aclDestroyTensor(g); aclDestroyTensor(y); aclDestroyTensor(r);
    aclrtFree(xd); aclrtFree(gd); aclrtFree(yd); aclrtFree(rd);
    aclrtDestroyStream(s);
    return beyond > 0 ? 1 : 0;
}

int main()
{
    aclInit(nullptr);
    aclrtSetDevice(0);
    run("CTRL", 8);               // 合法:8 个元素(32B)
    int oob = run("DEFECT", 1);   // 非法:1 个元素(4B)
    aclrtResetDevice(0);
    aclFinalize();
    return oob > 0 ? 1 : 0;
}

实测输出:

CTRL return code = 0
CTRL aclnn_ret = 0/0/0, declared_rstd_bytes = 32, written_beyond = 0
DEFECT return code = 1
DEFECT aclnn_ret = 0/0/0, declared_rstd_bytes = 4, written_beyond = 28

影响

按「标量 rstd」构造出参的调用方(1 元素张量、或复用别的算子的 [1] 形状模板)会静默踩到:两段返回 0、rstd 前几个值看起来正确,但内核已把 rows × 4 字节写进相邻显存——相邻张量被踩(数值错误、难定位)或堆元数据损坏(后续释放崩溃)。rows 越大越界越多(rows=4096 约 16 KB)。

建议

  1. 第一段补 rstdOut->Numel() != x->Numel() / gamma->Numel() 时返回 561002;另实测 rstdOut 声明成 y 的形状 [2,16] 也被接受,且只写前 2 个元素、其余保持调用方旧值。
  2. 内核写 rstd 前用 rstdOut 元素数做上界保护,避免「上游校验缺失 → 内核越界」的串联失效。
  3. 资料补一句「rstdOut 元素数必须等于 x.numel / gamma.numel」,并与示例 rstdShape={2,1} 对齐(正文「前几维」不含尾 1 维,易被写成 [2])。查重:cann/ops-nn#7067 是 950 支持面文档问题、#3908 是 Reduce 轴文档提问,均与本缺陷(越界写)不同。
likedislike
liuhanwen8liuhanwen8
27 days ago Added label: bug-report
yuning_chen
yuning_chenCollaborators
26 days ago Comments:

初步分析

感谢反馈,我们已开始核对这个问题。

现象理解:文档反馈;aclnnGemmaRmsNorm 的 rstdOut 元素数校验缺失:声明 1 个元素时内核按行数写入并越界写 28 字节。
当前判断:从描述看,这是资料/注释与实现口径不一致类文档反馈「aclnnGemmaRmsNorm 的 rstdOut 元素数校验缺失:声明 1 个元素时内核按行数写入并越界写 28 字节」。需要对照接口文档、头文件注释与实现/实测结果核对哪一侧需修正。以上为初步判断,待复现确认,不能当成已确认根因。
涉及范围:aclnnGemmaRmsNorm、aclnnop、aclnn_gemma_rms_norm
下一步:我们会先核对文档声明与实现/测试结果是否一致,再决定改资料还是改代码。

likedislike
yuning_chenyuning_chenCollaborators
25 days ago Assign east_yang as responsible person
yuning_chenyuning_chenCollaborators
25 days ago Removed responsible person east_yang
yuning_chenyuning_chenCollaborators
25 days ago Assign liujie12345678 as responsible person
yuning_chen
yuning_chenCollaborators
25 days ago Comments:

您好,问题已收到,当前 @liujie12345678 正在处理中

likedislike
liujie12345678liujie12345678Collaborators
25 days ago Assign liuyun_nj as responsible person
liujie12345678liujie12345678Collaborators
25 days ago Removed responsible person liujie12345678
liuyun_njliuyun_njCollaborators
17 days ago Associated pull request
liuyun_njliuyun_njCollaborators
17 days ago changed custom state from 进行中 to 已完成
liuyun_njliuyun_njCollaborators
17 days ago Close issue