Merged
增加对opType是否越界的校验 #803
ren-botaoAdd on Aug 31
增加对opType是否越界的校验 #803
Merged
Merged fromren-botao/opbaseop_type_max_checkintocann/opbasemaster
Total: 10 files changed+64-16
| @@ -37,6 +37,8 @@ constexpr aclnnStatus ACLNN_ERR_INNER_RUNTIME_INVALID_HANDLE = 561201; | |||
| 37 | 37 | ||
| 38 | constexpr uint32_t KERNEL_UTILS_THIRTY_TWO_BIT = 32; | 38 | constexpr uint32_t KERNEL_UTILS_THIRTY_TWO_BIT = 32; |
| 39 | 39 | ||
| 40 | +constexpr size_t MAX_OP_TYPE_COUNT = 1024; // max registered op type | ||
| 41 | + | ||
| 40 | // Aligns the supplied size to the specified PowerOfTwo | 42 | // Aligns the supplied size to the specified PowerOfTwo |
| 41 | inline size_t AlignSize(size_t sizeToAlign, size_t powerOfTwo) | 43 | inline size_t AlignSize(size_t sizeToAlign, size_t powerOfTwo) |
| 42 | { | 44 | { |
| @@ -459,8 +459,8 @@ aclnnStatus KernelMgr::ParseStaticKernels(uint32_t opType) | |||
| 459 | aclnnStatus KernelMgr::AclOpKernelInit(uint32_t opType) | 459 | aclnnStatus KernelMgr::AclOpKernelInit(uint32_t opType) |
| 460 | { | 460 | { |
| 461 | // init_ change name to initDynKernelFlags_. 2025.7.8 | 461 | // init_ change name to initDynKernelFlags_. 2025.7.8 |
| 462 | - if (opType >= MAX_OP_TYPE_COUNT) { | 462 | + if (opType >= MAX_OP_TYPE_COUNT || opType == 0) { |
| 463 | - OP_LOGD("Op type %u %s is larger than %zu has been initialized.", opType, | 463 | + OP_LOGW("Op type %u %s is invalid, opType may be unregistered or exceed max count %zu.", opType, |
| 464 | OpTypeDict::ToString(opType).GetString(), MAX_OP_TYPE_COUNT); | 464 | OpTypeDict::ToString(opType).GetString(), MAX_OP_TYPE_COUNT); |
| 465 | return ACLNN_SUCCESS; | 465 | return ACLNN_SUCCESS; |
| 466 | } | 466 | } |
| @@ -228,7 +228,7 @@ struct NonFiniteCheckTensorList { | |||
| 228 | 228 | ||
| 229 | struct NonFiniteCheckOpContext { | 229 | struct NonFiniteCheckOpContext { |
| 230 | NonFiniteCheckOpContext(aclOpExecutor* executor, aclrtStream stream, OpArgContext* opArgCtx) | 230 | NonFiniteCheckOpContext(aclOpExecutor* executor, aclrtStream stream, OpArgContext* opArgCtx) |
| 231 | - : executor_(executor), stream_(stream), opArgCtx_(opArgCtx), bin_(nullptr) {}; | 231 | + : executor_(executor), stream_(stream), opArgCtx_(opArgCtx), bin_(nullptr){}; |
| 232 | 232 | ||
| 233 | aclOpExecutor* executor_; | 233 | aclOpExecutor* executor_; |
| 234 | aclrtStream stream_; | 234 | aclrtStream stream_; |
| @@ -244,6 +244,7 @@ public: | |||
| 244 | static aclnnStatus RunNonfiniteCheckOp(NonFiniteCheckOpContext& nonFiniteCheckOpCtx, bool& dump) | 244 | static aclnnStatus RunNonfiniteCheckOp(NonFiniteCheckOpContext& nonFiniteCheckOpCtx, bool& dump) |
| 245 | { | 245 | { |
| 246 | static uint32_t opid = OpTypeDict::ToOpType("NonFiniteCheck"); | 246 | static uint32_t opid = OpTypeDict::ToOpType("NonFiniteCheck"); |
| 247 | + OP_CHECK(opid != 0, OP_LOGW("NonFiniteCheck op is not registered, opid is 0."), return ACLNN_ERR_INNER); | ||
| 247 | (void)gKernelMgr.AclOpKernelInit(opid); | 248 | (void)gKernelMgr.AclOpKernelInit(opid); |
| 248 | 249 | ||
| 249 | std::unordered_map<op::DataType, std::vector<aclTensor*>> dtype2Tensors; | 250 | std::unordered_map<op::DataType, std::vector<aclTensor*>> dtype2Tensors; |
| @@ -174,8 +174,7 @@ constexpr size_t MAX_ATTR_STRING_SIZE = 1024; // Reference: max op attr string l | |||
| 174 | constexpr size_t ATTR_CAPACITY = 32 * 1024; // Initial attr capacity: max total op attr data size in Bytes | 174 | constexpr size_t ATTR_CAPACITY = 32 * 1024; // Initial attr capacity: max total op attr data size in Bytes |
| 175 | // MAX_OP_ARG_NUM is used as the initial capacity for op args (inputs/outputs). | 175 | // MAX_OP_ARG_NUM is used as the initial capacity for op args (inputs/outputs). |
| 176 | // When arg count exceeds this value, it will automatically expand (2x growth factor). | 176 | // When arg count exceeds this value, it will automatically expand (2x growth factor). |
| 177 | -constexpr size_t MAX_OP_ARG_NUM = 1024; // Initial arg capacity: max op input/output arg count | 177 | +constexpr size_t MAX_OP_ARG_NUM = 1024; // Initial arg capacity: max op input/output arg count |
| 178 | -constexpr size_t MAX_OP_TYPE_COUNT = 1024; // max registed op type | ||
| 179 | } // namespace op::internal | 178 | } // namespace op::internal |
| 180 | 179 | ||
| 181 | #endif | 180 | #endif |
| @@ -1404,12 +1404,15 @@ public: | |||
| 1404 | const char* simpKey = nullptr; | 1404 | const char* simpKey = nullptr; |
| 1405 | auto ret = GenerateStaticParam(inputs, outputs, attrs, simpKey); | 1405 | auto ret = GenerateStaticParam(inputs, outputs, attrs, simpKey); |
| 1406 | if (ret != ACLNN_SUCCESS || simpKey == nullptr) { | 1406 | if (ret != ACLNN_SUCCESS || simpKey == nullptr) { |
| 1407 | + OP_LOGW("Failed to generate simplified key for static bin of op %s, GenerateStaticParam returned %d, " | ||
| 1408 | + "simplified key is %s.", | ||
| 1409 | + op::OpTypeDict::ToString(opType_).GetString(), ret, simpKey == nullptr ? "null" : "valid"); | ||
| 1407 | return nullptr; | 1410 | return nullptr; |
| 1408 | } | 1411 | } |
| 1409 | size_t hash = HashBinary(simpKey, strlen(simpKey)); | 1412 | size_t hash = HashBinary(simpKey, strlen(simpKey)); |
| 1410 | auto iter = staticBins_.find(hash); | 1413 | auto iter = staticBins_.find(hash); |
| 1411 | if (iter == staticBins_.end()) { | 1414 | if (iter == staticBins_.end()) { |
| 1412 | - OP_LOGI("Cannot find static bin of op %s, simplified key %s.", | 1415 | + OP_LOGW("Cannot find static bin of op %s, simplified key %s.", |
| 1413 | op::OpTypeDict::ToString(opType_).GetString(), simpKey); | 1416 | op::OpTypeDict::ToString(opType_).GetString(), simpKey); |
| 1414 | return nullptr; | 1417 | return nullptr; |
| 1415 | } | 1418 | } |
| @@ -34,6 +34,9 @@ aclnnStatus OpRunContextMgr::InitOpFunctions(uint32_t opType) | |||
| 34 | // This function may be called in phase 1 and phase 2 thread. | 34 | // This function may be called in phase 1 and phase 2 thread. |
| 35 | op::opploader::LoadAllOppPackage(); | 35 | op::opploader::LoadAllOppPackage(); |
| 36 | 36 | ||
| 37 | + CHECK_COND(opType < MAX_OP_TYPE_COUNT, ACLNN_ERR_INNER, "opType %u exceeds max count %zu.", opType, | ||
| 38 | + MAX_OP_TYPE_COUNT); | ||
| 39 | + | ||
| 37 | static std::once_flag flag[MAX_OP_TYPE_COUNT]; | 40 | static std::once_flag flag[MAX_OP_TYPE_COUNT]; |
| 38 | aclnnStatus ret = ACLNN_SUCCESS; | 41 | aclnnStatus ret = ACLNN_SUCCESS; |
| 39 | auto f = [&ret, &opType]() { | 42 | auto f = [&ret, &opType]() { |
| @@ -165,6 +165,7 @@ public: | |||
| 165 | const std::vector<MemSetTensorInfo>& memsetTensorInfo) | 165 | const std::vector<MemSetTensorInfo>& memsetTensorInfo) |
| 166 | { | 166 | { |
| 167 | static uint32_t opType = OpTypeDict::ToOpType("MemSet"); | 167 | static uint32_t opType = OpTypeDict::ToOpType("MemSet"); |
| 168 | + OP_CHECK(opType != 0, OP_LOGW("MemSet op is not registered, opType is 0."), return nullptr); | ||
| 168 | OP_CHECK(InitOpFunctions(opType) == ACLNN_SUCCESS, OP_LOGE(ACLNN_ERR_INNER, "InitOpFunctions failed"), | 169 | OP_CHECK(InitOpFunctions(opType) == ACLNN_SUCCESS, OP_LOGE(ACLNN_ERR_INNER, "InitOpFunctions failed"), |
| 169 | return nullptr); | 170 | return nullptr); |
| 170 | if (opTilingFuncs_[opType] == nullptr || opTilingFuncs_[opType]->tiling == nullptr) { | 171 | if (opTilingFuncs_[opType] == nullptr || opTilingFuncs_[opType]->tiling == nullptr) { |
| @@ -18,6 +18,7 @@ | |||
| 18 | #include <strings.h> | 18 | #include <strings.h> |
| 19 | #include "opdev/op_def.h" | 19 | #include "opdev/op_def.h" |
| 20 | #include "opdev/op_log.h" | 20 | #include "opdev/op_log.h" |
| 21 | +#include "kernel_utils.h" | ||
| 21 | 22 | ||
| 22 | namespace op { | 23 | namespace op { |
| 23 | 24 | ||
| @@ -128,7 +129,11 @@ aclnnStatus OpTypeDict::Add(uint32_t& id, const char* opName) | |||
| 128 | opTypeNum++; | 129 | opTypeNum++; |
| 129 | } | 130 | } |
| 130 | 131 | ||
| 131 | - // check duplicate | 132 | + if (opTypeNum >= op::internal::MAX_OP_TYPE_COUNT) { |
| 133 | + OP_LOGE(ACLNN_ERR_INNER, "Op type count %u reaches upper limit %zu, op %s registration rejected.", | ||
| 134 | + opTypeNum.load(), op::internal::MAX_OP_TYPE_COUNT, opName); | ||
| 135 | + return ACLNN_ERR_INNER; | ||
| 136 | + } | ||
| 132 | id = opTypeNum; | 137 | id = opTypeNum; |
| 133 | opTypeName_.emplace_back(ge::AscendString(opName)); | 138 | opTypeName_.emplace_back(ge::AscendString(opName)); |
| 134 | string opConfigFileStr(GetConfigJsonName(opTypeName_.back())); | 139 | string opConfigFileStr(GetConfigJsonName(opTypeName_.back())); |
| @@ -207,6 +212,11 @@ void BinConfigJsonDict::UpdateConfigJsonPath(uint32_t opType, const std::string& | |||
| 207 | std::vector<std::string> GetOpConfigJsonFileName(uint32_t opType) | 212 | std::vector<std::string> GetOpConfigJsonFileName(uint32_t opType) |
| 208 | { | 213 | { |
| 209 | const std::lock_guard<std::mutex> lock(opTypeMutex); | 214 | const std::lock_guard<std::mutex> lock(opTypeMutex); |
| 215 | + if (opType >= BinConfigJsonDict::opConfigJsonPath_.size()) { | ||
| 216 | + OP_LOGW("opType %u exceeds opConfigJsonPath_ size %zu, return empty.", opType, | ||
| 217 | + BinConfigJsonDict::opConfigJsonPath_.size()); | ||
| 218 | + return {}; | ||
| 219 | + } | ||
| 210 | return BinConfigJsonDict::opConfigJsonPath_[opType]; | 220 | return BinConfigJsonDict::opConfigJsonPath_[opType]; |
| 211 | } | 221 | } |
| 212 | 222 | ||
| @@ -266,23 +266,25 @@ TEST_F(KernelLaunchNewRtsUT, abnormalCase1) | |||
| 266 | int dummyStream = 0; | 266 | int dummyStream = 0; |
| 267 | void* stream = &dummyStream; | 267 | void* stream = &dummyStream; |
| 268 | 268 | ||
| 269 | + // opType 超过上限,在 AclOpKernelInit 入口被拦截(观察日志 "Op type ... is invalid" warning)。 | ||
| 269 | uint32_t opType = static_cast<uint32_t>(op::internal::MAX_OP_TYPE_COUNT) + 1; | 270 | uint32_t opType = static_cast<uint32_t>(op::internal::MAX_OP_TYPE_COUNT) + 1; |
| 270 | - auto rc2 = op::internal::gKernelMgr.Run(opType, stream, ctx); | ||
| 271 | - EXPECT_NE(rc2, ACL_SUCCESS); | ||
| 272 | const size_t* workspaceSize = nullptr; | 271 | const size_t* workspaceSize = nullptr; |
| 273 | size_t workspaceNum = 0; | 272 | size_t workspaceNum = 0; |
| 274 | - auto rc3 = op::internal::gKernelMgr.GetWorkspace( | 273 | + auto rc = op::internal::gKernelMgr.GetWorkspace( |
| 275 | opType, workspaceSize, workspaceNum, *ctx->GetOpArg(op::OpArgDef::OP_OPTION_ARG), | 274 | opType, workspaceSize, workspaceNum, *ctx->GetOpArg(op::OpArgDef::OP_OPTION_ARG), |
| 276 | *ctx->GetOpArg(op::OpArgDef::OP_OUTPUT_ARG), *ctx->GetOpArg(op::OpArgDef::OP_ATTR_ARG)); | 275 | *ctx->GetOpArg(op::OpArgDef::OP_OUTPUT_ARG), *ctx->GetOpArg(op::OpArgDef::OP_ATTR_ARG)); |
| 277 | - EXPECT_NE(rc3, ACL_SUCCESS); | ||
| 278 | - | ||
| 279 | - uint32_t opType1 = op::OpTypeDict::ToOpType("Axpy1"); | ||
| 280 | - auto rc = op::internal::gKernelMgr.Run(opType1, stream, ctx); | ||
| 281 | EXPECT_NE(rc, ACL_SUCCESS); | 276 | EXPECT_NE(rc, ACL_SUCCESS); |
| 282 | - auto rc1 = op::internal::gKernelMgr.GetWorkspace( | 277 | + rc = op::internal::gKernelMgr.Run(opType, stream, ctx); |
| 278 | + EXPECT_NE(rc, ACL_SUCCESS); | ||
| 279 | + | ||
| 280 | + // opType 为 0(未注册算子 ToOpType 返回 0),在 AclOpKernelInit 入口被拦截,观察日志确认。 | ||
| 281 | + uint32_t opType1 = op::OpTypeDict::ToOpType("Axpy1"); | ||
| 282 | + rc = op::internal::gKernelMgr.GetWorkspace( | ||
| 283 | opType1, workspaceSize, workspaceNum, *ctx->GetOpArg(op::OpArgDef::OP_OPTION_ARG), | 283 | opType1, workspaceSize, workspaceNum, *ctx->GetOpArg(op::OpArgDef::OP_OPTION_ARG), |
| 284 | *ctx->GetOpArg(op::OpArgDef::OP_OUTPUT_ARG), *ctx->GetOpArg(op::OpArgDef::OP_ATTR_ARG)); | 284 | *ctx->GetOpArg(op::OpArgDef::OP_OUTPUT_ARG), *ctx->GetOpArg(op::OpArgDef::OP_ATTR_ARG)); |
| 285 | - EXPECT_NE(rc1, ACL_SUCCESS); | 285 | + EXPECT_NE(rc, ACL_SUCCESS); |
| 286 | + rc = op::internal::gKernelMgr.Run(opType1, stream, ctx); | ||
| 287 | + EXPECT_NE(rc, ACL_SUCCESS); | ||
| 286 | op::DestroyOpArgContext(ctx); | 288 | op::DestroyOpArgContext(ctx); |
| 287 | } | 289 | } |
| 288 | 290 | ||
| @@ -2123,3 +2123,30 @@ TEST_F(OpKernelUT, Initialize_MergePriority_CustomOppWinsOverVendorsAndBuiltin) | |||
| 2123 | setenv("ASCEND_OPP_PATH", mockDir.c_str(), 1); | 2123 | setenv("ASCEND_OPP_PATH", mockDir.c_str(), 1); |
| 2124 | setenv("ASCEND_CUSTOM_OPP_PATH", (mockDir + "/custom").c_str(), 1); | 2124 | setenv("ASCEND_CUSTOM_OPP_PATH", (mockDir + "/custom").c_str(), 1); |
| 2125 | } | 2125 | } |
| 2126 | + | ||
| 2127 | +// GetOpConfigJsonFileName 对越界 opType 返回空 vector,且空 vector 对下游 ParseDynamicKernelConfig 无破坏性影响 | ||
| 2128 | +TEST_F(OpKernelUT, GetOpConfigJsonFileNameOutOfBounds) | ||
| 2129 | +{ | ||
| 2130 | + // 合法 opType 返回非空(对照,证明校验未误伤合法路径) | ||
| 2131 | + uint32_t validOpType = op::OpTypeDict::ToOpType("Axpy"); | ||
| 2132 | + auto validFiles = op::GetOpConfigJsonFileName(validOpType); | ||
| 2133 | + EXPECT_FALSE(validFiles.empty()); | ||
| 2134 | + | ||
| 2135 | + // 越界 opType(>= opConfigJsonPath_.size() 但 < MAX_OP_TYPE_COUNT):校验返回空 vector | ||
| 2136 | + uint32_t invalidOpType = static_cast<uint32_t>(op::OpTypeDict::GetAllOpTypeSize()) + 10U; | ||
| 2137 | + auto invalidFiles = op::GetOpConfigJsonFileName(invalidOpType); | ||
| 2138 | + EXPECT_TRUE(invalidFiles.empty()); | ||
| 2139 | + | ||
| 2140 | + // 空 vector 传入下游 ParseDynamicKernelConfig:for 循环空转,不越界访问 [0],走失败路径返回错误 | ||
| 2141 | + auto rc = op::internal::gKernelMgr.ParseDynamicKernelConfig(invalidFiles); | ||
| 2142 | + EXPECT_NE(rc, ACL_SUCCESS); | ||
| 2143 | +} | ||
| 2144 | + | ||
| 2145 | +// InitOpFunctions 对越界 opType 返回 ACLNN_ERR_INNER,不越界访问 flag[opType] | ||
| 2146 | +TEST_F(OpKernelUT, InitOpFunctionsOutOfBounds) | ||
| 2147 | +{ | ||
| 2148 | + // 越界 opType(>= MAX_OP_TYPE_COUNT):校验返回 ACLNN_ERR_INNER,不走到 flag[opType] 越界点 | ||
| 2149 | + uint32_t invalidOpType = static_cast<uint32_t>(op::internal::MAX_OP_TYPE_COUNT) + 1U; | ||
| 2150 | + auto invalidRc = op::internal::OpRunContextMgr::InitOpFunctions(invalidOpType); | ||
| 2151 | + EXPECT_EQ(invalidRc, ACLNN_ERR_INNER); | ||
| 2152 | +} | ||