|
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,12 @@ 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 && tensor_value->sizes() != nullptr && |
| 203 | + tensor_value->dim_order() != nullptr, |
| 204 | + InvalidProgram, |
| 205 | + "Null tensor metadata for input %zu", |
| 206 | + index); |
189 | 207 | return TensorInfo::create( |
190 | 208 | Span<const int32_t>( |
191 | 209 | tensor_value->sizes()->data(), tensor_value->sizes()->size()), |
@@ -237,7 +255,12 @@ Result<TensorInfo> MethodMeta::output_tensor_meta(size_t index) const { |
237 | 255 | auto output_index = s_plan_->outputs()->Get(index); |
238 | 256 | // output_index was already validated by output_tag(). |
239 | 257 | auto tensor_value = s_plan_->values()->Get(output_index)->val_as_Tensor(); |
240 | | - |
| 258 | + ET_CHECK_OR_RETURN_ERROR( |
| 259 | + tensor_value != nullptr && tensor_value->sizes() != nullptr && |
| 260 | + tensor_value->dim_order() != nullptr, |
| 261 | + InvalidProgram, |
| 262 | + "Null tensor metadata for output %zu", |
| 263 | + index); |
241 | 264 | return TensorInfo::create( |
242 | 265 | Span<const int32_t>( |
243 | 266 | tensor_value->sizes()->data(), tensor_value->sizes()->size()), |
@@ -274,10 +297,17 @@ Result<TensorInfo> MethodMeta::attribute_tensor_meta(size_t index) const { |
274 | 297 | auto value = values->Get(i); |
275 | 298 | if (value->val_type() == executorch_flatbuffer::KernelTypes::Tensor) { |
276 | 299 | auto tensor_value = value->val_as_Tensor(); |
277 | | - if (tensor_value->extra_tensor_info() != nullptr && |
| 300 | + if (tensor_value != nullptr && |
| 301 | + tensor_value->extra_tensor_info() != nullptr && |
278 | 302 | tensor_value->extra_tensor_info()->fully_qualified_name()->c_str() != |
279 | 303 | nullptr) { |
280 | 304 | if (counter == index) { |
| 305 | + ET_CHECK_OR_RETURN_ERROR( |
| 306 | + tensor_value->sizes() != nullptr && |
| 307 | + tensor_value->dim_order() != nullptr, |
| 308 | + InvalidProgram, |
| 309 | + "Null tensor metadata for attribute %zu", |
| 310 | + index); |
281 | 311 | auto t_name = |
282 | 312 | tensor_value->extra_tensor_info()->fully_qualified_name(); |
283 | 313 | // Count constant returns as memory planned |
@@ -322,7 +352,14 @@ Result<int64_t> MethodMeta::memory_planned_buffer_size(size_t index) const { |
322 | 352 | num_buffers); |
323 | 353 | // Index zero is reserved internally, and we hide it from users. Adjust the |
324 | 354 | // provided index to point to one of the actual buffers. |
325 | | - return s_plan_->non_const_buffer_sizes()->Get(index + 1); |
| 355 | + int64_t size = s_plan_->non_const_buffer_sizes()->Get(index + 1); |
| 356 | + ET_CHECK_OR_RETURN_ERROR( |
| 357 | + size >= 0, |
| 358 | + InvalidProgram, |
| 359 | + "memory_planned_buffer_size(%zu) has invalid negative size: %" PRId64, |
| 360 | + index, |
| 361 | + size); |
| 362 | + return size; |
326 | 363 | } |
327 | 364 |
|
328 | 365 | bool MethodMeta::uses_backend(const char* backend_name) const { |
|
0 commit comments