Closed
[Bug-Report|缺陷反馈]: aclnnDequantSwigluQuant 出参 yOut/scaleOut 尺寸未校验,小于推导尺寸时静默越界写 #7569
liuhanwen8Add on  24 days agoClosed at  16 days ago
liuhanwen8
liuhanwen8
24 days ago Create

现象

aclnnDequantSwigluQuant 第一段接口 aclnnDequantSwigluQuantGetWorkspaceSize 不校验出参 yOut / scaleOut 的尺寸是否等于推导尺寸。把 yOut 声明成推导尺寸的一半时,两段接口都返回 0,内核按 x 推导的真实尺寸写满输出,越过声明张量边界 128 字节(静默越界写,无任何报错)。

按资料 op_host/dequant_swiglu_quant_infershape.cpp:yOut.shape = x.shape(最后一维除 2),scaleOut.shape = yOut.shape 去掉尾轴。x=[4,128] 时 yOut 应为 [4,64](256 元素)。

实测(同一份数据、同一 die、连续两次一致):

场景 yOut 声明 ret1 ret2 边界外被写字节
基线对照 [4,64](256 元素) 0 0 0
出参减半 [4,32](128 元素) 0 0 128
行数 1/4/16 声明为真实的一半 0 0 32/128/512(线性)

scaleOut 同型:声明 [1] 而真实应为 [4] 时 ret1=0 ret2=0,边界外 3 个 float 被写。对照组越界恒为 0,排除"哨兵未初始化"误判。

期望

第一段接口按资料声明的错误码表返回 ACLNN_ERR_PARAM_INVALID(161002)("输入或输出的 shape 不满足约束要求"),或在 yOut/scaleOut 尺寸小于推导尺寸时直接拒绝,不得进入执行阶段。

环境

  • 硬件:Ascend 910B2(Atlas A2),8 die;复现 die 7
  • CANN 9.2.0(ascend-toolkit/latest)、npu-smi 26.1.1、Linux aarch64/glibc 2.35
  • 算子:ops-nn/quant/dequant_swiglu_quant,源码提交 711a4f5d

复现步骤

  1. 存为 repro.cpp,编译:g++ -std=c++17 -O1 repro.cpp -I$ASCEND/include -L$ASCEND/lib64 -L$ASCEND/aarch64-linux/lib64 -lascendcl -lnnopbase -lopapi -Wl,--allow-shlib-undefined -o repro($ASCEND 指 CANN 安装根)。
  2. 运行 ./repro base:出参 yOut=[4,64],打印 边界外被写字节=0(阴性对照)。
  3. 运行 ./repro half:只把 yOut 改成 [4,32],其余不变。
  4. 观察 ret1=0 ret2=0 声明边界外被写字节=128:两段都成功,越界写成立。

复现脚本

#include <cstdio>
#include <unistd.h>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_dequant_swiglu_quant.h"

static aclTensor *mk(const std::vector<int64_t> &s, void *a, aclDataType dt) {
    std::vector<int64_t> st(s.size(), 1);
    for (int i = (int)s.size() - 2; i >= 0; i--) st[i] = s[i + 1] * st[i + 1];
    return aclCreateTensor(s.data(), s.size(), dt, st.data(), 0, aclFormat::ACL_FORMAT_ND,
                           s.data(), s.size(), a);
}
static int64_t numel(const std::vector<int64_t> &s) {
    int64_t n = 1;
    for (auto v : s) n *= v;
    return n;
}

int main(int argc, char **argv) {
    bool half = argc > 1 && argv[1][0] == 'h';
    std::vector<int64_t> xs{4, 128}, qs{64}, ss{4};
    std::vector<int64_t> ys = half ? std::vector<int64_t>{4, 32} : std::vector<int64_t>{4, 64};
    aclInit(nullptr); aclrtSetDevice(0);
    aclrtStream stream = nullptr; aclrtCreateStream(&stream);
    const int64_t yAlloc = 4 * 64 * 2;   // 缓冲开大 2 倍,全填 0xA5 哨兵
    void *xd, *qd, *od, *yd, *sd;
    aclrtMalloc(&xd, numel(xs) * 2, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&qd, numel(qs) * 4, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&od, numel(qs) * 4, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&yd, yAlloc, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&sd, 64, ACL_MEM_MALLOC_HUGE_FIRST);
    std::vector<int8_t> sent(yAlloc, (int8_t)0xA5);
    std::vector<float> qv(64, 0.01f), ov(64, 0.0f);
    std::vector<aclFloat16> xh(numel(xs));
    for (size_t i = 0; i < xh.size(); i++) xh[i] = aclFloatToFloat16(0.05f * (float)((int)i % 32));
    aclrtMemcpy(xd, numel(xs) * 2, xh.data(), numel(xs) * 2, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(qd, 256, qv.data(), 256, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(od, 256, ov.data(), 256, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(yd, yAlloc, sent.data(), yAlloc, ACL_MEMCPY_HOST_TO_DEVICE);
    aclTensor *x = mk(xs, xd, ACL_FLOAT16), *q = mk(qs, qd, ACL_FLOAT), *o = mk(qs, od, ACL_FLOAT);
    aclTensor *y = mk(ys, yd, ACL_INT8), *s = mk(ss, sd, ACL_FLOAT);
    uint64_t ws = 0; aclOpExecutor *e = nullptr; char qm[] = "dynamic";
    int r1 = (int)aclnnDequantSwigluQuantGetWorkspaceSize(x, nullptr, nullptr, nullptr, q, o,
                                                          nullptr, true, qm, y, s, &ws, &e);
    int r2 = 127;
    if (r1 == 0) {
        void *w = nullptr;
        if (ws) aclrtMalloc(&w, ws, ACL_MEM_MALLOC_HUGE_FIRST);
        r2 = (int)aclnnDequantSwigluQuant(w, ws, e, stream);
        if (r2 == 0) aclrtSynchronizeStream(stream);
    }
    std::vector<int8_t> got(yAlloc);
    aclrtMemcpy(got.data(), yAlloc, yd, yAlloc, ACL_MEMCPY_DEVICE_TO_HOST);
    int64_t decl = numel(ys), over = 0;
    for (int64_t i = 0; i < yAlloc; i++) if (got[i] != (int8_t)0xA5 && i >= decl) over++;
    printf("mode=%s yOut声明元素=%ld ret1=%d ret2=%d 边界外被写字节=%ld\n",
           half ? "half" : "base", (long)decl, r1, r2, (long)over);
    fflush(stdout); _exit(0);
}

实测输出:

mode=base yOut声明元素=256 ret1=0 ret2=0 边界外被写字节=0
mode=half yOut声明元素=128 ret1=0 ret2=0 边界外被写字节=128

影响

  • 内存踩踏:调用方按声明的 yOut 尺寸申请显存时,内核会写到缓冲之外,破坏相邻张量且无报错。推理框架里出参常按 x.numel()/2 自算,一旦与内核推导不一致就是静默越界写。
  • 静默性:两段都返回 0,上层无法通过返回值发现问题,只能事后对比输出或 msSanitizer 才发现。
  • 与同族 issue 的区别:#7558 是"合法输入被拒(561002)"、#7559 是"资料未声明 quantScale 形状";本条是出参尺寸不校验导致越界写,方向相反(该拒的没拒),修复点在第一段接口的出参 shape 校验。

建议

  1. 在 dequant_swiglu_quant 的第一段接口(或 InferShape 之后的 check 环节)增加出参校验:yOut.shape == x.shape(末维 H/2)、scaleOut.shape == yOut.shape[:-1],不满足返回 161002。
  2. 同类可选出参(scaleOut)一并纳入校验,避免只校验 yOut 时 scaleOut 仍可小尺寸越界。
  3. 补一条 UT:x=[4,128] + yOut=[4,32] 期望 161002;x=[4,128] + scaleOut=[1] 期望 161002。
likedislike
liuhanwen8liuhanwen8
24 days ago Added label: bug-report
yuning_chen
yuning_chenCollaborators
24 days ago Comments:

初步分析

感谢反馈。

现象理解:
DequantSwigluQuant 未校验 yOut/scaleOut 尺寸,声明过小时两段返回 0 并越界写。

当前判断:初步判断,待复现确认。
偏出参尺寸校验缺失,属静默内存安全风险。

涉及范围:
dequant_swiglu_quant 出参 shape 校验与 kernel 写回。

下一步:
首段按推导尺寸校验并拒绝;内核加写上界保护。

likedislike
yuning_chenyuning_chenCollaborators
24 days ago Assign east_yang as responsible person
yuning_chen
yuning_chenCollaborators
24 days ago Comments:

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

likedislike
east_yang
east_yangCollaborators
19 days ago Comments:

/unassign @east_yang

likedislike
CANN-robotCANN-robotCollaborators
19 days ago Removed responsible person east_yang
east_yang
east_yangCollaborators
19 days ago Comments:

/assign @flag2233

likedislike
CANN-robotCANN-robotCollaborators
19 days ago Assign flag2233 as responsible person
flag2233flag2233Collaborators
17 days ago Associated pull request
CANN-robotCANN-robotCollaborators
16 days ago Close issue
CANN-robotCANN-robotCollaborators
16 days ago Added label: resolved
chenjiayi_
chenjiayi_Collaborators
10 days ago Comments:

补充说明本问题的修复结果:

结论:已在 tiling 阶段补齐 yOut / scaleOut 的尺寸校验,声明尺寸与推导不一致时第一段直接失败,不再进入执行阶段造成越界写。错误码为 561002(ACLNN_ERR_INNER_TILING_ERROR),与资料错误码表「输入或输出的 shape 不满足约束要求」一致,不是 161002。

修复内容(文件/函数):

  • quant/dequant_swiglu_quant/op_host/dequant_swiglu_quant_tiling_base.cpp(Base 路径):三处只打日志不返回的校验补上 return ge::GRAPH_FAILED——y 与 x 的 rank 不等、y 尾维不等于 x 尾维的一半、scaleOut 元素数不等于 rowLen;新增前导维逐维相等校验(x=[4,128] 配 y=[2,64] 这类前导维被压缩的形态同样会越界);尾维校验的日志改为打印实际 dim 值。
  • quant/dequant_swiglu_quant/op_host/dequant_swiglu_quant_tiling.cpp(Dsk 路径,groupIndex 非空时走这里):原代码只从 y 反推 outDimy_,没有任何尺寸校验,现补——y rank 与 x 一致、前导维逐维相等、outDimy_ * 2 == inDimy_、scaleOut 元素数等于 x 的行数,并对 GetOutputShape(1) 返回空指针的情况做保护后返回失败。

验证:

  • 新增 7 个 UT:Base 4 例(y 尾维减半、y rank 不等、y 前导维减半、scaleOut 尺寸不符),Dsk 3 例(group 场景 y 尾维减半、y 前导维减半、scaleOut 尺寸不符);这 7 例在未打补丁时全部失败、打补丁后全部通过;
  • dequant_swiglu_quant 全量 op_host UT 75 例通过。

该 issue 已随修复合入关闭。

MR 链接:https://gitcode.com/cann/ops-nn/merge_requests/13590

likedislike