-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix integer overflows in tensor byte-size computations #19055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ | |
|
|
||
| #include <random> | ||
|
|
||
| #include <c10/util/safe_numerics.h> | ||
|
|
||
| namespace executorch { | ||
| namespace extension { | ||
| namespace { | ||
|
|
@@ -111,9 +113,17 @@ TensorPtr empty_strided( | |
| std::vector<executorch::aten::StridesType> strides, | ||
| executorch::aten::ScalarType type, | ||
| executorch::aten::TensorShapeDynamism dynamism) { | ||
| std::vector<uint8_t> data( | ||
| executorch::aten::compute_numel(sizes.data(), sizes.size()) * | ||
| executorch::aten::elementSize(type)); | ||
| const auto numel = static_cast<size_t>( | ||
| executorch::aten::compute_numel(sizes.data(), sizes.size())); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to use safe_numel here. |
||
| const auto elem_size = | ||
| static_cast<size_t>(executorch::aten::elementSize(type)); | ||
| size_t nbytes = 0; | ||
| ET_CHECK_MSG( | ||
| !c10::mul_overflows(numel, elem_size, &nbytes), | ||
| "empty_strided size overflow: numel %zu * element size %zu", | ||
| numel, | ||
| elem_size); | ||
| std::vector<uint8_t> data(nbytes); | ||
| return make_tensor_ptr( | ||
| std::move(sizes), | ||
| std::move(data), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| */ | ||
|
|
||
| #include <c10/util/irange.h> | ||
| #include <c10/util/safe_numerics.h> | ||
| #include <executorch/runtime/core/exec_aten/exec_aten.h> | ||
| #include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> | ||
| #include <executorch/runtime/core/span.h> | ||
|
|
@@ -19,15 +20,25 @@ namespace { | |
| Result<size_t> calculate_nbytes( | ||
| const Span<const int32_t>& sizes, | ||
| const executorch::aten::ScalarType& scalar_type) { | ||
| ssize_t n = 1; | ||
| size_t n = 1; | ||
| for (const auto i : c10::irange(sizes.size())) { | ||
| if (sizes[i] < 0) { | ||
| return Error::InvalidArgument; | ||
| } | ||
| n *= sizes[i]; | ||
| size_t next = 0; | ||
| if (c10::mul_overflows(n, static_cast<size_t>(sizes[i]), &next)) { | ||
| return Error::InvalidArgument; | ||
| } | ||
| n = next; | ||
|
Comment on lines
+28
to
+32
|
||
| } | ||
| // Use the full namespace to disambiguate from c10::elementSize. | ||
| return n * executorch::runtime::elementSize(scalar_type); | ||
| const size_t elem_size = | ||
| static_cast<size_t>(executorch::runtime::elementSize(scalar_type)); | ||
| size_t total = 0; | ||
| if (c10::mul_overflows(n, elem_size, &total)) { | ||
| return Error::InvalidArgument; | ||
| } | ||
| return total; | ||
| } | ||
| } // namespace | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
empty_strided()still relies onexecutorch::aten::compute_numel()(which returnsssize_tand, in executor mode, multiplies sizes using signed arithmetic without overflow checks). If the size product overflows insidecompute_numel, that's undefined behavior and may yield a wrappednumelbefore you even reach themul_overflows(numel, elem_size, ...)guard.Consider computing
numellocally usingsize_t+c10::mul_overflowsover thesizesvector (and validating non-negative sizes) so the overflow is caught deterministically before any UB occurs, then multiply byelem_sizeas you do now.