Skip to content

Commit d869385

Browse files
dbortfacebook-github-bot
authored andcommitted
Check for missing arrays in Program (#1519)
Summary: Pull Request resolved: #1519 Flatbuffer array fields can be missing, so we need to check for `nullptr` before calling `size()` on them. Discovered by lionhead fuzzing. Reviewed By: larryliu0820 Differential Revision: D52493423 fbshipit-source-id: 50ed3ebba5cccb580068bd6078cbade91931a3dc
1 parent b4c6afc commit d869385

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

runtime/executor/program.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,33 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
146146

147147
// Constant data may live inside the flatbuffer data (constant_buffer) or in a
148148
// separate segment (constant_segment). It should not be in both.
149-
const auto& constant_buffer = flatbuffer_program->constant_buffer();
150-
const auto& constant_segment = flatbuffer_program->constant_segment();
151-
152-
// Check if the constant data is inside a separate segment.
153-
if (constant_segment != nullptr && constant_segment->offsets()->size() > 0) {
149+
const auto* constant_segment = flatbuffer_program->constant_segment();
150+
if (constant_segment != nullptr && constant_segment->offsets() != nullptr &&
151+
constant_segment->offsets()->size() > 0) {
152+
// The constant data is inside a separate segment.
153+
const auto* constant_buffer = flatbuffer_program->constant_buffer();
154154
ET_CHECK_OR_RETURN_ERROR(
155-
constant_buffer->size() == 0,
156-
InvalidState,
157-
"constant_buffer contains %u items, constant_segment.offsets contains %u items. Only one should be used.",
155+
constant_buffer == nullptr || constant_buffer->size() == 0,
156+
InvalidProgram,
157+
"constant_buffer contains %u items, "
158+
"constant_segment.offsets contains %u items. Only one should be used.",
158159
constant_buffer->size(),
159160
constant_segment->offsets()->size());
161+
const auto* segments = flatbuffer_program->segments();
162+
ET_CHECK_OR_RETURN_ERROR(
163+
segments != nullptr, InvalidProgram, "No segments in program");
160164

161165
// Load constant segment.
162166
// TODO(T171839323): Add test for segment_index > num available segments.
163167
ET_CHECK_OR_RETURN_ERROR(
164-
constant_segment->segment_index() <
165-
flatbuffer_program->segments()->size(),
166-
InvalidArgument,
168+
constant_segment->segment_index() < segments->size(),
169+
InvalidProgram,
167170
"Constant segment index %d invalid for program segments range %d",
168171
constant_segment->segment_index(),
169-
flatbuffer_program->segments()->size());
172+
segments->size());
170173

171174
const executorch_flatbuffer::DataSegment* data_segment =
172-
flatbuffer_program->segments()->Get(constant_segment->segment_index());
175+
segments->Get(constant_segment->segment_index());
173176
Result<FreeableBuffer> constant_segment_data = loader->Load(
174177
segment_base_offset + data_segment->offset(), data_segment->size());
175178
if (!constant_segment_data.ok()) {
@@ -199,7 +202,12 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
199202
size_t Program::num_methods() const {
200203
auto internal_program =
201204
static_cast<const executorch_flatbuffer::Program*>(internal_program_);
202-
return internal_program->execution_plan()->size();
205+
const auto execution_plan = internal_program->execution_plan();
206+
if (execution_plan != nullptr) {
207+
return execution_plan->size();
208+
} else {
209+
return 0;
210+
}
203211
}
204212

205213
Result<const char*> Program::get_method_name(size_t plan_index) const {

0 commit comments

Comments
 (0)