Skip to content

Commit 84f39aa

Browse files
lucylqGithub Executorch
andauthored
Arm backend: Fix integer overflow in VGFBackend IO size computation (pytorch#19256)
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. This PR was authored with the assistance of Claude. cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell Co-authored-by: Github Executorch <github_executorch@arm.com>
1 parent beca948 commit 84f39aa

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

backends/arm/runtime/VGFBackend.cpp

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

8-
#include <list>
9-
#include <numeric>
8+
#include <cinttypes>
109
using namespace std;
1110

11+
#include <c10/util/safe_numerics.h>
1212
#include <executorch/runtime/backend/interface.h>
1313
#include <executorch/runtime/core/error.h>
1414
#include <executorch/runtime/core/evalue.h>
@@ -191,8 +191,18 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
191191
if (!io->is_input)
192192
continue;
193193

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

197207
void* data;
198208
if (!repr->map_io(io, &data)) {
@@ -226,8 +236,18 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
226236
if (io->is_input)
227237
continue;
228238

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

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

0 commit comments

Comments
 (0)