Skip to content

Commit f3eea5c

Browse files
dbortYIWENX14
authored andcommitted
Validate value indices when deserializing list types
Differential Revision: D68127899 Pull Request resolved: #7637
1 parent b4e7d32 commit f3eea5c

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

runtime/executor/method.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,15 @@ Error Method::parse_values() {
351351

352352
// initialize boxed list
353353
for (size_t j = 0; j < items->size(); j++) {
354-
evalp_list[j] = &values_[static_cast<size_t>(items->Get(j))];
354+
auto value_index = items->Get(j);
355+
ET_CHECK_OR_RETURN_ERROR(
356+
value_index >= 0 && value_index < n_value,
357+
InvalidProgram,
358+
"Invalid value index %" PRId64 " for IntList %zu index %zu",
359+
value_index,
360+
i,
361+
j);
362+
evalp_list[j] = &values_[static_cast<size_t>(value_index)];
355363
}
356364
new (&values_[i]) EValue(
357365
BoxedEvalueList<int64_t>(evalp_list, int_list, items->size()));
@@ -411,6 +419,7 @@ Error Method::parse_values() {
411419
auto tensors = deserialization::parseTensorList(
412420
static_cast<const executorch_flatbuffer::TensorList*>(val)->items(),
413421
values_,
422+
n_value, // The size of the full array.
414423
memory_manager_);
415424
if (!tensors.ok()) {
416425
ET_LOG(
@@ -430,6 +439,7 @@ Error Method::parse_values() {
430439
val)
431440
->items(),
432441
values_,
442+
n_value, // The size of the full array.
433443
memory_manager_);
434444
if (!tensors.ok()) {
435445
ET_LOG(

runtime/executor/tensor_parser.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ ET_NODISCARD Result<executorch::aten::Tensor> parseTensor(
2525

2626
ET_NODISCARD Result<BoxedEvalueList<executorch::aten::Tensor>> parseTensorList(
2727
const flatbuffers::Vector<int32_t>* tensor_indices,
28-
EValue* values_,
28+
EValue* values,
29+
size_t values_len,
2930
MemoryManager* memory_manager);
3031

3132
// Deserializes a List of optional type. The code here is the same between all
@@ -35,7 +36,8 @@ template <typename T>
3536
ET_NODISCARD Result<BoxedEvalueList<executorch::aten::optional<T>>>
3637
parseListOptionalType(
3738
const flatbuffers::Vector<int32_t>* value_indices,
38-
EValue* values_,
39+
EValue* values,
40+
size_t values_len,
3941
MemoryManager* memory_manager) {
4042
auto* evalp_list = memory_manager->method_allocator()->allocateList<EValue*>(
4143
value_indices->size());
@@ -55,7 +57,7 @@ parseListOptionalType(
5557
// already allocated) and stick it in the list.
5658
for (int32_t index : *value_indices) {
5759
// Lists of objects are stored in fbb as list[int] where the ints are
58-
// indices into values_. Currently serialization is deciding if they want to
60+
// indices into values. Currently serialization is deciding if they want to
5961
// put -1 for serialized None type indices, or give us a valid index to a
6062
// serialized None. We support either for now.
6163
// Placement new as the list elements are not initialized, so calling
@@ -68,9 +70,14 @@ parseListOptionalType(
6870
// TODO(T161156879): do something less hacky here.
6971
evalp_list[output_idx] = nullptr;
7072
} else {
73+
ET_CHECK_OR_RETURN_ERROR(
74+
index >= 0 && index < values_len,
75+
InvalidProgram,
76+
"Invalid value index %" PRId32 " for ListOptional",
77+
index);
7178
new (&optional_tensor_list[output_idx])
72-
executorch::aten::optional<T>(values_[index].toOptional<T>());
73-
evalp_list[output_idx] = &values_[static_cast<size_t>(index)];
79+
executorch::aten::optional<T>(values[index].toOptional<T>());
80+
evalp_list[output_idx] = &values[static_cast<size_t>(index)];
7481
}
7582
output_idx++;
7683
}

runtime/executor/tensor_parser_exec_aten.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ ET_NODISCARD Result<void*> getMemPlannedPtr(
7070

7171
ET_NODISCARD Result<BoxedEvalueList<exec_aten::Tensor>> parseTensorList(
7272
const flatbuffers::Vector<int32_t>* tensor_indices,
73-
EValue* values_,
73+
EValue* values,
74+
size_t values_len,
7475
MemoryManager* memory_manager) {
7576
EXECUTORCH_SCOPE_PROF("TensorParser::parseTensorList");
7677

@@ -90,11 +91,17 @@ ET_NODISCARD Result<BoxedEvalueList<exec_aten::Tensor>> parseTensorList(
9091
// already allocated) and stick it in the list.
9192
size_t output_idx = 0;
9293
for (int32_t tensor_index : *tensor_indices) {
94+
ET_CHECK_OR_RETURN_ERROR(
95+
tensor_index >= 0 && tensor_index < values_len,
96+
InvalidProgram,
97+
"Invalid value index %" PRId32 " for TensorList",
98+
tensor_index);
99+
93100
// Placement new as the list elements are not initialized, so calling
94-
// copy assignment is not defined if its non trivial.
95-
new (&tensor_list[output_idx]) exec_aten::Tensor(
96-
values_[static_cast<size_t>(tensor_index)].toTensor());
97-
evalp_list[output_idx] = &values_[static_cast<size_t>(tensor_index)];
101+
// copy assignment is not defined if it's non trivial.
102+
new (&tensor_list[output_idx])
103+
exec_aten::Tensor(values[static_cast<size_t>(tensor_index)].toTensor());
104+
evalp_list[output_idx] = &values[static_cast<size_t>(tensor_index)];
98105
output_idx++;
99106
}
100107

0 commit comments

Comments
 (0)