Closed
[Bug-Report|缺陷反馈]: aclnnSquaredRelu out 与 input shape 不一致未拦截并越界写 16 个元素 #7756
liuhanwen8Add on  23 days agoClosed at  18 days ago
liuhanwen8
liuhanwen8
23 days ago Create

aclnnSquaredRelu 的 out 与 input shape 不一致未被拦截,并越界写 16 个元素

现象

第一段接口 aclnnSquaredReluGetWorkspaceSize 没有校验 out 与 input 的 shape 是否一致:input 传 fp32[4x8]、out 传 fp32[4x4] 时,两段接口都返回 0,内核按 input 的 32 个元素算完并写入只有 16 个元素的 out 缓冲区。

用例 input out 首段返回 out 元素数 越界写元素数
对照 fp32[4x8] fp32[4x8] 0 32 0
缺陷 fp32[4x8] fp32[4x4] 0(预期 161002) 16 16
缺陷 fp32[4x8] fp32[1x8] 0(预期 161002) 8 24
CTRL   return code = 0
CTRL   written beyond out numel(32) = 0
DEFECT return code = 0
DEFECT written beyond out numel(16) = 16

期望

资料 activation/squared_relu/docs/aclnnSquaredRelu.md 参数说明的 out 行写明「输出的数据类型、shape、数据格式与输入保持一致」。shape 不一致属非法入参,第一段应返回 161002,不得进入执行。

环境

  • 昇腾 910B3(Atlas A2 训练系列),CANN 9.2.0
  • 算子仓 cann/ops-nn master 59c0720c

复现步骤

  1. 把下方脚本保存为 repro.cpp。
  2. 编译:g++ -O2 -std=c++17 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi_nn -lopapi_math。
  3. 运行:ASCEND_RT_VISIBLE_DEVICES=0 ./repro。
  4. 对照行应输出 return code = 0、越界写 = 0;缺陷行输出 return code = 0(应 161002)、越界写 = 16。

复现脚本

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

static const float SENT = -12345.0f;

static void *Dev(const std::vector<float> &v)
{
    void *p = nullptr;
    aclrtMalloc(&p, v.size() * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(p, v.size() * sizeof(float), v.data(), v.size() * sizeof(float), ACL_MEMCPY_HOST_TO_DEVICE);
    return p;
}

static std::vector<int64_t> Strides(const std::vector<int64_t> &d)
{
    std::vector<int64_t> s(d.size(), 1);
    for (int i = (int)d.size() - 2; i >= 0; --i) s[i] = s[i + 1] * d[i + 1];
    return s;
}

// outShape 是 aclTensor 声明形状;cap 是底层 buffer 实际 float 数
static void Run(const char *tag, std::vector<int64_t> inShape, std::vector<int64_t> outShape,
                int64_t cap, aclrtStream stream)
{
    int64_t inN = 1, outN = 1;
    for (auto d : inShape) inN *= d;
    for (auto d : outShape) outN *= d;
    std::vector<float> inVals((size_t)inN), outVals((size_t)cap, SENT);
    for (int64_t i = 0; i < inN; ++i) inVals[i] = (float)(i % 7) - 3.0f;
    void *iPtr = Dev(inVals), *oPtr = Dev(outVals);
    auto is = Strides(inShape), os = Strides(outShape);
    aclTensor *in = aclCreateTensor(inShape.data(), inShape.size(), ACL_FLOAT, is.data(), 0,
                                    ACL_FORMAT_ND, inShape.data(), inShape.size(), iPtr);
    aclTensor *out = aclCreateTensor(outShape.data(), outShape.size(), ACL_FLOAT, os.data(), 0,
                                     ACL_FORMAT_ND, outShape.data(), outShape.size(), oPtr);
    uint64_t ws = 0;
    aclOpExecutor *exec = nullptr;
    int ret = aclnnSquaredReluGetWorkspaceSize(in, out, &ws, &exec);
    if (ret == 0) {
        void *w = nullptr;
        if (ws > 0) aclrtMalloc(&w, ws, ACL_MEM_MALLOC_HUGE_FIRST);
        ret = aclnnSquaredRelu(w, ws, exec, stream);
        if (ret == 0) aclrtSynchronizeStream(stream);
        if (w) aclrtFree(w);
    }
    std::vector<float> back((size_t)cap, 0.0f);
    aclrtMemcpy(back.data(), back.size() * sizeof(float), oPtr, back.size() * sizeof(float),
                ACL_MEMCPY_DEVICE_TO_HOST);
    int64_t oob = 0;
    for (int64_t i = outN; i < cap; ++i) if (back[i] != SENT) ++oob;
    std::printf("%s return code = %d\n", tag, ret);
    std::printf("%s written beyond out numel(%lld) = %lld\n", tag, (long long)outN, (long long)oob);
    aclDestroyTensor(in);
    aclDestroyTensor(out);
    aclrtFree(iPtr);
    aclrtFree(oPtr);
}

int main()
{
    aclInit(nullptr);
    aclrtSetDevice(0);
    aclrtStream stream = nullptr;
    aclrtCreateStream(&stream);
    Run("CTRL  ", {4, 8}, {4, 8}, 32, stream);
    Run("DEFECT", {4, 8}, {4, 4}, 32, stream);
    aclrtDestroyStream(stream);
    aclrtResetDevice(0);
    aclFinalize();
    return 0;
}

影响

调用方把 out 传成更小或可 broadcast 的 shape,就会在无任何错误码的情况下越界写 HBM,写坏相邻缓冲区并导致后续算子偶发精度异常或崩溃。

建议

在 aclnnSquaredReluGetWorkspaceSize 的参数校验里补 out 与 input 的 shape 一致性检查(与已有 dtype 一致性检查放在同一处),不一致返回 161002;同时补一条 out shape 不一致的 UT。

likedislike
liuhanwen8liuhanwen8
23 days ago Added label: bug-report
liuhanwen8liuhanwen8
23 days ago Modify description of issue
yuning_chenyuning_chenCollaborators
23 days ago Assign east_yang as responsible person
yuning_chen
yuning_chenCollaborators
23 days ago Comments:

初步分析

感谢反馈。

现象理解:aclnnSquaredRelu 在 out 与 input 关系不满足约束时行为异常(见正文复现)。
当前判断:初步判断,待复现确认。出参与入参约束校验可能不完整。
涉及范围:activation/squared_relu op_api/host。
下一步:按资料补齐校验与错误码并回归。

likedislike
yuning_chen
yuning_chenCollaborators
23 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 @guijianwei

likedislike
CANN-robotCANN-robotCollaborators
19 days ago Assign guijianwei as responsible person
guijianweiguijianweiCollaborators
18 days ago Associated pull request
CANN-robotCANN-robotCollaborators
18 days ago Close issue
CANN-robotCANN-robotCollaborators
17 days ago Added label: resolved