|
6 | 6 | * LICENSE file in the root directory of this source tree. |
7 | 7 | */ |
8 | 8 |
|
| 9 | +#include <cinttypes> // @donotremove |
| 10 | + |
9 | 11 | #include <c10/util/safe_numerics.h> |
10 | 12 | #include <executorch/runtime/core/error.h> |
11 | 13 | #include <executorch/runtime/core/exec_aten/exec_aten.h> |
@@ -58,6 +60,12 @@ Result<size_t> calculate_nbytes( |
58 | 60 | executorch::aten::ScalarType scalar_type) { |
59 | 61 | size_t n = 1; |
60 | 62 | for (size_t i = 0; i < sizes.size(); i++) { |
| 63 | + ET_CHECK_OR_RETURN_ERROR( |
| 64 | + sizes[i] >= 0, |
| 65 | + InvalidProgram, |
| 66 | + "Invalid size[%zu]: %d. Size must not be negative", |
| 67 | + i, |
| 68 | + sizes[i]); |
61 | 69 | size_t next_n; |
62 | 70 | bool overflow = |
63 | 71 | c10::mul_overflows(n, static_cast<size_t>(sizes[i]), &next_n); |
@@ -92,6 +100,10 @@ Result<size_t> calculate_nbytes( |
92 | 100 | executorch::aten::ScalarType scalar_type, |
93 | 101 | const bool is_memory_planned, |
94 | 102 | std::string_view name) { |
| 103 | + ET_CHECK_OR_RETURN_ERROR( |
| 104 | + sizes.data() != nullptr && dim_order.data() != nullptr, |
| 105 | + InvalidProgram, |
| 106 | + "Null sizes or dim_order in tensor metadata"); |
95 | 107 | auto nbytes = calculate_nbytes(sizes, scalar_type); |
96 | 108 | ET_CHECK_OR_RETURN_ERROR( |
97 | 109 | nbytes.ok(), |
@@ -186,6 +198,11 @@ Result<TensorInfo> MethodMeta::input_tensor_meta(size_t index) const { |
186 | 198 | auto input_index = s_plan_->inputs()->Get(index); |
187 | 199 | // input_index was already validated by input_tag(). |
188 | 200 | auto tensor_value = s_plan_->values()->Get(input_index)->val_as_Tensor(); |
| 201 | + ET_CHECK_OR_RETURN_ERROR( |
| 202 | + tensor_value != nullptr, |
| 203 | + InvalidProgram, |
| 204 | + "Null tensor value for input %zu", |
| 205 | + index); |
189 | 206 | return TensorInfo::create( |
190 | 207 | Span<const int32_t>( |
191 | 208 | tensor_value->sizes()->data(), tensor_value->sizes()->size()), |
@@ -237,7 +254,11 @@ Result<TensorInfo> MethodMeta::output_tensor_meta(size_t index) const { |
237 | 254 | auto output_index = s_plan_->outputs()->Get(index); |
238 | 255 | // output_index was already validated by output_tag(). |
239 | 256 | auto tensor_value = s_plan_->values()->Get(output_index)->val_as_Tensor(); |
240 | | - |
| 257 | + ET_CHECK_OR_RETURN_ERROR( |
| 258 | + tensor_value != nullptr, |
| 259 | + InvalidProgram, |
| 260 | + "Null tensor value for output %zu", |
| 261 | + index); |
241 | 262 | return TensorInfo::create( |
242 | 263 | Span<const int32_t>( |
243 | 264 | tensor_value->sizes()->data(), tensor_value->sizes()->size()), |
@@ -280,7 +301,6 @@ Result<TensorInfo> MethodMeta::attribute_tensor_meta(size_t index) const { |
280 | 301 | if (counter == index) { |
281 | 302 | auto t_name = |
282 | 303 | tensor_value->extra_tensor_info()->fully_qualified_name(); |
283 | | - // Count constant returns as memory planned |
284 | 304 | return TensorInfo::create( |
285 | 305 | Span<const int32_t>( |
286 | 306 | tensor_value->sizes()->data(), tensor_value->sizes()->size()), |
@@ -322,7 +342,14 @@ Result<int64_t> MethodMeta::memory_planned_buffer_size(size_t index) const { |
322 | 342 | num_buffers); |
323 | 343 | // Index zero is reserved internally, and we hide it from users. Adjust the |
324 | 344 | // provided index to point to one of the actual buffers. |
325 | | - return s_plan_->non_const_buffer_sizes()->Get(index + 1); |
| 345 | + int64_t size = s_plan_->non_const_buffer_sizes()->Get(index + 1); |
| 346 | + ET_CHECK_OR_RETURN_ERROR( |
| 347 | + size >= 0, |
| 348 | + InvalidProgram, |
| 349 | + "memory_planned_buffer_size(%zu) has invalid negative size: %" PRId64, |
| 350 | + index, |
| 351 | + size); |
| 352 | + return size; |
326 | 353 | } |
327 | 354 |
|
328 | 355 | bool MethodMeta::uses_backend(const char* backend_name) const { |
|
0 commit comments