Skip to content

Commit 59f66cf

Browse files
lucylqGithub Executorch
andauthored
Add null checks to xnnpack flatbuffer (#18799)
Add null checks to the flatbuffer graph on: - flatbuffer graph itself - xnodes - xvalues Check that num_externs is <= num_values Authored-with: Claude Co-authored-by: Github Executorch <github_executorch@arm.com>
1 parent 273aee9 commit 59f66cf

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

backends/xnnpack/runtime/XNNCompiler.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,12 @@ ET_NODISCARD Error XNNCompiler::compileModel(
19271927
flatbuffers::GetBufferIdentifier(flatbuffer_data));
19281928

19291929
auto flatbuffer_graph = fb_xnnpack::GetXNNGraph(flatbuffer_data);
1930+
ET_CHECK_OR_RETURN_ERROR(
1931+
flatbuffer_graph != nullptr && flatbuffer_graph->xvalues() != nullptr &&
1932+
flatbuffer_graph->xnodes() != nullptr,
1933+
InvalidProgram,
1934+
"Failed to deserialize XNNPACK flatbuffer graph; null graph, xvalues, or xnodes.");
1935+
19301936
// initialize xnnpack
19311937
xnn_status status = xnn_initialize(/*allocator =*/nullptr);
19321938
ET_CHECK_OR_RETURN_ERROR(
@@ -1936,9 +1942,18 @@ ET_NODISCARD Error XNNCompiler::compileModel(
19361942
xnn_status_to_string(status));
19371943

19381944
// create xnnpack subgraph
1945+
uint32_t num_externs = flatbuffer_graph->num_externs();
1946+
uint32_t num_values = flatbuffer_graph->xvalues()->size();
1947+
ET_CHECK_OR_RETURN_ERROR(
1948+
num_externs <= num_values,
1949+
InvalidProgram,
1950+
"num_externs (%u) exceeds total number of values (%u)",
1951+
num_externs,
1952+
num_values);
1953+
19391954
xnn_subgraph_t subgraph_ptr = nullptr;
19401955
status = xnn_create_subgraph(
1941-
/*external_value_ids=*/flatbuffer_graph->num_externs(),
1956+
/*external_value_ids=*/num_externs,
19421957
/*flags=*/0,
19431958
&subgraph_ptr);
19441959
ET_CHECK_OR_RETURN_ERROR(

0 commit comments

Comments
 (0)