Skip to content

Commit 067d98d

Browse files
fadara01Sqvid
andcommitted
backport: cpu: aarch64: fix potential overflow in ACL strides
Co-authored-by: Siddhartha Menon <[email protected]> Signed-off-by: Fadi Arafeh <[email protected]>
1 parent 3dc6a7d commit 067d98d

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

src/cpu/aarch64/acl_convolution_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ status_t acl_init_conf(acl_conv_conf_t &acp, memory_desc_t &src_md,
278278
return status::unimplemented;
279279
}
280280

281-
acl_utils::reorder_to_weight_format(acp.wei_tensor_info, weights_md,
282-
expected_weight_format, I_dim, O_dim, {W_dim, H_dim}, {});
281+
CHECK(acl_utils::reorder_to_weight_format(acp.wei_tensor_info, weights_md,
282+
expected_weight_format, I_dim, O_dim, {W_dim, H_dim}, {}));
283283

284284
return status::success;
285285
}

src/cpu/aarch64/acl_inner_product.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ status_t acl_inner_product_fwd_t::pd_t::init_conf_ip(
264264
}
265265

266266
const memory_desc_t weights_md_received = weights_md_;
267-
acl_utils::reorder_to_weight_format(aip_.wei_tensor_info, weights_md_,
268-
expected_weight_format, inner_dim, o_dim, remaining_dims, {});
267+
CHECK(acl_utils::reorder_to_weight_format(aip_.wei_tensor_info, weights_md_,
268+
expected_weight_format, inner_dim, o_dim, remaining_dims, {}));
269269

270270
ACL_CHECK_SUPPORT((weights_format_kind_received == format_kind::blocked)
271271
&& !(dnnl_memory_desc_equal(

src/cpu/aarch64/acl_utils.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*******************************************************************************/
1616

1717
#include "cpu/aarch64/acl_utils.hpp"
18+
#include <limits>
1819

1920
namespace dnnl {
2021
namespace impl {
@@ -26,6 +27,17 @@ namespace acl_utils {
2627
using namespace dnnl::impl::alg_kind;
2728
using namespace data_type;
2829

30+
status_t safe_set_strides(arm_compute::Strides &strides, size_t dim, size_t val,
31+
bool inc_dim = true) {
32+
// ACL stride value is uint32, check for overflow
33+
if (val > std::numeric_limits<uint32_t>::max()) {
34+
return status::unimplemented;
35+
}
36+
37+
strides.set(dim, val, inc_dim);
38+
return status::success;
39+
}
40+
2941
arm_compute::DataType get_acl_data_t(
3042
const dnnl_data_type_t dt, const bool is_quantized) {
3143
switch (dt) {
@@ -154,8 +166,8 @@ status_t tensor_info(
154166
for (int i = md.ndims() - 1; i >= 0; --i) {
155167
// ACL strides are in bytes, oneDNN strides are in numbers of elements,
156168
// multiply by data type size to convert
157-
strides_in_bytes.set(
158-
acl_stride_i, blocking_desc.strides[i] * md.data_type_size());
169+
CHECK(safe_set_strides(strides_in_bytes, acl_stride_i,
170+
blocking_desc.strides[i] * md.data_type_size()));
159171
++acl_stride_i;
160172
}
161173

@@ -190,10 +202,12 @@ status_t insert_singleton_dimension(arm_compute::TensorInfo &ti, size_t dim_i) {
190202
arm_compute::Strides strides;
191203
for (size_t old_i = 0, new_i = 0; old_i < ti.num_dimensions(); ++old_i) {
192204
if (old_i == dim_i) {
193-
strides.set(new_i, ti.strides_in_bytes()[old_i], false);
205+
CHECK(safe_set_strides(
206+
strides, new_i, ti.strides_in_bytes()[old_i], false));
194207
++new_i;
195208
}
196-
strides.set(new_i, ti.strides_in_bytes()[old_i], false);
209+
CHECK(safe_set_strides(
210+
strides, new_i, ti.strides_in_bytes()[old_i], false));
197211
++new_i;
198212
}
199213

@@ -274,9 +288,9 @@ int reorder_dimensions_by_stride(std::vector<memory_desc_t *> permuted_mds,
274288
return reordered_dims;
275289
}
276290

277-
void reorder_to_weight_format(arm_compute::TensorInfo &info, memory_desc_t &md,
278-
arm_compute::WeightFormat wf, dim_t I_dim, dim_t O_dim,
279-
const std::vector<dim_t> &spatial_dims,
291+
status_t reorder_to_weight_format(arm_compute::TensorInfo &info,
292+
memory_desc_t &md, arm_compute::WeightFormat wf, dim_t I_dim,
293+
dim_t O_dim, const std::vector<dim_t> &spatial_dims,
280294
const std::vector<dim_t> &batch_dims) {
281295

282296
md.format_kind = format_kind::blocked;
@@ -336,12 +350,14 @@ void reorder_to_weight_format(arm_compute::TensorInfo &info, memory_desc_t &md,
336350
// the interleaving). Note that we use the innermost_batch_stride
337351
// because all the batched dimensions are collapsed (as required by ACL).
338352
arm_compute::Strides new_strides_in_bytes = info.strides_in_bytes();
339-
new_strides_in_bytes.set(1, ldb * info.element_size());
340-
new_strides_in_bytes.set(2, innermost_batch_stride * info.element_size());
353+
CHECK(safe_set_strides(new_strides_in_bytes, 1, ldb * info.element_size()));
354+
CHECK(safe_set_strides(new_strides_in_bytes, 2,
355+
innermost_batch_stride * info.element_size()));
341356

342357
info.init(info.tensor_shape(), info.num_channels(), info.data_type(),
343358
new_strides_in_bytes, info.offset_first_element_in_bytes(),
344359
memory_desc_wrapper(md).size());
360+
return status::success;
345361
}
346362

347363
} // namespace acl_utils

src/cpu/aarch64/acl_utils.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ int reorder_dimensions_by_stride(std::vector<memory_desc_t *> permuted_mds,
9292
// matmul, ordered from innermost to outermost. ACL calls these
9393
// the multi_stride_b. These will become the outermost (least dense)
9494
// dimensions and will be collapsed.
95-
void reorder_to_weight_format(arm_compute::TensorInfo &info, memory_desc_t &md,
96-
arm_compute::WeightFormat wf, dim_t I_dim, dim_t O_dim,
97-
const std::vector<dim_t> &spatial_dims,
95+
status_t reorder_to_weight_format(arm_compute::TensorInfo &info,
96+
memory_desc_t &md, arm_compute::WeightFormat wf, dim_t I_dim,
97+
dim_t O_dim, const std::vector<dim_t> &spatial_dims,
9898
const std::vector<dim_t> &batch_dims = {});
9999

100100
// Logs a custom 'info' line describing an unsupported case

src/cpu/aarch64/matmul/acl_matmul_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ status_t init_conf_matmul(acl_matmul_conf_t &amp, memory_desc_t &src_md,
215215
for (dim_t i = K_dim - 1; i >= 0; --i)
216216
batch_dims.push_back(i);
217217

218-
acl_utils::reorder_to_weight_format(amp.wei_tensor_info, wei_md,
219-
expected_weight_format, K_dim, N_dim, {}, batch_dims);
218+
CHECK(acl_utils::reorder_to_weight_format(amp.wei_tensor_info, wei_md,
219+
expected_weight_format, K_dim, N_dim, {}, batch_dims));
220220
}
221221

222222
return status::success;

0 commit comments

Comments
 (0)