1212#include < executorch/extension/data_loader/buffer_data_loader.h>
1313#include < executorch/extension/data_loader/file_data_loader.h>
1414#include < executorch/runtime/core/error.h>
15+ #include < executorch/runtime/executor/method.h>
1516#include < executorch/runtime/executor/program.h>
17+ #include < executorch/runtime/executor/test/managed_memory_manager.h>
18+ #include < executorch/runtime/kernel/operator_registry.h>
1619#include < executorch/runtime/platform/runtime.h>
1720#include < executorch/schema/program_generated.h>
1821
@@ -72,7 +75,10 @@ struct EValueConfig {
7275// specifies which value indices appear in the execution plan's inputs list.
7376std::vector<uint8_t > CreateTestProgram (
7477 const std::vector<EValueConfig>& configs,
75- const std::vector<int32_t >& input_indices = {}) {
78+ const std::vector<int32_t >& input_indices = {},
79+ // If true, emit a chain of two KernelCalls: the first's op_index is out of
80+ // range so resolve_operator() fails on a non-final instruction.
81+ bool with_out_of_range_kernel_call = false ) {
7682 flatbuffers::FlatBufferBuilder builder (1024 );
7783
7884 std::vector<flatbuffers::Offset<executorch_flatbuffer::EValue>> evalues;
@@ -132,10 +138,37 @@ std::vector<uint8_t> CreateTestProgram(
132138 auto inputs_vec = builder.CreateVector (input_indices);
133139 auto empty_int_vec = builder.CreateVector (std::vector<int32_t >{});
134140 auto empty_int64_vec = builder.CreateVector (std::vector<int64_t >{0 });
135- auto empty_chain_vec = builder.CreateVector (
136- std::vector<flatbuffers::Offset<executorch_flatbuffer::Chain>>{});
137- auto empty_operators_vec = builder.CreateVector (
138- std::vector<flatbuffers::Offset<executorch_flatbuffer::Operator>>{});
141+ std::vector<flatbuffers::Offset<executorch_flatbuffer::Chain>> chains;
142+ if (with_out_of_range_kernel_call) {
143+ auto kernel_call = executorch_flatbuffer::CreateKernelCall (
144+ builder, /* op_index=*/ 5 , builder.CreateVector (std::vector<int32_t >{}));
145+ auto good_call = executorch_flatbuffer::CreateKernelCall (
146+ builder, /* op_index=*/ 0 , builder.CreateVector (std::vector<int32_t >{}));
147+ std::vector<flatbuffers::Offset<executorch_flatbuffer::Instruction>>
148+ instructions{
149+ executorch_flatbuffer::CreateInstruction (
150+ builder,
151+ executorch_flatbuffer::InstructionArguments::KernelCall,
152+ kernel_call.Union ()),
153+ // Resolves, so letting a later Ok overwrite the failure returns Ok.
154+ executorch_flatbuffer::CreateInstruction (
155+ builder,
156+ executorch_flatbuffer::InstructionArguments::KernelCall,
157+ good_call.Union ())};
158+ chains.push_back (executorch_flatbuffer::CreateChain (
159+ builder,
160+ /* inputs=*/ builder.CreateVector (std::vector<int32_t >{}),
161+ /* outputs=*/ builder.CreateVector (std::vector<int32_t >{}),
162+ builder.CreateVector (instructions),
163+ /* stacktrace=*/ 0 ));
164+ }
165+ auto chain_vec = builder.CreateVector (chains);
166+ std::vector<flatbuffers::Offset<executorch_flatbuffer::Operator>> operators;
167+ if (with_out_of_range_kernel_call) {
168+ operators.push_back (executorch_flatbuffer::CreateOperator (
169+ builder, builder.CreateString (" test::noop" ), builder.CreateString (" " )));
170+ }
171+ auto operators_vec = builder.CreateVector (operators);
139172 auto empty_delegates_vec = builder.CreateVector (
140173 std::vector<
141174 flatbuffers::Offset<executorch_flatbuffer::BackendDelegate>>{});
@@ -147,8 +180,8 @@ std::vector<uint8_t> CreateTestProgram(
147180 values_vec,
148181 /* inputs=*/ inputs_vec,
149182 /* outputs=*/ empty_int_vec,
150- empty_chain_vec ,
151- empty_operators_vec ,
183+ chain_vec ,
184+ operators_vec ,
152185 empty_delegates_vec,
153186 empty_int64_vec);
154187
@@ -339,3 +372,35 @@ TEST_F(ProgramValidationTest, TensorListWithOutOfBoundsIndexDetected) {
339372 Program::load (&loader, Program::Verification::InternalConsistency);
340373 EXPECT_EQ (program.error (), Error::InvalidProgram);
341374}
375+
376+ // A resolve_operator() failure is surfaced after the loop, so a later
377+ // instruction must not discard it.
378+ TEST_F (ProgramValidationTest, ResolveOperatorFailureSurfacesFromInit) {
379+ std::vector<EValueConfig> configs = {
380+ {EValueType::Tensor, {2 , 2 }, {}, /* is_dynamic=*/ false }};
381+
382+ AlignedBuffer buf (CreateTestProgram (
383+ configs,
384+ /* input_indices=*/ {},
385+ /* with_out_of_range_kernel_call=*/ true ));
386+ auto loader = buf.loader ();
387+
388+ // Minimal, so validation doesn't reject the bad op_index before init() runs.
389+ Result<Program> program =
390+ Program::load (&loader, Program::Verification::Minimal);
391+ ASSERT_EQ (program.error (), Error::Ok);
392+
393+ // A keyless kernel matches any meta, so instruction 1 resolves.
394+ static const auto noop = executorch::runtime::Kernel (
395+ " test::noop" ,
396+ [](executorch::runtime::KernelRuntimeContext&,
397+ executorch::runtime::Span<executorch::runtime::EValue*>) {});
398+ ASSERT_EQ (executorch::runtime::register_kernel (noop), Error::Ok);
399+
400+ executorch::runtime::testing::ManagedMemoryManager mmm (
401+ /* planned_memory_bytes=*/ 32 * 1024U ,
402+ /* method_allocator_bytes=*/ 32 * 1024U );
403+ Result<executorch::runtime::Method> method =
404+ program->load_method (" forward" , &mmm.get ());
405+ EXPECT_EQ (method.error (), Error::InvalidProgram);
406+ }
0 commit comments