Skip to content

Commit 5d13a5c

Browse files
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/blob_format_importers.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ namespace {
2020

2121
using namespace intel_npu;
2222

23+
ov::Tensor allocate_aligned_tensor(size_t blobSize) {
24+
ov::Allocator customAllocator{utils::AlignedAllocator{utils::STANDARD_PAGE_SIZE}};
25+
if (blobSize > static_cast<decltype(blobSize)>(std::numeric_limits<std::streamsize>::max())) {
26+
OPENVINO_THROW("Blob size is too large to be represented on a std::streamsize!");
27+
}
28+
ov::Tensor tensor(ov::element::u8, ov::Shape{blobSize}, customAllocator);
29+
30+
return tensor;
31+
}
32+
2333
constexpr std::string_view HANDLER_FACTOR_LOGGER_NAME = "blob_format_importer_factory";
2434
constexpr std::string_view RAW_BLOB_HANDLER_LOGGER_NAME = "RawBlobImporter";
2535
constexpr std::string_view BLOB_V1_HADNLER_LOGGER_NAME = "BlobFormatV1Importer";
@@ -35,16 +45,6 @@ constexpr std::string_view DECRYPTING_PAYLOAD_MESSAGE = "Decrypting the compiler
3545

3646
const std::vector<size_t> CONSTANT_NODE_DUMMY_SHAPE{1};
3747

38-
ov::Tensor allocate_aligned_tensor(size_t blobSize) {
39-
ov::Allocator customAllocator{utils::AlignedAllocator{utils::STANDARD_PAGE_SIZE}};
40-
ov::Tensor tensor(ov::element::u8, ov::Shape{blobSize}, customAllocator);
41-
if (blobSize > static_cast<decltype(blobSize)>(std::numeric_limits<std::streamsize>::max())) {
42-
OPENVINO_THROW("Blob size is too large to be represented on a std::streamsize!");
43-
}
44-
45-
return tensor;
46-
}
47-
4848
/**
4949
* @brief Special case for PERF_COUNT as it requires compiler_type detection in case it is still set to PREFER_PLUGIN
5050
*/

src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <gtest/gtest.h>
88

9+
#include <limits>
910
#include <sstream>
1011
#include <string_view>
1112

0 commit comments

Comments
 (0)