Skip to content

Commit 20415bf

Browse files
meta-codesync[bot]DevmateRemedimateMacaClaude Bot
andauthored
Fix out_of_bounds_read in getConstantDataPtr (XNNCompiler.cpp) (T267371218) (pytorch#19593)
Reviewed By: psiddh Differential Revision: D104380965 Co-authored-by: DevmateRemedimateMacaClaude Bot <noreply+1383054420177565@fb.com>
1 parent 2a0a2f8 commit 20415bf

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

backends/xnnpack/runtime/XNNCompiler.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <executorch/extension/threadpool/threadpool.h>
1313
#include <executorch/runtime/executor/pte_data_map.h>
1414
#include <xnnpack.h>
15+
#include <cinttypes>
1516
#include <string>
1617
#include <unordered_map>
1718
#include <vector>
@@ -179,6 +180,7 @@ Result<const uint8_t*> getConstantDataPtr(
179180
uint32_t buffer_idx,
180181
GraphPtr flatbuffer_graph,
181182
const uint8_t* constant_data_ptr,
183+
uint64_t constant_data_size,
182184
const NamedDataMap* named_data_map,
183185
std::vector<FreeableBuffer>& freeable_buffers,
184186
XNNWeightsCache* weights_cache,
@@ -220,10 +222,20 @@ Result<const uint8_t*> getConstantDataPtr(
220222
"Null constant_data entry at buffer_idx %u",
221223
buffer_idx);
222224
uint64_t offset = constant_data_offset->offset();
225+
uint64_t entry_size = constant_data_offset->size();
223226
bool has_named_key = flatbuffers::IsFieldPresent(
224227
constant_data_offset, fb_xnnpack::ConstantDataOffset::VT_NAMED_KEY);
225228
// If there is no tensor name
226229
if (!has_named_key) {
230+
ET_CHECK_OR_RETURN_ERROR(
231+
offset <= constant_data_size &&
232+
entry_size <= constant_data_size - offset,
233+
InvalidProgram,
234+
"ConstantDataOffset {offset=%" PRIu64 ", size=%" PRIu64
235+
"} out of bounds for constant_data region of size %" PRIu64,
236+
offset,
237+
entry_size,
238+
constant_data_size);
227239
return constant_data_ptr + offset;
228240
} else {
229241
ET_CHECK_OR_RETURN_ERROR(
@@ -266,6 +278,7 @@ Result<const uint8_t*> getConstantDataPtr(
266278
const fb_xnnpack::XNNTensorValue* tensor_value,
267279
GraphPtr flatbuffer_graph,
268280
const uint8_t* constant_data_ptr,
281+
uint64_t constant_data_size,
269282
const NamedDataMap* named_data_map,
270283
std::vector<FreeableBuffer>& freeable_buffers,
271284
XNNWeightsCache* weights_cache,
@@ -274,6 +287,7 @@ Result<const uint8_t*> getConstantDataPtr(
274287
tensor_value->constant_buffer_idx(),
275288
flatbuffer_graph,
276289
constant_data_ptr,
290+
constant_data_size,
277291
named_data_map,
278292
freeable_buffers,
279293
weights_cache,
@@ -291,6 +305,7 @@ Error defineTensor(
291305
ValuePtr value,
292306
GraphPtr flatbuffer_graph,
293307
const uint8_t* constant_data_ptr,
308+
uint64_t constant_data_size,
294309
std::vector<uint32_t>& input_ids,
295310
std::vector<uint32_t>& output_ids,
296311
CompileAllocator& allocator,
@@ -349,6 +364,7 @@ Error defineTensor(
349364
tensor_value,
350365
flatbuffer_graph,
351366
constant_data_ptr,
367+
constant_data_size,
352368
named_data_map,
353369
freeable_buffers,
354370
weights_cache,
@@ -505,6 +521,7 @@ Error defineTensor(
505521
qparams->scale_buffer_idx(),
506522
flatbuffer_graph,
507523
constant_data_ptr,
524+
constant_data_size,
508525
named_data_map,
509526
freeable_buffers,
510527
weights_cache,
@@ -552,6 +569,7 @@ Error defineTensor(
552569
qparams->scale_buffer_idx(),
553570
flatbuffer_graph,
554571
constant_data_ptr,
572+
constant_data_size,
555573
named_data_map,
556574
freeable_buffers,
557575
weights_cache,
@@ -1988,6 +2006,7 @@ ET_NODISCARD Error XNNCompiler::compileModel(
19882006
Result<XNNHeader> header = XNNHeader::Parse(buffer_pointer, num_bytes);
19892007
const uint8_t* flatbuffer_data = nullptr;
19902008
const uint8_t* constant_data = nullptr;
2009+
uint64_t constant_data_size = 0;
19912010
size_t flatbuffer_size = 0;
19922011
CompileAllocator compile_allocator;
19932012

@@ -1998,6 +2017,7 @@ ET_NODISCARD Error XNNCompiler::compileModel(
19982017
flatbuffer_size = header->flatbuffer_size;
19992018
constant_data = reinterpret_cast<const uint8_t*>(buffer_pointer) +
20002019
header->constant_data_offset;
2020+
constant_data_size = header->constant_data_size;
20012021
} else if (header.error() == Error::NotFound) {
20022022
flatbuffer_data = reinterpret_cast<const uint8_t*>(buffer_pointer);
20032023
flatbuffer_size = num_bytes;
@@ -2089,6 +2109,7 @@ ET_NODISCARD Error XNNCompiler::compileModel(
20892109
value,
20902110
flatbuffer_graph,
20912111
constant_data,
2112+
constant_data_size,
20922113
input_ids,
20932114
output_ids,
20942115
compile_allocator,

0 commit comments

Comments
 (0)