Skip to content

Commit

Permalink
[AST] Fix handling of long double and bool in __builtin_bit_cast
Browse files Browse the repository at this point in the history
On x86, long double has 6 unused trailing bytes. This patch changes the
constant evaluator to treat them as though they were padding bytes, so reading
from them results in an indeterminate value, and nothing is written for them.
Also, fix a similar bug with bool, but instead of treating the unused bits as
padding, enforce that they're zero.

Differential revision: https://reviews.llvm.org/D76323
  • Loading branch information
epilk committed Sep 2, 2020
1 parent e0e7eb2 commit 9523cf0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticASTKinds.td
Expand Up @@ -298,6 +298,8 @@ def note_constexpr_bit_cast_invalid_subtype : Note<
def note_constexpr_bit_cast_indet_dest : Note<
"indeterminate value can only initialize an object of type 'unsigned char'"
"%select{, 'char',|}1 or 'std::byte'; %0 is invalid">;
def note_constexpr_bit_cast_unrepresentable_value : Note<
"value %1 cannot be represented in type %0">;
def note_constexpr_pseudo_destructor : Note<
"pseudo-destructor call is not permitted in constant expressions "
"until C++20">;
Expand Down
42 changes: 39 additions & 3 deletions clang/lib/AST/ExprConstant.cpp
Expand Up @@ -6627,9 +6627,15 @@ class APValueToBufferConverter {
}

bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) {
CharUnits Width = Info.Ctx.getTypeSizeInChars(Ty);
SmallVector<unsigned char, 8> Bytes(Width.getQuantity());
llvm::StoreIntToMemory(Val, &*Bytes.begin(), Width.getQuantity());
APSInt AdjustedVal = Val;
unsigned Width = AdjustedVal.getBitWidth();
if (Ty->isBooleanType()) {
Width = Info.Ctx.getTypeSize(Ty);
AdjustedVal = AdjustedVal.extend(Width);
}

SmallVector<unsigned char, 8> Bytes(Width / 8);
llvm::StoreIntToMemory(AdjustedVal, &*Bytes.begin(), Width / 8);
Buffer.writeObject(Offset, Bytes);
return true;
}
Expand Down Expand Up @@ -6670,6 +6676,13 @@ class BufferToAPValueConverter {
return None;
}

llvm::NoneType unrepresentableValue(QualType Ty, const APSInt &Val) {
Info.FFDiag(BCE->getBeginLoc(),
diag::note_constexpr_bit_cast_unrepresentable_value)
<< Ty << Val.toString(/*Radix=*/10);
return None;
}

Optional<APValue> visit(const BuiltinType *T, CharUnits Offset,
const EnumType *EnumSugar = nullptr) {
if (T->isNullPtrType()) {
Expand All @@ -6680,6 +6693,20 @@ class BufferToAPValueConverter {
}

CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T);

// Work around floating point types that contain unused padding bytes. This
// is really just `long double` on x86, which is the only fundamental type
// with padding bytes.
if (T->isRealFloatingType()) {
const llvm::fltSemantics &Semantics =
Info.Ctx.getFloatTypeSemantics(QualType(T, 0));
unsigned NumBits = llvm::APFloatBase::getSizeInBits(Semantics);
assert(NumBits % 8 == 0);
CharUnits NumBytes = CharUnits::fromQuantity(NumBits / 8);
if (NumBytes != SizeOf)
SizeOf = NumBytes;
}

SmallVector<uint8_t, 8> Bytes;
if (!Buffer.readObject(Offset, SizeOf, Bytes)) {
// If this is std::byte or unsigned char, then its okay to store an
Expand All @@ -6704,6 +6731,15 @@ class BufferToAPValueConverter {

if (T->isIntegralOrEnumerationType()) {
Val.setIsSigned(T->isSignedIntegerOrEnumerationType());

unsigned IntWidth = Info.Ctx.getIntWidth(QualType(T, 0));
if (IntWidth != Val.getBitWidth()) {
APSInt Truncated = Val.trunc(IntWidth);
if (Truncated.extend(Val.getBitWidth()) != Val)
return unrepresentableValue(QualType(T, 0), Val);
Val = Truncated;
}

return APValue(Val);
}

Expand Down
66 changes: 66 additions & 0 deletions clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
Expand Up @@ -23,6 +23,10 @@ static_assert(sizeof(long long) == 8);
template <class To, class From>
constexpr To bit_cast(const From &from) {
static_assert(sizeof(To) == sizeof(From));
// expected-note@+9 {{cannot be represented in type 'bool'}}
#ifdef __x86_64
// expected-note@+7 {{or 'std::byte'; '__int128' is invalid}}
#endif
#ifdef __CHAR_UNSIGNED__
// expected-note@+4 2 {{indeterminate value can only initialize an object of type 'unsigned char', 'char', or 'std::byte'; 'signed char' is invalid}}
#else
Expand Down Expand Up @@ -397,3 +401,65 @@ union IdentityInUnion {
};
constexpr IdentityInUnion identity3a = {42};
constexpr unsigned char identity3b = __builtin_bit_cast(unsigned char, identity3a.n);

namespace test_bool {

constexpr bool test_bad_bool = bit_cast<bool>('A'); // expected-error {{must be initialized by a constant expression}} expected-note{{in call}}

static_assert(round_trip<signed char>(true), "");
static_assert(round_trip<unsigned char>(false), "");
static_assert(round_trip<bool>(false), "");

static_assert(round_trip<bool>((char)0), "");
static_assert(round_trip<bool>((char)1), "");
}

namespace test_long_double {
#ifdef __x86_64
constexpr __int128_t test_cast_to_int128 = bit_cast<__int128_t>((long double)0); // expected-error{{must be initialized by a constant expression}} expected-note{{in call}}

constexpr long double ld = 3.1425926539;

struct bytes {
unsigned char d[16];
};

static_assert(round_trip<bytes>(ld), "");

static_assert(round_trip<long double>(10.0L));

constexpr bool f(bool read_uninit) {
bytes b = bit_cast<bytes>(ld);
unsigned char ld_bytes[10] = {
0x0, 0x48, 0x9f, 0x49, 0xf0,
0x3c, 0x20, 0xc9, 0x0, 0x40,
};

for (int i = 0; i != 10; ++i)
if (ld_bytes[i] != b.d[i])
return false;

if (read_uninit && b.d[10]) // expected-note{{read of uninitialized object is not allowed in a constant expression}}
return false;

return true;
}

static_assert(f(/*read_uninit=*/false), "");
static_assert(f(/*read_uninit=*/true), ""); // expected-error{{static_assert expression is not an integral constant expression}} expected-note{{in call to 'f(true)'}}

constexpr bytes ld539 = {
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0xc0, 0x86,
0x8, 0x40, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
};

constexpr long double fivehundredandthirtynine = 539.0;

static_assert(bit_cast<long double>(ld539) == fivehundredandthirtynine, "");

#else
static_assert(round_trip<__int128_t>(34.0L));
#endif
}

0 comments on commit 9523cf0

Please sign in to comment.