Skip to content

Commit aa1f963

Browse files
author
Github Executorch
committed
Arm backend: Fix integer overflow in VGFBackend IO size computation
Replace std::accumulate with std::multiplies<>() with an explicit loop using c10::mul_overflows() to detect overflow before each multiplication. The previous code would silently wrap on overflow, producing an undersized memcpy size that could lead to out-of-bounds reads/writes when copying tensor data to/from Vulkan device memory. Also reject negative dimensions before casting to size_t. Addresses TOB-EXECUTORCH-27. This PR was authored with the assistance of Claude.
1 parent 5e8a0df commit aa1f963

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

backends/arm/runtime/VGFBackend.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
#include <cinttypes>
89
#include <list>
910
#include <numeric>
1011
using namespace std;
1112

13+
#include <c10/util/safe_numerics.h>
1214
#include <executorch/runtime/backend/interface.h>
1315
#include <executorch/runtime/core/error.h>
1416
#include <executorch/runtime/core/evalue.h>
@@ -190,8 +192,18 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
190192
if (!io->is_input)
191193
continue;
192194

193-
size_t io_size = accumulate(
194-
io->size.begin(), io->size.end(), io->elt_size, std::multiplies<>());
195+
size_t io_size = io->elt_size;
196+
for (int64_t dim : io->size) {
197+
ET_CHECK_OR_RETURN_ERROR(
198+
dim >= 0,
199+
InvalidArgument,
200+
"Negative dimension in IO size: %" PRId64,
201+
dim);
202+
ET_CHECK_OR_RETURN_ERROR(
203+
!c10::mul_overflows(io_size, static_cast<size_t>(dim), &io_size),
204+
InvalidArgument,
205+
"Overflow computing IO buffer size");
206+
}
195207

196208
void* data;
197209
if (!repr->map_io(io, &data)) {
@@ -225,8 +237,18 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
225237
if (io->is_input)
226238
continue;
227239

228-
size_t io_size = accumulate(
229-
io->size.begin(), io->size.end(), io->elt_size, std::multiplies<>());
240+
size_t io_size = io->elt_size;
241+
for (int64_t dim : io->size) {
242+
ET_CHECK_OR_RETURN_ERROR(
243+
dim >= 0,
244+
InvalidArgument,
245+
"Negative dimension in IO size: %" PRId64,
246+
dim);
247+
ET_CHECK_OR_RETURN_ERROR(
248+
!c10::mul_overflows(io_size, static_cast<size_t>(dim), &io_size),
249+
InvalidArgument,
250+
"Overflow computing IO buffer size");
251+
}
230252

231253
void* data;
232254
if (!repr->map_io(io, &data)) {

0 commit comments

Comments
 (0)