Commit 5d13a5c
intel_npu: reject oversized blob size before allocation in allocate_aligned_tensor (#37186)
## Summary
In `allocate_aligned_tensor` (Intel NPU plugin,
`src/plugins/intel_npu/src/plugin/src/blob_format_importers.cpp`), the
bounds check guarding against `blobSize` values too large to fit in a
`std::streamsize` was performed **after** the `ov::Tensor` allocation.
This is the bug reported in #37185.
On platforms where `std::streamsize` is narrower than `size_t` (e.g.
32-bit `streamsize`, 64-bit `size_t`), the allocation may succeed for an
oversized `blobSize`, after which the caller's `stream.read(...,
static_cast<std::streamsize>(blobSize))` wraps the value to a negative
`streamsize`, producing a truncated or no-op read instead of an error.
## Fix
Move the guard before the allocation so oversized sizes are rejected
immediately, before any resource is allocated:
```cpp
ov::Tensor allocate_aligned_tensor(size_t blobSize) {
ov::Allocator customAllocator{utils::AlignedAllocator{utils::STANDARD_PAGE_SIZE}};
if (blobSize > static_cast<decltype(blobSize)>(std::numeric_limits<std::streamsize>::max())) {
OPENVINO_THROW("Blob size is too large to be represented on a std::streamsize!");
}
ov::Tensor tensor(ov::element::u8, ov::Shape{blobSize}, customAllocator);
return tensor;
}
```
To make the function unit-testable, its declaration was moved from the
anonymous namespace into the `intel_npu` namespace in
`blob_format_importers.hpp`. Behavior is unchanged for callers.
## Test plan
Added regression tests to
`src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cpp`:
- `AllocateAlignedTensorRejectsOversizedSize` — an oversized size (above
`std::numeric_limits<std::streamsize>::max()`) throws.
- `AllocateAlignedTensorAcceptsValidSize` — a valid size is allocated
without throwing.
## Verification
A standalone reproduction of the pre/post logic confirms the buggy
ordering attempted the allocation before the guard (and the
`static_cast` wrapped to a negative value), while the fixed ordering
rejects the oversized size before any allocation.
Fixes: #37185
---------
Signed-off-by: Deepak Bhagat <deepak988088@gmail.com>
Co-authored-by: Razvan Apetroaie <razvan-mihai.apetroaie@intel.com>1 parent afdb243 commit 5d13a5c
2 files changed
Lines changed: 11 additions & 10 deletions
File tree
- src/plugins/intel_npu
- src/plugin/src
- tests/unit/npu
Lines changed: 10 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
23 | 33 | | |
24 | 34 | | |
25 | 35 | | |
| |||
35 | 45 | | |
36 | 46 | | |
37 | 47 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
0 commit comments