-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Signed integer overflow in Constraint Elimination pass issue #133154 #133620
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
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-llvm-support Author: 黃國庭 (houngkoungting) ChangesAdjusted the limit check to use MaxNegative Full diff: https://github.com/llvm/llvm-project/pull/133620.diff 1 Files Affected:
diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 519fcc8fde5d5..fe6db8fe14019 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -760,31 +760,44 @@ std::enable_if_t<std::is_signed_v<T>, T> SubOverflow(T X, T Y, T &Result) {
/// Multiply two signed integers, computing the two's complement truncated
/// result, returning true if an overflow occurred.
template <typename T>
-std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) {
+std::enable_if_t<std::is_signed_v<T>, bool> MulOverflow(T X, T Y, T &Result) {
#if __has_builtin(__builtin_mul_overflow)
return __builtin_mul_overflow(X, Y, &Result);
#else
- // Perform the unsigned multiplication on absolute values.
using U = std::make_unsigned_t<T>;
- const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
- const U UY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y);
- const U UResult = UX * UY;
- // Convert to signed.
- const bool IsNegative = (X < 0) ^ (Y < 0);
- Result = IsNegative ? (0 - UResult) : UResult;
-
- // If any of the args was 0, result is 0 and no overflow occurs.
- if (UX == 0 || UY == 0)
+ // Handle zero case
+ if (X == 0 || Y == 0) {
+ Result = 0;
return false;
+ }
- // UX and UY are in [1, 2^n], where n is the number of digits.
- // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for
- // positive) divided by an argument compares to the other.
- if (IsNegative)
- return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY;
- else
- return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY;
+ bool IsNegative = (X < 0) ^ (Y < 0);
+
+ // Safely compute absolute values
+ const U AbsX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
+ const U AbsY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y);
+
+ // Overflow check before actual multiplication
+ constexpr U MaxPositive = static_cast<U>(std::numeric_limits<T>::max());
+ constexpr U MaxNegative = static_cast<U>(std::numeric_limits<T>::max()) + 1;
+
+ // Safe to multiply
+ U AbsResult = AbsX * AbsY;
+ Result = IsNegative ? static_cast<T>(0-AbsResult) : static_cast<T>(AbsResult);
+
+ // Handle INT_MIN * -1 overflow case explicitly
+ if ((X == std::numeric_limits<T>::min() && Y == -1) ||
+ (Y == std::numeric_limits<T>::min() && X == -1)) {
+ return true; // overflow
+ }
+
+ U Limit = IsNegative ? MaxNegative : MaxPositive;
+
+ if (AbsX > Limit / AbsY)
+ return true;
+
+ return false;
#endif
}
|
|
sorry |
Adjusted the limit check to use MaxNegative