Skip to content

Commit dd503e2

Browse files
authored
Fix null pointer dereference in MethodMeta::uses_backend() (pytorch#22405)
Fixes pytorch#22404 ### Summary `MethodMeta::uses_backend()` dereferenced `ExecutionPlan::delegates()` without a null check. Per `schema/program.fbs`, `delegates` has no default and can be legitimately unset. `num_backends()`, six lines below in the same file, already guards this exact condition — this PR applies the same guard to `uses_backend()`. See pytorch#22404 for the full root-cause writeup, reproduction, and crash output. ### Test plan Added `MethodMetaTest.UsesBackendOnUnsetDelegatesReturnsFalse` to `runtime/executor/test/method_meta_test.cpp`, constructing a minimal, self-contained, schema-valid `Program` (via `flatbuffers::FlatBufferBuilder` + `BufferDataLoader`) where `delegates` is left unset, then asserting `uses_backend()` returns `false` instead of crashing. Verified locally (Clang, Debug, `-fsanitize=address,undefined`): - Without the fix: the new test crashes with an ASan/UBSan SIGSEGV on `flatbuffers::Vector::size()` inside `uses_backend()`. - With the fix: the new test passes, and the full `method_meta_test` suite (7/7 tests) passes with no regressions. - Also ran the neighboring `program_test`, `method_test`, and `memory_manager_test` suites via `ctest` — all passing, no side effects from this change. Commands used: ``` cmake --build build-test --target method_meta_test .\build-test\runtime\executor\test\method_meta_test.exe ctest --test-dir build-test -R "method_meta_test|memory_manager_test|program_test|method_test" --output-on-failure ``` cc @larryliu0820 @JacobSzwejbka @lucylq
1 parent 751481b commit dd503e2

4 files changed

Lines changed: 96 additions & 2 deletions

File tree

‎runtime/executor/method_meta.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,14 @@ Result<etensor::Device> MethodMeta::memory_planned_buffer_device(
403403
bool MethodMeta::uses_backend(const char* backend_name) const {
404404
ET_CHECK_MSG(backend_name, "backend name is null");
405405
const auto delegates = s_plan_->delegates();
406+
if (delegates == nullptr) {
407+
return false;
408+
}
406409
for (size_t i = 0; i < delegates->size(); i++) {
407410
auto delegate = delegates->Get(i);
411+
if (delegate == nullptr || delegate->id() == nullptr) {
412+
continue;
413+
}
408414
auto backend_name_len = std::strlen(backend_name);
409415
auto delegate_id_len = delegate->id()->size();
410416
if (backend_name_len == delegate_id_len &&

‎runtime/executor/test/CMakeLists.txt‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ add_custom_target(
7979
"${CMAKE_CURRENT_BINARY_DIR}/ModuleStateful.pte"
8080
)
8181

82+
add_custom_command(
83+
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/ModuleAddWithDevice.pte"
84+
COMMAND ${PYTHON_EXECUTABLE} -m test.models.export_program_with_device_info
85+
--outdir "${CMAKE_CURRENT_BINARY_DIR}"
86+
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
87+
)
88+
89+
add_custom_target(
90+
method_meta_test_pte_files
91+
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/ModuleAddWithDevice.pte"
92+
)
93+
8294
set(test_env
8395
"DEPRECATED_ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH=${EXECUTORCH_ROOT}/test/models/deprecated/ModuleLinear-no-constant-segment.pte"
8496
"ET_MODULE_ADD_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModuleAdd.pte"
@@ -92,6 +104,7 @@ set(test_env
92104
"ET_MODULE_SIMPLE_TRAIN_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModuleSimpleTrain.pte"
93105
"ET_MODULE_STATEFUL_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModuleStateful.pte"
94106
"ET_MODULE_ADD_MUL_DELEGATED_PATH=${CMAKE_CURRENT_BINARY_DIR}/delegated/ModuleAddMul.pte"
107+
"ET_MODULE_ADD_WITH_DEVICE_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModuleAddWithDevice.pte"
95108
)
96109

97110
et_cxx_test(
@@ -130,8 +143,19 @@ et_cxx_test(
130143
add_dependencies(method_test generated_pte_files)
131144
set_property(TEST method_test PROPERTY ENVIRONMENT ${test_env})
132145

133-
# TODO(T191569140): Enable this test. et_cxx_test(method_meta_test SOURCES
134-
# method_meta_test.cpp EXTRA_LIBS extension_data_loader)
146+
et_cxx_test(
147+
method_meta_test SOURCES method_meta_test.cpp EXTRA_LIBS
148+
extension_data_loader program_schema
149+
)
150+
add_dependencies(
151+
method_meta_test generated_pte_files method_meta_test_pte_files
152+
)
153+
set_property(TEST method_meta_test PROPERTY ENVIRONMENT ${test_env})
154+
155+
target_include_directories(
156+
method_meta_test PRIVATE "${CMAKE_INSTALL_PREFIX}/schema/include"
157+
"${EXECUTORCH_ROOT}/third-party/flatbuffers/include"
158+
)
135159

136160
et_cxx_test(
137161
program_test SOURCES program_test.cpp EXTRA_LIBS extension_data_loader

‎runtime/executor/test/method_meta_test.cpp‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
#include <limits>
1313
#include <vector>
1414

15+
#include <executorch/extension/data_loader/buffer_data_loader.h>
1516
#include <executorch/extension/data_loader/file_data_loader.h>
1617
#include <executorch/runtime/core/exec_aten/exec_aten.h>
1718
#include <executorch/runtime/executor/program.h>
19+
#include <executorch/runtime/platform/runtime.h>
20+
#include <executorch/schema/program_generated.h>
1821
#include <executorch/test/utils/DeathTest.h>
1922
#include <gtest/gtest.h>
2023

@@ -25,6 +28,7 @@ using executorch::runtime::Program;
2528
using executorch::runtime::Result;
2629
using executorch::runtime::Span;
2730
using executorch::runtime::TensorInfo;
31+
using torch::executor::util::BufferDataLoader;
2832
using torch::executor::util::FileDataLoader;
2933

3034
namespace executorch {
@@ -72,6 +76,9 @@ class MethodMetaTest : public ::testing::Test {
7276
}
7377

7478
void SetUp() override {
79+
// Required to initialize the PAL timer before tests trigger ET_LOG calls in
80+
// et_pal_current_ticks().
81+
executorch::runtime::runtime_init();
7582
load_program(std::getenv("ET_MODULE_ADD_PATH"), "add");
7683
load_program(std::getenv("ET_MODULE_STATEFUL_PATH"), "stateful");
7784
const char* device_path = std::getenv("ET_MODULE_ADD_WITH_DEVICE_PATH");
@@ -275,3 +282,58 @@ TEST_F(MethodMetaTest, MethodMetaBufferDeviceReturnsCudaForDeviceBuffer) {
275282
method_meta->memory_planned_buffer_device(2).error(),
276283
Error::InvalidArgument);
277284
}
285+
286+
TEST_F(MethodMetaTest, UsesBackendOnUnsetDelegatesReturnsFalse) {
287+
// Construct a minimal schema-valid FlatBuffer program where delegates is
288+
// unset (nullptr).
289+
flatbuffers::FlatBufferBuilder fbb;
290+
std::vector<int32_t> empty_inputs = {};
291+
std::vector<int32_t> empty_outputs = {};
292+
std::vector<int64_t> non_const_buffer_sizes = {0};
293+
auto plan = executorch_flatbuffer::CreateExecutionPlanDirect(
294+
fbb,
295+
/*name=*/"forward",
296+
/*container_meta_type=*/0,
297+
/*values=*/nullptr,
298+
/*inputs=*/&empty_inputs,
299+
/*outputs=*/&empty_outputs,
300+
/*chains=*/nullptr,
301+
/*operators=*/nullptr,
302+
/*delegates=*/nullptr,
303+
/*non_const_buffer_sizes=*/&non_const_buffer_sizes,
304+
/*non_const_buffer_device=*/nullptr);
305+
std::vector<flatbuffers::Offset<executorch_flatbuffer::ExecutionPlan>> plans =
306+
{plan};
307+
308+
auto const_offsets = fbb.CreateVector(std::vector<uint64_t>{0});
309+
auto constant_segment = executorch_flatbuffer::CreateSubsegmentOffsets(
310+
fbb, /*segment_index=*/0, const_offsets);
311+
std::vector<flatbuffers::Offset<executorch_flatbuffer::DataSegment>>
312+
segments = {executorch_flatbuffer::CreateDataSegment(
313+
fbb, /*offset=*/0, /*size=*/0)};
314+
315+
auto program = executorch_flatbuffer::CreateProgramDirect(
316+
fbb,
317+
/*version=*/Program::kMaxSupportedSchemaVersion,
318+
/*execution_plan=*/&plans,
319+
/*constant_buffer=*/nullptr,
320+
/*backend_delegate_data=*/nullptr,
321+
/*segments=*/&segments,
322+
/*constant_segment=*/constant_segment);
323+
executorch_flatbuffer::FinishProgramBuffer(fbb, program);
324+
325+
std::vector<uint8_t> buffer(
326+
fbb.GetBufferPointer(), fbb.GetBufferPointer() + fbb.GetSize());
327+
if (buffer.size() < Program::kMinHeadBytes) {
328+
buffer.resize(Program::kMinHeadBytes, 0);
329+
}
330+
331+
BufferDataLoader loader(buffer.data(), buffer.size());
332+
Result<Program> prog = Program::load(&loader, Program::Verification::Minimal);
333+
ASSERT_EQ(prog.error(), Error::Ok);
334+
335+
Result<MethodMeta> method_meta = prog->method_meta("forward");
336+
ASSERT_EQ(method_meta.error(), Error::Ok);
337+
338+
EXPECT_FALSE(method_meta->uses_backend("xnnpack"));
339+
}

‎runtime/executor/test/targets.bzl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ def define_common_targets(is_fbcode = False):
177177
],
178178
deps = [
179179
"//executorch/runtime/executor:program",
180+
"//executorch/extension/data_loader:buffer_data_loader",
180181
"//executorch/extension/data_loader:file_data_loader",
182+
"//executorch/schema:program",
181183
],
182184
env = dict(
183185
modules_env,

0 commit comments

Comments
 (0)