Merged
fix: validate negative indices in gather_v2 aicpu #14527
fix: validate negative indices in gather_v2 aicpu #14527
Merged
ayuemoonAdd on 5 days ago
Total: 2 files changed+66-10
@@ -93,12 +93,11 @@ uint32_t CopyGatherV2Data(const CpuKernelContext& ctx, const GatherV2ComputeInfo
93 for (int64_t i = 0; i < info.outer_size; ++i) {93 for (int64_t i = 0; i < info.outer_size; ++i) {
94 for (int64_t j = 0; j < info.batch_indices; ++j) {94 for (int64_t j = 0; j < info.batch_indices; ++j) {
95 int64_t indices_idx = batch * info.batch_indices + j;95 int64_t indices_idx = batch * info.batch_indices + j;
96- int64_t indices_value = indices_data[indices_idx] < 0 ?96+ const int64_t raw_index = static_cast<int64_t>(indices_data[indices_idx]);
97- indices_data[indices_idx] + info.gather_dim_size :97+ KERNEL_CHECK_FALSE(raw_index >= -info.gather_dim_size && raw_index < info.gather_dim_size,
98- indices_data[indices_idx];98+ KERNEL_STATUS_PARAM_INVALID, "Indices[%ld] = %ld is not in [-%ld, %ld)", indices_idx,
99- KERNEL_CHECK_FALSE(indices_value < info.gather_dim_size, KERNEL_STATUS_PARAM_INVALID,99+ raw_index, info.gather_dim_size, info.gather_dim_size);
100- "Indices[%ld] = %ld is not in [0, %ld)", indices_idx,100+ const int64_t indices_value = raw_index < 0 ? raw_index + info.gather_dim_size : raw_index;
101- static_cast<int64_t>(indices_data[indices_idx]), info.gather_dim_size);
102 auto params_idx = (i * info.gather_dim_size + indices_value +101 auto params_idx = (i * info.gather_dim_size + indices_value +
103 batch * info.outer_size * info.gather_dim_size) *102 batch * info.outer_size * info.gather_dim_size) *
104 info.inner_size;103 info.inner_size;
@@ -10,6 +10,7 @@
10 10 
11#include <complex>11#include <complex>
12#include <functional>12#include <functional>
13+#include <limits>
13#include <memory>14#include <memory>
14#include <numeric>15#include <numeric>
15#include <vector>16#include <vector>
@@ -41,8 +42,9 @@ class TEST_GATHER_V2_UT : public testing::Test {};
41 .Attr("batch_dims", batch_dims)42 .Attr("batch_dims", batch_dims)
42 43 
43template <typename T, typename Index, typename AxisType>44template <typename T, typename Index, typename AxisType>
44-void RunGatherV2Kernel(const vector<vector<int64_t>>& shapes, const vector<DataType>& data_types, const T* input_x,45+uint32_t RunGatherV2Kernel(const vector<vector<int64_t>>& shapes, const vector<DataType>& data_types, const T* input_x,
45- const Index* input_indices, const AxisType* input_axis, T* expect_output, int64_t batch_dims = 0)46+ const Index* input_indices, const AxisType* input_axis, T* expect_output,
47+ int64_t batch_dims = 0, uint32_t expect_status = KERNEL_STATUS_OK)
46{48{
47 auto calc_size = [](const vector<int64_t>& shape) -> uint64_t {49 auto calc_size = [](const vector<int64_t>& shape) -> uint64_t {
48 return shape.empty() ? 0 : accumulate(shape.begin(), shape.end(), 1LL, multiplies<int64_t>());50 return shape.empty() ? 0 : accumulate(shape.begin(), shape.end(), 1LL, multiplies<int64_t>());
@@ -70,8 +72,11 @@ void RunGatherV2Kernel(const vector<vector<int64_t>>& shapes, const vector<DataT
70 vector<void*> datas = {static_cast<void*>(x_data.get()), static_cast<void*>(indices_data.get()),72 vector<void*> datas = {static_cast<void*>(x_data.get()), static_cast<void*>(indices_data.get()),
71 static_cast<void*>(axis_data.get()), static_cast<void*>(output_data.get())};73 static_cast<void*>(axis_data.get()), static_cast<void*>(output_data.get())};
72 CREATE_NODEDEF(shapes, data_types, datas, batch_dims);74 CREATE_NODEDEF(shapes, data_types, datas, batch_dims);
73- RUN_KERNEL(node_def, HOST, KERNEL_STATUS_OK);75+ RUN_KERNEL(node_def, HOST, expect_status);
74- EXPECT_TRUE(CompareResult(output_data.get(), expect_output, output_size));76+ if (ret == KERNEL_STATUS_OK) {
77+ EXPECT_TRUE(CompareResult(output_data.get(), expect_output, output_size));
78+ }
79+ return ret;
atomgit-bot
atomgit-botatomgit-bot5 days ago

🟡 Medium Priority

变更点:test_gather_v2.cpp 第 75-79 行改为 RUN_KERNEL(node_def, HOST, expect_status); 后直接使用 ret(if (ret == KERNEL_STATUS_OK) 与 return ret;),但本文件从未声明 ret,它只能来自 utils/aicpu_test_utils.h 中 RUN_KERNEL 宏的内部实现。

证据链与影响:

  1. RUN_KERNEL、CompareResult、CpuKernelUtils::RunKernel 均不在本仓库内(在 index 目录下搜索 #define RUN_KERNEL、bool CompareResult 均无结果,该头文件来自已安装的 CANN aicpu 测试框架)。
  2. 因此“宏是否把状态变量暴露到调用者作用域、变量名是否恰为 ret”无法从仓库代码确认;若该宏以 do { ... } while (0) 或裸 { } 块包裹并在内部声明 uint32_t ret,则第 76 行与第 79 行会因 ret 未声明而编译失败;若宏内部变量名为 ret 且未加块作用域,才可编译。
  3. 该 PR 描述中明确说明作者未能实际编译(验证环境缺 Python/GCC/CMake,构建停在依赖检查阶段),pre-commit 的 clang-format/codespell 等检查也不会发现此类问题,因此“测试文件编译不过”这一失败模式目前无任何验证覆盖。

失败模式:bash build.sh --opkernel_aicpu_test --ops=gather_v2 编译 test_gather_v2.cpp 时报 'ret' was not declared in this scope,导致整个 gather_v2 AICPU UT 目标构建失败(属构建级阻断)。触发条件就是该 UT 目标被编译,无额外前置条件。

likedislike
Incorrect?
75}80}
76 81 
77TEST_F(TEST_GATHER_V2_UT, DATA_TYPE_DT_COMPLEX128_INT64_SUCC)82TEST_F(TEST_GATHER_V2_UT, DATA_TYPE_DT_COMPLEX128_INT64_SUCC)
@@ -108,6 +113,19 @@ TEST_F(TEST_GATHER_V2_UT, DATA_TYPE_DT_INT32_NEGATIVE_INDEX_SUCC)
108 RunGatherV2Kernel(shapes, data_types, input_x, input_indices, input_axis, expect_output);113 RunGatherV2Kernel(shapes, data_types, input_x, input_indices, input_axis, expect_output);
109}114}
110 115 
116+TEST_F(TEST_GATHER_V2_UT, DATA_TYPE_DT_COMPLEX64_NEGATIVE_INDEX_BOUNDARY_SUCC)
117+{
118+ vector<DataType> data_types = {DT_COMPLEX64, DT_INT64, DT_INT64, DT_COMPLEX64};
119+ vector<vector<int64_t>> shapes = {{10}, {3}, {1}, {3}};
120+ complex<float> input_x[10] = {{0, 10}, {1, 11}, {2, 12}, {3, 13}, {4, 14},
121+ {5, 15}, {6, 16}, {7, 17}, {8, 18}, {9, 19}};
122+ int64_t input_indices[3] = {-10, -1, 9};
123+ int64_t input_axis[1] = {0};
124+ complex<float> expect_output[3] = {{0, 10}, {9, 19}, {9, 19}};
125+ EXPECT_EQ(RunGatherV2Kernel(shapes, data_types, input_x, input_indices, input_axis, expect_output),
126+ KERNEL_STATUS_OK);
127+}
128+ 
111TEST_F(TEST_GATHER_V2_UT, FAILED_INDICES_TYPE)129TEST_F(TEST_GATHER_V2_UT, FAILED_INDICES_TYPE)
112{130{
113 vector<DataType> data_types = {DT_DOUBLE, DT_DOUBLE, DT_INT64, DT_DOUBLE};131 vector<DataType> data_types = {DT_DOUBLE, DT_DOUBLE, DT_INT64, DT_DOUBLE};
@@ -135,3 +153,42 @@ TEST_F(TEST_GATHER_V2_UT, FAILED_OUT_OF_BOUND_INDEX)
135 CREATE_NODEDEF(shapes, data_types, datas, 0);153 CREATE_NODEDEF(shapes, data_types, datas, 0);
136 RUN_KERNEL(node_def, HOST, KERNEL_STATUS_PARAM_INVALID);154 RUN_KERNEL(node_def, HOST, KERNEL_STATUS_PARAM_INVALID);
137}155}
156+ 
157+TEST_F(TEST_GATHER_V2_UT, FAILED_NEGATIVE_INDEX_OUT_OF_BOUND_DOUBLE)
158+{
159+ vector<DataType> data_types = {DT_DOUBLE, DT_INT64, DT_INT64, DT_DOUBLE};
160+ vector<vector<int64_t>> shapes = {{10}, {1}, {1}, {1}};
161+ double input_x[10] = {0};
162+ int64_t input_indices[1] = {-11};
163+ int64_t input_axis[1] = {0};
164+ double output[1] = {0};
165+ EXPECT_EQ(RunGatherV2Kernel(shapes, data_types, input_x, input_indices, input_axis, output, 0,
166+ KERNEL_STATUS_PARAM_INVALID),
167+ KERNEL_STATUS_PARAM_INVALID);
168+}
169+ 
170+TEST_F(TEST_GATHER_V2_UT, FAILED_NEGATIVE_INDEX_OUT_OF_BOUND_COMPLEX64)
171+{
172+ vector<DataType> data_types = {DT_COMPLEX64, DT_INT64, DT_INT64, DT_COMPLEX64};
173+ vector<vector<int64_t>> shapes = {{10}, {1}, {1}, {1}};
174+ complex<float> input_x[10] = {{0, 0}};
175+ int64_t input_indices[1] = {-100};
176+ int64_t input_axis[1] = {0};
177+ complex<float> output[1] = {{0, 0}};
178+ EXPECT_EQ(RunGatherV2Kernel(shapes, data_types, input_x, input_indices, input_axis, output, 0,
179+ KERNEL_STATUS_PARAM_INVALID),
180+ KERNEL_STATUS_PARAM_INVALID);
181+}
182+ 
183+TEST_F(TEST_GATHER_V2_UT, FAILED_NEGATIVE_INDEX_OUT_OF_BOUND_COMPLEX128)
184+{
185+ vector<DataType> data_types = {DT_COMPLEX128, DT_INT64, DT_INT64, DT_COMPLEX128};
186+ vector<vector<int64_t>> shapes = {{10}, {1}, {1}, {1}};
187+ complex<double> input_x[10] = {{0, 0}};
188+ int64_t input_indices[1] = {numeric_limits<int64_t>::min()};
189+ int64_t input_axis[1] = {0};
190+ complex<double> output[1] = {{0, 0}};
191+ EXPECT_EQ(RunGatherV2Kernel(shapes, data_types, input_x, input_indices, input_axis, output, 0,
192+ KERNEL_STATUS_PARAM_INVALID),
193+ KERNEL_STATUS_PARAM_INVALID);
194+}