Fix integer modulo by zero crash in CPU EP Mod operator#27833
Merged
hariharans29 merged 2 commits intomainfrom Mar 30, 2026
Merged
Fix integer modulo by zero crash in CPU EP Mod operator#27833hariharans29 merged 2 commits intomainfrom
hariharans29 merged 2 commits intomainfrom
Conversation
Add pre-check for zero values in the divisor tensor for integral types in the Mod operator. Returns an error instead of hitting undefined behavior (SIGFPE / structured exception). - When the divisor is a constant initializer, TryGetConstantInput validates for zeros once at kernel creation time in the constructor. - In Compute, scans non-constant divisors before calling the mod dispatch, skipping the check when the constant was already validated. - Added Mod_int8_by_zero, Mod_int32_by_zero, Mod_int64_by_zero_scalar, and Mod_int32_by_zero_constant_initializer tests. Co-authored-by: edgchen1 <18449977+edgchen1@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/a7664acb-d5cc-4408-9621-8e67b1bc4f23
Copilot created this pull request from a session on behalf of
edgchen1
March 25, 2026 01:24
View session
…xpr, return Status, keep mod_internal contiguous - Merged two template specializations into single template with if constexpr - Changed operator() to return Status using ORT_RETURN_IF - Use InvokeRet<Status> with ORT_THROW_IF_ERROR in constructor and ORT_RETURN_IF_ERROR in Compute - Moved Mod constructor out-of-line after mod_internal namespace to keep the namespace contiguous Co-authored-by: edgchen1 <18449977+edgchen1@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/3fba85ca-1037-479d-8d46-4c1403a914ec
Contributor
Author
edgchen1
approved these changes
Mar 26, 2026
hariharans29
approved these changes
Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add a pre-check for zero values in the divisor tensor for integral types in
Mod. Returns an errorStatusinstead of hitting undefined behavior (SIGFPE / structured exception).element_wise_ops.cc: AddedCheckZeroDivisorImplas a single template struct in themod_internalnamespace usingif constexpr (std::is_integral<T>::value)to guard the check — no-op for non-integer types. The struct'soperator()returnsStatus(viaORT_RETURN_IF) and is dispatched withInvokeRet<Status>. When the divisor is a constant initializer,TryGetConstantInputvalidates for zeros once at kernel creation time in the out-of-line constructor (usingORT_THROW_IF_ERROR), avoiding per-Computeoverhead. Adivisor_is_validated_constant_flag tracks whether the one-time check was performed. InCompute, non-constant divisors are scanned via the type dispatcher (usingORT_RETURN_IF_ERROR) before callingCallModImpl, skipping the check when the constant was already validated. The Mod constructor is defined out-of-line after themod_internalnamespace to keep it contiguous.element_wise_ops_test.cc: AddedMod_int8_by_zero,Mod_int32_by_zero,Mod_int64_by_zero_scalartests covering tensor and scalar divisor cases, plusMod_int32_by_zero_constant_initializerto exercise theTryGetConstantInputconstructor path withis_initializer = true.Motivation and Context
Integer modulo by zero is UB in C++ and causes a hardware exception that crashes the process. Float types produce NaN naturally via
std::fmod, but int8/int16/int32/int64/uint* types do not. This is the same class of issue that was fixed for theDivoperator in #27693, now applied to theModoperator.💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.