Skip to content

Commit 73a9d6e

Browse files
lucylqGithub Executorch
andauthored
Qualcomm overflow fixes (pytorch#19469)
Replace std::accumulate with std::multiplies<>() with an explicit loop using c10::mul_overflows() to detect overflow before each multiplication. The previous code accumulated into a uint32_t, which would silently wrap on overflow and cause CreateTensorWrapper to allocate or report an undersized buffer for the tensor data. CreateTensorWrapper has no error return, so abort via ET_CHECK_MSG when overflow is detected. This is AOT-only host code. This PR was authored with the assistance of Claude. Co-authored-by: Github Executorch <github_executorch@arm.com>
1 parent ce08814 commit 73a9d6e

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

‎backends/qualcomm/aot/wrappers/TensorWrapper.cpp‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#include <executorch/backends/qualcomm/aot/wrappers/TensorWrapper.h>
1010

11+
#include <c10/util/safe_numerics.h>
12+
#include <executorch/runtime/platform/assert.h>
13+
1114
#include <atomic>
1215
#include <cstring>
1316
#include <limits>
@@ -181,8 +184,14 @@ std::shared_ptr<TensorWrapper> CreateTensorWrapper(
181184
const void* data,
182185
bool copy_data) {
183186
if (bytes == 0) {
184-
bytes = std::accumulate(
185-
dims, dims + rank, GetDataTypeSize(data_type), std::multiplies<>());
187+
std::uint32_t computed_bytes = GetDataTypeSize(data_type);
188+
for (std::uint32_t i = 0; i < rank; ++i) {
189+
ET_CHECK_MSG(
190+
!c10::mul_overflows(computed_bytes, dims[i], &computed_bytes),
191+
"Overflow computing tensor byte size for tensor of rank %u",
192+
rank);
193+
}
194+
bytes = computed_bytes;
186195
}
187196
return std::make_shared<TensorWrapper>(
188197
tensor_name,

0 commit comments

Comments
 (0)