Skip to content

Commit 092f42f

Browse files
authored
Fix null pointer dereference in get_execution_plan (pytorch#16795)
### Summary Add null ptr check in program.get_execution_plan. Not sure if the test is really necessary but have it here just in case. ### Test plan ``` cmake .. -DEXECUTORCH_BUILD_TESTS=ON cmake --build . --target program_test ctest -R program_test -V ```
1 parent f8eead2 commit 092f42f

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎runtime/executor/program.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
4949
auto execution_plans = program->execution_plan();
5050
for (size_t i = 0; i < execution_plans->size(); i++) {
5151
auto plan = execution_plans->GetMutableObject(i);
52-
if (std::strcmp(plan->name()->c_str(), method_name) == 0) {
52+
if (plan != nullptr && plan->name() != nullptr &&
53+
std::strcmp(plan->name()->c_str(), method_name) == 0) {
5354
return plan;
5455
}
5556
}

‎runtime/executor/test/program_test.cpp‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,50 @@ TEST_F(ProgramTest, GetOutputFlatteningEncodingWithMissingEncodedOutStr) {
780780

781781
EXPECT_EQ(encoding.error(), Error::InvalidProgram);
782782
}
783+
784+
TEST_F(ProgramTest, NullPlanNameDoesNotCrash) {
785+
flatbuffers::FlatBufferBuilder builder(1024);
786+
787+
// Create dummy execution_plan with null plan_name.
788+
auto execution_plan = executorch_flatbuffer::CreateExecutionPlan(
789+
builder,
790+
0,
791+
0, // name=null, container_meta_type=null
792+
builder.CreateVector(
793+
std::vector<flatbuffers::Offset<executorch_flatbuffer::EValue>>{}),
794+
builder.CreateVector(std::vector<int32_t>{}),
795+
builder.CreateVector(std::vector<int32_t>{}),
796+
builder.CreateVector(
797+
std::vector<flatbuffers::Offset<executorch_flatbuffer::Chain>>{}),
798+
builder.CreateVector(
799+
std::vector<flatbuffers::Offset<executorch_flatbuffer::Operator>>{}),
800+
builder.CreateVector(
801+
std::vector<
802+
flatbuffers::Offset<executorch_flatbuffer::BackendDelegate>>{}),
803+
builder.CreateVector(std::vector<int64_t>{0}));
804+
805+
auto program = executorch_flatbuffer::CreateProgram(
806+
builder,
807+
0,
808+
builder.CreateVector({execution_plan}),
809+
builder.CreateVector(
810+
std::vector<flatbuffers::Offset<executorch_flatbuffer::Buffer>>{}),
811+
builder.CreateVector(
812+
std::vector<flatbuffers::Offset<
813+
executorch_flatbuffer::BackendDelegateInlineData>>{}),
814+
builder.CreateVector(
815+
std::vector<
816+
flatbuffers::Offset<executorch_flatbuffer::DataSegment>>{}));
817+
818+
builder.Finish(program, executorch_flatbuffer::ProgramIdentifier());
819+
820+
alignas(16) uint8_t buf[2048];
821+
std::memcpy(buf, builder.GetBufferPointer(), builder.GetSize());
822+
BufferDataLoader loader(buf, builder.GetSize());
823+
824+
Result<Program> p = Program::load(&loader, Program::Verification::Minimal);
825+
ASSERT_EQ(p.error(), Error::Ok);
826+
827+
// Should return error, not crash
828+
EXPECT_EQ(p->method_meta("forward").error(), Error::InvalidArgument);
829+
}

0 commit comments

Comments
 (0)