Skip to content

Commit 043baf7

Browse files
author
Github Executorch
committed
et check
1 parent cdabb14 commit 043baf7

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

runtime/core/portable_type/tensor_impl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cstdint>
1414

1515
#include <c10/util/irange.h>
16+
#include <c10/util/safe_numerics.h>
1617

1718
#include <executorch/runtime/core/exec_aten/util/dim_order_util.h>
1819
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
@@ -39,13 +40,14 @@ ssize_t compute_numel(const TensorImpl::SizesType* sizes, ssize_t dim) {
3940
"Size must be non-negative, got %zd at dimension %zd",
4041
static_cast<ssize_t>(sizes[i]),
4142
i);
43+
ssize_t next_numel;
4244
ET_CHECK_MSG(
43-
sizes[i] == 0 || numel <= SSIZE_MAX / sizes[i],
45+
!c10::mul_overflows(numel, static_cast<ssize_t>(sizes[i]), &next_numel),
4446
"Overflow computing numel: %zd * %zd would overflow ssize_t at dimension %zd",
4547
numel,
4648
static_cast<ssize_t>(sizes[i]),
4749
i);
48-
numel *= sizes[i];
50+
numel = next_numel;
4951
}
5052
return numel;
5153
}

runtime/executor/tensor_parser_portable.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#include <executorch/runtime/executor/tensor_parser.h>
1010

11+
#include <climits>
12+
13+
#include <c10/util/safe_numerics.h>
14+
1115
#include <executorch/runtime/core/exec_aten/exec_aten.h>
1216
#include <executorch/runtime/core/exec_aten/util/dim_order_util.h>
1317
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
@@ -118,17 +122,25 @@ Result<Tensor> parseTensor(
118122
dim_order =
119123
const_cast<executorch::aten::DimOrderType*>(serialized_dim_order);
120124
}
121-
// Validate sizes before using them in case the PTE data is bad. We can't
122-
// detect bad positive values, but we can reject negative values, which would
123-
// otherwise panic in the TensorImpl ctor. dim_order_to_stride() will validate
124-
// dim_order.
125+
// Validate sizes before using them in case the PTE data is bad. Reject
126+
// negative values and check that the product of all dimensions doesn't
127+
// overflow ssize_t, which would otherwise abort in the TensorImpl ctor.
128+
// dim_order_to_stride() will validate dim_order.
129+
ssize_t numel = 1;
125130
for (flatbuffers::uoffset_t i = 0; i < dim; i++) {
126131
ET_CHECK_OR_RETURN_ERROR(
127132
sizes[i] >= 0,
128133
InvalidProgram,
129134
"Negative size[%zu] %" PRId32,
130135
static_cast<size_t>(i),
131136
sizes[i]);
137+
ssize_t next_numel;
138+
ET_CHECK_OR_RETURN_ERROR(
139+
!c10::mul_overflows(numel, static_cast<ssize_t>(sizes[i]), &next_numel),
140+
InvalidProgram,
141+
"Overflow computing numel at dim %zu",
142+
static_cast<size_t>(i));
143+
numel = next_numel;
132144
}
133145

134146
// We will remove strides from schema.

0 commit comments

Comments
 (0)