-
Notifications
You must be signed in to change notification settings - Fork 954
Fix overflows in et #19057
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
base: main
Are you sure you want to change the base?
Fix overflows in et #19057
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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| // A llama 3.2 runner that includes preprocessing and post processing | ||
| // logic. The module takes in a string as input and emits a string as output. | ||
|
|
||
| #include <c10/util/safe_numerics.h> | ||
| #include <executorch/examples/models/llama/runner/runner.h> | ||
| #include <executorch/examples/models/llama/tokenizer/llama_tiktoken.h> | ||
| #include <executorch/examples/qualcomm/oss_scripts/llama/runner/client_mem.h> | ||
|
|
@@ -433,8 +434,11 @@ Error Runner<T>::generate_from_prompt_or_file( | |
| } | ||
| int num_prompt_tokens = prompt_tokens.size(); | ||
| ET_CHECK_MSG(num_prompt_tokens >= 1, "Expected at least 1 prompt token"); | ||
| int64_t end_pos = 0; | ||
| ET_CHECK_MSG( | ||
| cur_pos_ + num_prompt_tokens < seq_len, | ||
| !c10::add_overflows( | ||
| cur_pos_, static_cast<int64_t>(num_prompt_tokens), &end_pos) && | ||
| end_pos < static_cast<int64_t>(seq_len), | ||
|
Comment on lines
+437
to
+441
|
||
| "sequence length exceeded - please increase the seq_len value"); | ||
|
|
||
| // Prompt Processor first | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
|
|
||
| #include <executorch/extension/flat_tensor/flat_tensor_data_map.h> | ||
|
|
||
| #include <c10/util/safe_numerics.h> | ||
|
|
||
| #include <executorch/extension/flat_tensor/serialize/flat_tensor_generated.h> | ||
| #include <executorch/extension/flat_tensor/serialize/flat_tensor_header.h> | ||
|
|
||
|
|
@@ -73,9 +75,13 @@ Result<const flat_tensor_flatbuffer::NamedData*> get_named_data( | |
| key.data(), | ||
| segments->size()); | ||
| // Validate the segment. | ||
| uint64_t seg_end = 0; | ||
| ET_CHECK_OR_RETURN_ERROR( | ||
| (segments->Get(segment_index)->offset() + | ||
| segments->Get(segment_index)->size()) <= segment_end_offset, | ||
| !c10::add_overflows( | ||
| static_cast<uint64_t>(segments->Get(segment_index)->offset()), | ||
| static_cast<uint64_t>(segments->Get(segment_index)->size()), | ||
| &seg_end) && | ||
| seg_end <= static_cast<uint64_t>(segment_end_offset), | ||
| InvalidExternalData, | ||
|
Comment on lines
+78
to
85
|
||
| "Invalid segment offset %" PRIu64 | ||
| " is larger than the segment_base_offset + segment_data_size %" PRIu64 | ||
|
|
||
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.
This uses c10::add_overflows with signed int64_t operands. In this repo's MSVC fallback implementation, add_overflows computes
a + bfor signed types, which can trigger signed-overflow UB before detection. To keep this check safe cross-platform, prefer unsigned arithmetic (after asserting start_pos >= 0) or rewrite as a non-overflowing bounds check using wider/unsigned types.