Skip to content

Fix stack buffer overflow when XNNPACK tensor has too many dims #3233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backends/xnnpack/runtime/XNNExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ __ET_NODISCARD Error XNNExecutor::prepare_args(EValue** args) {
if (i < input_ids_.size()) {
size_t num_dims = tensor->dim();
size_t dims[XNN_MAX_TENSOR_DIMS];
ET_CHECK_OR_RETURN_ERROR(
num_dims <= XNN_MAX_TENSOR_DIMS,
InvalidArgument,
"XNNPACK backend accepts tensors with at most %d dims, but got %zu",
XNN_MAX_TENSOR_DIMS,
num_dims);
for (int d = 0; d < num_dims; ++d) {
dims[d] = tensor->size(d);
}
Expand Down
4 changes: 3 additions & 1 deletion backends/xnnpack/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ def define_common_targets():
preprocessor_flags = [
# "-DENABLE_XNNPACK_PROFILING",
],
exported_deps = [
"//executorch/runtime/backend:interface",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason we make it an exported_deps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's an include in a header (I think XNNExecutor.h), so dependent libraries that include that header need this dep. This is what exported_deps is for.

],
deps = [
third_party_dep("XNNPACK"),
"//executorch/runtime/backend:interface",
"//executorch/backends/xnnpack/serialization:xnnpack_flatbuffer_header",
"//executorch/backends/xnnpack/threadpool:threadpool",
"//executorch/runtime/core/exec_aten/util:tensor_util",
Expand Down
94 changes: 94 additions & 0 deletions backends/xnnpack/test/runtime/test_xnnexecutor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/backends/xnnpack/runtime/XNNExecutor.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh is it for the test....

#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h>
#include <gtest/gtest.h>
#include <xnnpack/subgraph.h>

using torch::executor::Error;
using torch::executor::EValue;
using torch::executor::testing::TensorFactory;
using torch::executor::xnnpack::delegate::XNNExecutor;

TEST(XNNExecutorTest, ArgumentWithTooManyDimensions) {
XNNExecutor executor;
xnn_subgraph_t subgraph = nullptr;
xnn_runtime_t rt = nullptr;
et_pal_init();
ASSERT_EQ(xnn_initialize(nullptr), xnn_status_success);
ASSERT_EQ(xnn_create_subgraph(2, 0, &subgraph), xnn_status_success);
std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(
subgraph, xnn_delete_subgraph);

auto input_id = XNN_INVALID_NODE_ID;
std::vector<size_t> dims = {
1,
};
ASSERT_EQ(
xnn_status_success,
xnn_define_quantized_tensor_value(
subgraph,
xnn_datatype_qint8,
0,
1,
dims.size(),
dims.data(),
nullptr,
/*external_id=*/0,
/*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT,
&input_id));
ASSERT_NE(input_id, XNN_INVALID_NODE_ID);

auto output_id = XNN_INVALID_NODE_ID;
ASSERT_EQ(
xnn_status_success,
xnn_define_quantized_tensor_value(
subgraph,
xnn_datatype_qint8,
0,
1,
dims.size(),
dims.data(),
nullptr,
/*external_id=*/0,
/*flags=*/XNN_VALUE_FLAG_EXTERNAL_OUTPUT,
&output_id));
ASSERT_NE(output_id, XNN_INVALID_NODE_ID);

ASSERT_EQ(
xnn_status_success,
xnn_define_clamp(subgraph, 1, 2, input_id, output_id, 0));

ASSERT_EQ(xnn_create_runtime(subgraph, &rt), xnn_status_success);
EXPECT_EQ(
executor.initialize(
rt,
{
0,
},
{
1,
}),
Error::Ok);
TensorFactory<exec_aten::ScalarType::Int> tf;
auto input_tensor = tf.make({1, 1, 1, 1, 1, 1, 1, 1, 1}, {42});
ASSERT_EQ(input_tensor.dim(), 9);
auto output_tensor = tf.make(
{
1,
},
{
1,
});
EValue input_ev(input_tensor);
EValue output_ev(output_tensor);
std::array<EValue*, 2> args = {&input_ev, &output_ev};
// Check for invalid number of dimensions should fail without stack overflow.
EXPECT_EQ(executor.prepare_args(args.data()), Error::InvalidArgument);
}
12 changes: 12 additions & 0 deletions backends/xnnpack/test/targets.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@fbsource//xplat/executorch/backends/xnnpack/third-party:third_party_libs.bzl", "third_party_dep")
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

def define_common_targets():
Expand All @@ -17,3 +18,14 @@ def define_common_targets():
"//executorch/backends/xnnpack:dynamic_quant_utils",
],
)

runtime.cxx_test(
name = "xnnexecutor_test",
srcs = ["runtime/test_xnnexecutor.cpp"],
deps = [
third_party_dep("XNNPACK"),
"//executorch/runtime/core/exec_aten/testing_util:tensor_util",
"//executorch/runtime/core/exec_aten/util:scalar_type_util",
"//executorch/backends/xnnpack:xnnpack_backend",
],
)