From 35052fcbb9b4246743cb57ba80fa1cbf3f7a9911 Mon Sep 17 00:00:00 2001 From: Ryan VanderMeulen Date: Mon, 14 Sep 2026 14:49:50 -0400 Subject: [PATCH] Patch onnx v1.22.0 tensor.h to honour ONNX_NO_EXCEPTIONS onnx v1.22.0 rewrote Tensor::elem_num() and Tensor::size_from_dim() around safe_dim_product() with an error callback that does a raw `throw tensor_error(msg)`, bypassing onnx's ONNX_THROW_EX macro. That breaks any onnxruntime build with exceptions disabled as soon as those headers are included. Route both callbacks through the existing [[noreturn]] throw_tensor_error() helper, which honours ONNX_NO_EXCEPTIONS. This is onnx/onnx#8220 (merged upstream, fixes onnx/onnx#8219), carried in cmake/patches/onnx/onnx.patch and its vcpkg mirror cmake/vcpkg-ports/onnx/binskim.patch until the onnx dependency moves to a release that contains it. --- cmake/patches/onnx/onnx.patch | 26 ++++++++++++++++++++++++++ cmake/vcpkg-ports/onnx/binskim.patch | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/cmake/patches/onnx/onnx.patch b/cmake/patches/onnx/onnx.patch index 8b0068b7b0c80..63b4c3650f43f 100644 --- a/cmake/patches/onnx/onnx.patch +++ b/cmake/patches/onnx/onnx.patch @@ -59,3 +59,29 @@ index 0c271d5..6d64da7 100644 fail_shape_inference( "Unexpected axis value (", axis, +diff --git a/onnx/common/tensor.h b/onnx/common/tensor.h +--- a/onnx/common/tensor.h ++++ b/onnx/common/tensor.h +@@ -51,14 +51,20 @@ + /// if tensor is a scalar, the sizes is empty, but the element number is actually 1. + /// size_from_dim() cannot handle this case, while elem_num() handles it correctly + int64_t elem_num() const { +- return safe_dim_product(sizes_, [](const char* msg) { throw tensor_error(msg); }); ++ return safe_dim_product(sizes_, [](const char* msg) { ++ std::string error_msg(msg); ++ throw_tensor_error(error_msg); ++ }); + } + int64_t size_from_dim(int dim) const { + if (dim < 0) { + dim += static_cast(sizes_.size()); + } + ONNX_ASSERT(dim >= 0 && (size_t)dim < sizes_.size()) +- return safe_dim_product(sizes_.begin() + dim, sizes_.end(), [](const char* msg) { throw tensor_error(msg); }); ++ return safe_dim_product(sizes_.begin() + dim, sizes_.end(), [](const char* msg) { ++ std::string error_msg(msg); ++ throw_tensor_error(error_msg); ++ }); + } + + int32_t elem_type() const { diff --git a/cmake/vcpkg-ports/onnx/binskim.patch b/cmake/vcpkg-ports/onnx/binskim.patch index 8b0068b7b0c80..63b4c3650f43f 100644 --- a/cmake/vcpkg-ports/onnx/binskim.patch +++ b/cmake/vcpkg-ports/onnx/binskim.patch @@ -59,3 +59,29 @@ index 0c271d5..6d64da7 100644 fail_shape_inference( "Unexpected axis value (", axis, +diff --git a/onnx/common/tensor.h b/onnx/common/tensor.h +--- a/onnx/common/tensor.h ++++ b/onnx/common/tensor.h +@@ -51,14 +51,20 @@ + /// if tensor is a scalar, the sizes is empty, but the element number is actually 1. + /// size_from_dim() cannot handle this case, while elem_num() handles it correctly + int64_t elem_num() const { +- return safe_dim_product(sizes_, [](const char* msg) { throw tensor_error(msg); }); ++ return safe_dim_product(sizes_, [](const char* msg) { ++ std::string error_msg(msg); ++ throw_tensor_error(error_msg); ++ }); + } + int64_t size_from_dim(int dim) const { + if (dim < 0) { + dim += static_cast(sizes_.size()); + } + ONNX_ASSERT(dim >= 0 && (size_t)dim < sizes_.size()) +- return safe_dim_product(sizes_.begin() + dim, sizes_.end(), [](const char* msg) { throw tensor_error(msg); }); ++ return safe_dim_product(sizes_.begin() + dim, sizes_.end(), [](const char* msg) { ++ std::string error_msg(msg); ++ throw_tensor_error(error_msg); ++ }); + } + + int32_t elem_type() const {