// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include "core/common/exceptions.h" // Setup the infrastructure to throw ORT exceptions so they're caught by existing handlers. template class SafeIntExceptionHandler; template <> class SafeIntExceptionHandler { public: [[noreturn]] static void SafeIntOnOverflow() { ORT_THROW("Integer overflow"); } [[noreturn]] static void SafeIntOnDivZero() { ORT_THROW("Divide by zero"); } }; #define SAFEINT_EXCEPTION_HANDLER_CPP 1 #define SafeIntDefaultExceptionHandler SafeIntExceptionHandler #if defined(__GNUC__) #include "onnxruntime_config.h" #pragma GCC diagnostic push #ifdef HAS_UNUSED_BUT_SET_PARAMETER #pragma GCC diagnostic ignored "-Wunused-but-set-parameter" #endif #endif #include "SafeInt.hpp" #if defined(__GNUC__) #pragma GCC diagnostic pop #endif #include namespace onnxruntime { template using remove_cvref_t = std::remove_cv_t>; template inline constexpr bool is_supported_integer_v = std::is_integral_v> && !std::is_same_v, bool>; //------------------------------------------------------------------------------ // Safe multiplication of two or more integer values into an explicit result type R. // Throws OnnxRuntimeException on overflow. //------------------------------------------------------------------------------ template [[nodiscard]] R SafeMul(T a, U b, Rest... rest) { static_assert(is_supported_integer_v, "SafeMul requires an integral result type (excluding bool)"); static_assert(is_supported_integer_v && is_supported_integer_v, "SafeMul requires integral operand types (excluding bool)"); static_assert((is_supported_integer_v && ...), "SafeMul requires integral operand types (excluding bool)"); // SafeMultiply(T, U, T&) requires the first argument and result to share // the same type. Cast the first operand to R so the result is directly in R. R cast_a{}; if (!SafeCast(a, cast_a)) { SafeIntDefaultExceptionHandler::SafeIntOnOverflow(); } R result{}; if (!SafeMultiply(cast_a, b, result)) { SafeIntDefaultExceptionHandler::SafeIntOnOverflow(); } if constexpr (sizeof...(rest) > 0) { return SafeMul(result, rest...); } else { return result; } } } // namespace onnxruntime