onnxruntime
3454f86e - Fix BitShift UB when shift amount >= bit width (#28272)

Commit
62 days ago
Fix BitShift UB when shift amount >= bit width (#28272) ### Description Shifting by >= the bit width of an unsigned type is undefined behavior in C++. On x86-64, the hardware masks 64-bit shift amounts to 6 bits, so `x >> 64` silently becomes `x >> 0`, returning the original value instead of 0. Added `SafeShiftLeft`/`SafeShiftRight` helpers that return 0 when `shift >= sizeof(T) * 8`, applied across all three broadcast code paths (scalar-X, scalar-Y, element-wise). ```cpp template <typename T> inline T SafeShiftRight(T value, T shift) { return shift >= sizeof(T) * 8 ? T{0} : value >> shift; } ``` Added tests covering: - Shift by exact bit width (32, 64) for `uint32_t` and `uint64_t` - Shift by more than bit width (65, 128) - All three broadcast paths (scalar-X, scalar-Y, element-wise) - New tests are excluded for DirectML EP, which has the same hardware-level shift masking behavior ### Motivation and Context `BitShift` with `direction="RIGHT"` on `uint64` inputs with shift amount 64 returns the original values instead of zeros. Reproduces with `CPUExecutionProvider` and `ORT_DISABLE_ALL` (constant folding masks the bug under `ORT_ENABLE_ALL`). --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Author
Parents
Loading