Closed
[Bug-Report|缺陷反馈]: aclnnSquaredRelu out 与 input shape 不一致未拦截并越界写 16 个元素 #7756
liuhanwen8Add on 23 days agoClosed at 18 days ago
23 days ago Added label: bug-report
23 days ago Modify description of issue
23 days ago Assign east_yang as responsible person
yuning_chen
23 days ago Comments:
23 days ago Comments:
yuning_chen
23 days ago Comments:
23 days ago Comments:
您好,该问题已收到,当前问题 @east_yang 正在处理中


east_yang
19 days ago Comments:
19 days ago Comments:
/unassign @east_yang


19 days ago Removed responsible person east_yang
east_yang
19 days ago Comments:
19 days ago Comments:
/assign @guijianwei


19 days ago Assign guijianwei as responsible person
18 days ago Associated pull request
18 days ago Close issue
17 days ago Added label: resolved
aclnnSquaredRelu 的 out 与 input shape 不一致未被拦截,并越界写 16 个元素
现象
第一段接口
aclnnSquaredReluGetWorkspaceSize没有校验out与input的 shape 是否一致:input传fp32[4x8]、out传fp32[4x4]时,两段接口都返回 0,内核按 input 的 32 个元素算完并写入只有 16 个元素的 out 缓冲区。期望
资料
activation/squared_relu/docs/aclnnSquaredRelu.md参数说明的out行写明「输出的数据类型、shape、数据格式与输入保持一致」。shape 不一致属非法入参,第一段应返回161002,不得进入执行。环境
cann/ops-nnmaster59c0720c复现步骤
repro.cpp。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。ASCEND_RT_VISIBLE_DEVICES=0 ./repro。复现脚本
#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。