-
Notifications
You must be signed in to change notification settings - Fork 931
Add bounds checking on buffer_idx for constant_buffer and constant_data in XNNPACK #18820
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?
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -173,7 +173,7 @@ Obtaining the constant data pointer can either be from within the flatbuffer | |||||||||||||||||||
| payload (deprecated) or via offsets to the constant_data_ptr. If no constant | ||||||||||||||||||||
| data associated with the tensor value, then returns nullptr. | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| const uint8_t* getConstantDataPtr( | ||||||||||||||||||||
| Result<const uint8_t*> getConstantDataPtr( | ||||||||||||||||||||
| uint32_t buffer_idx, | ||||||||||||||||||||
| GraphPtr flatbuffer_graph, | ||||||||||||||||||||
| const uint8_t* constant_data_ptr, | ||||||||||||||||||||
|
|
@@ -184,13 +184,39 @@ const uint8_t* getConstantDataPtr( | |||||||||||||||||||
| if (!constant_data_ptr) { | ||||||||||||||||||||
| // TODO(T172265611): Remove constant_buffer in flatbuffer path after BC | ||||||||||||||||||||
| // window | ||||||||||||||||||||
| const auto& constant_buffer = *flatbuffer_graph->constant_buffer(); | ||||||||||||||||||||
| return constant_buffer[buffer_idx]->storage()->data(); | ||||||||||||||||||||
| auto* cb = flatbuffer_graph->constant_buffer(); | ||||||||||||||||||||
| ET_CHECK_OR_RETURN_ERROR( | ||||||||||||||||||||
| cb != nullptr, InvalidProgram, "constant_buffer is null"); | ||||||||||||||||||||
| ET_CHECK_OR_RETURN_ERROR( | ||||||||||||||||||||
| buffer_idx < cb->size(), | ||||||||||||||||||||
| InvalidProgram, | ||||||||||||||||||||
| "buffer_idx %u out of bounds for constant_buffer of size %u", | ||||||||||||||||||||
| buffer_idx, | ||||||||||||||||||||
| cb->size()); | ||||||||||||||||||||
| auto* buffer_entry = (*cb)[buffer_idx]; | ||||||||||||||||||||
| ET_CHECK_OR_RETURN_ERROR( | ||||||||||||||||||||
| buffer_entry != nullptr && buffer_entry->storage() != nullptr, | ||||||||||||||||||||
| InvalidProgram, | ||||||||||||||||||||
| "Null constant_buffer entry at buffer_idx %u", | ||||||||||||||||||||
| buffer_idx); | ||||||||||||||||||||
| return buffer_entry->storage()->data(); | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| ConstantDataOffsetPtr constant_data_offset = | ||||||||||||||||||||
| flatbuffer_graph->constant_data()->Get(buffer_idx); | ||||||||||||||||||||
| auto* cd = flatbuffer_graph->constant_data(); | ||||||||||||||||||||
| ET_CHECK_OR_RETURN_ERROR( | ||||||||||||||||||||
| cd != nullptr, InvalidProgram, "constant_data is null"); | ||||||||||||||||||||
| ET_CHECK_OR_RETURN_ERROR( | ||||||||||||||||||||
| buffer_idx < cd->size(), | ||||||||||||||||||||
| InvalidProgram, | ||||||||||||||||||||
| "buffer_idx %u out of bounds for constant_data of size %u", | ||||||||||||||||||||
| buffer_idx, | ||||||||||||||||||||
| cd->size()); | ||||||||||||||||||||
|
Comment on lines
+208
to
+212
|
||||||||||||||||||||
| ConstantDataOffsetPtr constant_data_offset = cd->Get(buffer_idx); | ||||||||||||||||||||
|
||||||||||||||||||||
| ConstantDataOffsetPtr constant_data_offset = cd->Get(buffer_idx); | |
| ConstantDataOffsetPtr constant_data_offset = cd->Get(buffer_idx); | |
| if (constant_data_offset == nullptr) { | |
| ET_LOG( | |
| Error, | |
| "Invalid null constant_data entry at buffer_idx %u", | |
| buffer_idx); | |
| return nullptr; | |
| } |
Copilot
AI
Apr 10, 2026
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.
On weights-cache failures, this now always returns Error::InvalidProgram, even though load_unpacked_data() already returns a more specific error (e.g., InvalidExternalData). Consider returning data_ptr.error() (and optionally logging it) so callers can distinguish invalid models vs missing/invalid external data.
Copilot
AI
Apr 10, 2026
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 branch discards the underlying named_data_map->get_data() error and always returns Error::InvalidProgram. Propagating buffer.error() would preserve the real failure mode (e.g., NotFound/InvalidExternalData) and make error handling/debugging more accurate.
| return Error::InvalidProgram; | |
| return buffer.error(); |
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.
The format string uses
%uforcb->size(), butflatbuffers::Vector::size()issize_t. This is undefined behavior on some platforms; use%zu(or cast to an unsigned type and keep%u) so the log formatting matches the argument type.