66 * LICENSE file in the root directory of this source tree.
77 */
88
9+ #include < cinttypes> // @donotremove
10+
911#include < c10/util/safe_numerics.h>
1012#include < executorch/runtime/core/error.h>
1113#include < executorch/runtime/core/exec_aten/exec_aten.h>
@@ -58,6 +60,12 @@ Result<size_t> calculate_nbytes(
5860 executorch::aten::ScalarType scalar_type) {
5961 size_t n = 1 ;
6062 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]);
6169 size_t next_n;
6270 bool overflow =
6371 c10::mul_overflows (n, static_cast <size_t >(sizes[i]), &next_n);
@@ -186,6 +194,12 @@ Result<TensorInfo> MethodMeta::input_tensor_meta(size_t index) const {
186194 auto input_index = s_plan_->inputs ()->Get (index);
187195 // input_index was already validated by input_tag().
188196 auto tensor_value = s_plan_->values ()->Get (input_index)->val_as_Tensor ();
197+ ET_CHECK_OR_RETURN_ERROR (
198+ tensor_value != nullptr && tensor_value->sizes () != nullptr &&
199+ tensor_value->dim_order () != nullptr ,
200+ InvalidProgram,
201+ " Null tensor metadata for input %zu" ,
202+ index);
189203 return TensorInfo::create (
190204 Span<const int32_t >(
191205 tensor_value->sizes ()->data (), tensor_value->sizes ()->size ()),
@@ -237,7 +251,12 @@ Result<TensorInfo> MethodMeta::output_tensor_meta(size_t index) const {
237251 auto output_index = s_plan_->outputs ()->Get (index);
238252 // output_index was already validated by output_tag().
239253 auto tensor_value = s_plan_->values ()->Get (output_index)->val_as_Tensor ();
240-
254+ ET_CHECK_OR_RETURN_ERROR (
255+ tensor_value != nullptr && tensor_value->sizes () != nullptr &&
256+ tensor_value->dim_order () != nullptr ,
257+ InvalidProgram,
258+ " Null tensor metadata for output %zu" ,
259+ index);
241260 return TensorInfo::create (
242261 Span<const int32_t >(
243262 tensor_value->sizes ()->data (), tensor_value->sizes ()->size ()),
@@ -257,7 +276,10 @@ size_t MethodMeta::num_attributes() const {
257276 auto value = values->Get (i);
258277 if (value->val_type () == executorch_flatbuffer::KernelTypes::Tensor) {
259278 auto tensor_value = value->val_as_Tensor ();
260- if (tensor_value->extra_tensor_info () != nullptr &&
279+ if (tensor_value != nullptr &&
280+ tensor_value->extra_tensor_info () != nullptr &&
281+ tensor_value->extra_tensor_info ()->fully_qualified_name () !=
282+ nullptr &&
261283 tensor_value->extra_tensor_info ()->fully_qualified_name ()->c_str () !=
262284 nullptr ) {
263285 ++counter;
@@ -274,10 +296,19 @@ Result<TensorInfo> MethodMeta::attribute_tensor_meta(size_t index) const {
274296 auto value = values->Get (i);
275297 if (value->val_type () == executorch_flatbuffer::KernelTypes::Tensor) {
276298 auto tensor_value = value->val_as_Tensor ();
277- if (tensor_value->extra_tensor_info () != nullptr &&
299+ if (tensor_value != nullptr &&
300+ tensor_value->extra_tensor_info () != nullptr &&
301+ tensor_value->extra_tensor_info ()->fully_qualified_name () !=
302+ nullptr &&
278303 tensor_value->extra_tensor_info ()->fully_qualified_name ()->c_str () !=
279304 nullptr ) {
280305 if (counter == index) {
306+ ET_CHECK_OR_RETURN_ERROR (
307+ tensor_value->sizes () != nullptr &&
308+ tensor_value->dim_order () != nullptr ,
309+ InvalidProgram,
310+ " Null tensor metadata for attribute %zu" ,
311+ index);
281312 auto t_name =
282313 tensor_value->extra_tensor_info ()->fully_qualified_name ();
283314 // Count constant returns as memory planned
@@ -322,7 +353,14 @@ Result<int64_t> MethodMeta::memory_planned_buffer_size(size_t index) const {
322353 num_buffers);
323354 // Index zero is reserved internally, and we hide it from users. Adjust the
324355 // provided index to point to one of the actual buffers.
325- return s_plan_->non_const_buffer_sizes ()->Get (index + 1 );
356+ int64_t size = s_plan_->non_const_buffer_sizes ()->Get (index + 1 );
357+ ET_CHECK_OR_RETURN_ERROR (
358+ size >= 0 ,
359+ InvalidProgram,
360+ " memory_planned_buffer_size(%zu) has invalid negative size: %" PRId64,
361+ index,
362+ size);
363+ return size;
326364}
327365
328366bool MethodMeta::uses_backend (const char * backend_name) const {
0 commit comments