-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][Interp] Fix value truncation when casting int128 to smaller size #67961
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
@llvm/pr-subscribers-clang ChangesBefore this patch, we would run into an assertion in Full diff: https://github.com/llvm/llvm-project/pull/67961.diff 2 Files Affected:
diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index b2b367f30c238fe..f67b15dbc9b31ac 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -55,14 +55,14 @@ template <bool Signed> class IntegralAP final {
bool operator<=(IntegralAP RHS) const { return V <= RHS.V; }
explicit operator bool() const { return !V.isZero(); }
- explicit operator int8_t() const { return V.getSExtValue(); }
- explicit operator uint8_t() const { return V.getZExtValue(); }
- explicit operator int16_t() const { return V.getSExtValue(); }
- explicit operator uint16_t() const { return V.getZExtValue(); }
- explicit operator int32_t() const { return V.getSExtValue(); }
- explicit operator uint32_t() const { return V.getZExtValue(); }
- explicit operator int64_t() const { return V.getSExtValue(); }
- explicit operator uint64_t() const { return V.getZExtValue(); }
+ explicit operator int8_t() const { return truncateCast<int8_t>(V); }
+ explicit operator uint8_t() const { return truncateCast<uint8_t>(V); }
+ explicit operator int16_t() const { return truncateCast<int16_t>(V); }
+ explicit operator uint16_t() const { return truncateCast<uint16_t>(V); }
+ explicit operator int32_t() const { return truncateCast<int32_t>(V); }
+ explicit operator uint32_t() const { return truncateCast<uint32_t>(V); }
+ explicit operator int64_t() const { return truncateCast<int64_t>(V); }
+ explicit operator uint64_t() const { return truncateCast<uint64_t>(V); }
template <typename T> static IntegralAP from(T Value, unsigned NumBits = 0) {
assert(NumBits > 0);
@@ -210,7 +210,6 @@ template <bool Signed> class IntegralAP final {
}
static bool comp(IntegralAP A, IntegralAP *R) {
- assert(false);
*R = IntegralAP(~A.V);
return false;
}
@@ -249,6 +248,11 @@ template <bool Signed> class IntegralAP final {
R->V = A.V - B.V;
return false; // Success!
}
+
+ template <typename T> static T truncateCast(const APSInt &V) {
+ return std::is_signed_v<T> ? V.trunc(sizeof(T) * 8).getSExtValue()
+ : V.trunc(sizeof(T) * 8).getZExtValue();
+ }
};
template <bool Signed>
diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index eca0e4c2cbd26f1..65d211422ddac92 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -52,6 +52,9 @@ namespace i128 {
constexpr int128_t Two = (int128_t)1 << 1ul;
static_assert(Two == 2, "");
+ constexpr uint128_t AllOnes = ~static_cast<uint128_t>(0);
+ static_assert(AllOnes == UINT128_MAX, "");
+
#if __cplusplus >= 201402L
template <typename T>
constexpr T CastFrom(__int128_t A) {
@@ -67,6 +70,12 @@ namespace i128 {
static_assert(CastFrom<double>(12) == 12, "");
static_assert(CastFrom<long double>(12) == 12, "");
+ static_assert(CastFrom<char>(AllOnes) == -1, "");
+ static_assert(CastFrom<unsigned char>(AllOnes) == 0xFF, "");
+ static_assert(CastFrom<long>(AllOnes) == -1, "");
+ static_assert(CastFrom<unsigned short>(AllOnes) == 0xFFFF, "");
+ static_assert(CastFrom<int>(AllOnes) == -1, "");
+
template <typename T>
constexpr __int128 CastTo(T A) {
int128_t B = (int128_t)A;
|
clang/lib/AST/Interp/IntegralAP.h
Outdated
@@ -249,6 +248,11 @@ template <bool Signed> class IntegralAP final { | |||
R->V = A.V - B.V; | |||
return false; // Success! | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this template forward declared? Either way, I think as none of the calls are dependent, this ends up being UB (As the lookup 'changes' between the declaration and the call).
All that to say: I think you just have to move this definition above its uses.
✅ With the latest revision this PR passed the C/C++ code formatter. |
Before this patch, we would run into an assertion in
APInt::get{S,Z}ExtValue()
because theAllOnes
value had more than 64 active bits.