Skip to content

Commit 51b06fd

Browse files
lucylqGithub Executorch
andauthored
Examples: Fix integer overflows in size computations (pytorch#19467)
Replace std::accumulate / std::reduce with std::multiplies<>() and std::accumulate sums with explicit loops using c10::mul_overflows() and c10::add_overflows() to detect overflow before each operation. The previous code would silently wrap on overflow, producing undersized buffer sizes that could lead to out-of-bounds access. Touches: - examples/mediatek/.../LlamaModelChunk.cpp: GetCacheNumRows and GetCacheStrideSize (multiplicative reductions over cache shape) - examples/models/llama/runner/static_attention_io_manager.h: StaticKVCache constructor cache_data_size_/update_data_size_ (sum and chained multiplies) - examples/qualcomm/executor_runner/qnn_executor_runner.cpp: output nbytes computation from dynamic shape file These functions cannot return Error, so abort via ET_CHECK_MSG when overflow is detected. As examples they also serve as templates for third-party code, so getting the pattern right matters. This PR was authored with the assistance of Claude. Co-authored-by: Github Executorch <github_executorch@arm.com>
1 parent 0782745 commit 51b06fd

3 files changed

Lines changed: 61 additions & 20 deletions

File tree

‎examples/mediatek/executor_runner/llama_runner/LlamaModelChunk.cpp‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <unordered_map>
1212
#include <vector>
1313

14+
#include <c10/util/safe_numerics.h>
1415
#include <executorch/extension/data_loader/file_data_loader.h>
1516
#include <executorch/extension/evalue_util/print_evalue.h>
1617
#include <executorch/runtime/executor/method.h>
@@ -445,19 +446,35 @@ void LlamaModelChunk::PrepareCacheIOs() {
445446
}
446447

447448
size_t LlamaModelChunk::GetCacheNumRows() const {
448-
return std::reduce(
449-
mCacheShape.begin(),
450-
mCacheShape.begin() + kCacheLengthDim,
451-
1,
452-
std::multiplies<>());
449+
size_t result = 1;
450+
for (auto it = mCacheShape.begin();
451+
it != mCacheShape.begin() + kCacheLengthDim;
452+
++it) {
453+
ET_CHECK_MSG(
454+
*it >= 0,
455+
"Negative dimension %d in cache shape",
456+
static_cast<int>(*it));
457+
ET_CHECK_MSG(
458+
!c10::mul_overflows(result, static_cast<size_t>(*it), &result),
459+
"Overflow computing cache row count");
460+
}
461+
return result;
453462
}
454463

455464
size_t LlamaModelChunk::GetCacheStrideSize() const {
456-
return std::reduce(
457-
mCacheShape.begin() + kCacheLengthDim + 1,
458-
mCacheShape.end(),
459-
kCacheTypeSize,
460-
std::multiplies<>());
465+
size_t result = kCacheTypeSize;
466+
for (auto it = mCacheShape.begin() + kCacheLengthDim + 1;
467+
it != mCacheShape.end();
468+
++it) {
469+
ET_CHECK_MSG(
470+
*it >= 0,
471+
"Negative dimension %d in cache shape",
472+
static_cast<int>(*it));
473+
ET_CHECK_MSG(
474+
!c10::mul_overflows(result, static_cast<size_t>(*it), &result),
475+
"Overflow computing cache stride size");
476+
}
477+
return result;
461478
}
462479

463480
void LlamaModelChunk::InitMaskBuilder() {

‎examples/models/llama/runner/static_attention_io_manager.h‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <unordered_map>
1515
#include <vector>
1616

17+
#include <c10/util/safe_numerics.h>
1718
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
1819
#include <executorch/runtime/core/span.h>
1920
#include <executorch/runtime/executor/method.h>
@@ -54,11 +55,25 @@ class StaticKVCache {
5455
style_(style),
5556
input_ptrs_(n_caches_),
5657
output_ptrs_(n_caches_) {
57-
size_t total_cache_len = std::accumulate(
58-
cache_lengths_.begin(), cache_lengths_.end(), size_t(0));
59-
cache_data_size_ = total_cache_len * n_heads_per_cache_ * head_dim_;
60-
update_data_size_ =
61-
n_caches_ * n_heads_per_cache_ * max_input_len_ * head_dim_;
58+
size_t total_cache_len = 0;
59+
for (size_t cache_len : cache_lengths_) {
60+
ET_CHECK_MSG(
61+
!c10::add_overflows(total_cache_len, cache_len, &total_cache_len),
62+
"Overflow summing cache lengths");
63+
}
64+
ET_CHECK_MSG(
65+
!c10::mul_overflows(
66+
total_cache_len, n_heads_per_cache_, &cache_data_size_) &&
67+
!c10::mul_overflows(cache_data_size_, head_dim_, &cache_data_size_),
68+
"Overflow computing cache_data_size_");
69+
ET_CHECK_MSG(
70+
!c10::mul_overflows(
71+
n_caches_, n_heads_per_cache_, &update_data_size_) &&
72+
!c10::mul_overflows(
73+
update_data_size_, max_input_len_, &update_data_size_) &&
74+
!c10::mul_overflows(
75+
update_data_size_, head_dim_, &update_data_size_),
76+
"Overflow computing update_data_size_");
6277

6378
cache_data_ = allocator_.allocate(cache_data_size_);
6479
update_data_ = allocator_.allocate(update_data_size_);

‎examples/qualcomm/executor_runner/qnn_executor_runner.cpp‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <executorch/runtime/platform/log.h>
3030
#include <executorch/runtime/platform/runtime.h>
3131

32+
#include <c10/util/safe_numerics.h>
3233
#include <gflags/gflags.h>
3334

3435
#include <chrono>
@@ -613,11 +614,19 @@ int main(int argc, char** argv) {
613614
auto output_tensor = outputs[output_index].toTensor();
614615
size_t nbytes = output_tensor.nbytes();
615616
if (!expected_output_shapes.empty()) {
616-
nbytes = std::accumulate(
617-
expected_output_shapes[output_index].begin(),
618-
expected_output_shapes[output_index].end(),
619-
executorch::runtime::elementSize(output_tensor.scalar_type()),
620-
std::multiplies<int>());
617+
size_t computed = static_cast<size_t>(
618+
executorch::runtime::elementSize(output_tensor.scalar_type()));
619+
for (int32_t dim : expected_output_shapes[output_index]) {
620+
ET_CHECK_MSG(
621+
dim >= 0,
622+
"Negative dimension %d in expected output shape",
623+
dim);
624+
ET_CHECK_MSG(
625+
!c10::mul_overflows(
626+
computed, static_cast<size_t>(dim), &computed),
627+
"Overflow computing expected output nbytes");
628+
}
629+
nbytes = computed;
621630
}
622631
auto output_file_name = FLAGS_output_folder_path + "/output_" +
623632
std::to_string(inference_index) + "_" +

0 commit comments

Comments
 (0)