Skip to content

[clang][bytecode] Check composite bitcasts for indeterminate bits #118988

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

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions clang/lib/AST/ByteCode/BitcastBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ BitcastBuffer::copyBits(Bits BitOffset, Bits BitWidth, Bits FullBitWidth,
}

bool BitcastBuffer::allInitialized() const {
Bits Sum;
for (BitRange BR : InitializedBits)
Sum += BR.size();

return Sum == FinalBitSize;
return rangeInitialized(Bits::zero(), FinalBitSize);
}

void BitcastBuffer::markInitialized(Bits Offset, Bits Length) {
Expand Down Expand Up @@ -111,6 +107,34 @@ void BitcastBuffer::markInitialized(Bits Offset, Bits Length) {
#endif
}

bool BitcastBuffer::rangeInitialized(Bits Offset, Bits Length) const {
if (Length.isZero())
return true;

BitRange Range(Offset, Offset + Length - Bits(1));
Bits Sum;
bool FoundStart = false;
for (BitRange BR : InitializedBits) {
if (FoundStart) {
if (BR.contains(Range.End)) {
Sum += (Range.End - BR.Start + Bits(1));
break;
}

// Else, BR is completely inside Range.
Sum += BR.size();
}
if (BR.contains(Range.Start)) {
Sum += (BR.End - Range.Start + Bits(1));
FoundStart = true;
}
}

// Note that Sum can be larger than Range, e.g. when Range is fully
// contained in one range.
return Sum >= Range.size();
}

#if 0
template<typename T>
static std::string hex(T t) {
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ByteCode/BitcastBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ struct Bytes {
Bits toBits() const { return Bits(N * 8); }
};

/// A bit range. Both Start and End are inclusive.
struct BitRange {
Bits Start;
Bits End;

BitRange(Bits Start, Bits End) : Start(Start), End(End) {}
Bits size() const { return End - Start + Bits(1); }
bool operator<(BitRange Other) const { return Start.N < Other.Start.N; }

bool contains(Bits B) { return Start <= B && End >= B; }
};

/// Track what bits have been initialized to known values and which ones
Expand All @@ -85,6 +88,7 @@ struct BitcastBuffer {
/// Marks the bits in the given range as initialized.
/// FIXME: Can we do this automatically in pushData()?
void markInitialized(Bits Start, Bits Length);
bool rangeInitialized(Bits Offset, Bits Length) const;

/// Push \p BitWidth bits at \p BitOffset from \p In into the buffer.
/// \p TargetEndianness is the endianness of the target we're compiling for.
Expand Down
33 changes: 26 additions & 7 deletions clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,10 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
if (BitWidth.isZero())
return true;

if (!P.isInitialized()) {
assert(false && "Implement uninitialized value tracking");
return ReturnOnUninit;
}
// Bits will be left uninitialized and diagnosed when reading.
if (!P.isInitialized())
return true;

assert(P.isInitialized());
if (T == PT_Ptr) {
assert(P.getType()->isNullPtrType());
// Clang treats nullptr_t has having NO bits in its value
Expand All @@ -262,6 +260,7 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
return true;
}

assert(P.isInitialized());
auto Buff =
std::make_unique<std::byte[]>(ObjectReprChars.getQuantity());
// Work around floating point types that contain unused padding bytes.
Expand Down Expand Up @@ -355,10 +354,11 @@ bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
ToPtr, S.getContext(), Buffer.size(),
[&](const Pointer &P, PrimType T, Bits BitOffset,
bool PackedBools) -> bool {
CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(P.getType());
QualType PtrType = P.getType();
CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(PtrType);
Bits FullBitWidth = Bits(ASTCtx.toBits(ObjectReprChars));
if (T == PT_Float) {
const auto &Semantics = ASTCtx.getFloatTypeSemantics(P.getType());
const auto &Semantics = ASTCtx.getFloatTypeSemantics(PtrType);
Bits NumBits = Bits(llvm::APFloatBase::getSizeInBits(Semantics));
assert(NumBits.isFullByte());
assert(NumBits.getQuantity() <= FullBitWidth.getQuantity());
Expand All @@ -382,6 +382,25 @@ bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
else
BitWidth = FullBitWidth;

// If any of the bits are uninitialized, we need to abort unless the
// target type is std::byte or unsigned char.
bool Initialized = Buffer.rangeInitialized(BitOffset, BitWidth);
if (!Initialized) {
if (!PtrType->isStdByteType() &&
!PtrType->isSpecificBuiltinType(BuiltinType::UChar) &&
!PtrType->isSpecificBuiltinType(BuiltinType::Char_U)) {
const Expr *E = S.Current->getExpr(OpPC);
S.FFDiag(E, diag::note_constexpr_bit_cast_indet_dest)
<< PtrType << S.getLangOpts().CharIsSigned
<< E->getSourceRange();

return false;
}
llvm::errs() << "Not all initialized\n";
return true;
}
llvm::errs() << "All initialized.\n";

auto Memory = Buffer.copyBits(BitOffset, BitWidth, FullBitWidth,
TargetEndianness);
if (llvm::sys::IsBigEndianHost)
Expand Down
31 changes: 23 additions & 8 deletions clang/test/AST/ByteCode/builtin-bit-cast-bitfields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct bytes {

constexpr unsigned char operator[](size_t index) {
if (index < N)
return d[index];
return d[index]; // expected-note {{read of uninitialized object}}
return -1;
}
};
Expand Down Expand Up @@ -141,11 +141,11 @@ namespace BitFields {
// expected-note {{indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte'; 'byte' is invalid}}

struct M {
// ref-note@+1 {{subobject declared here}}
// expected-note@+1 {{subobject declared here}}
unsigned char mem[sizeof(BF)];
};
// ref-error@+2 {{initialized by a constant expression}}
// ref-note@+1 {{not initialized}}
// expected-error@+2 {{initialized by a constant expression}}
// expected-note@+1 {{not initialized}}
constexpr M m = bit_cast<M>(bf);

constexpr auto f = []() constexpr {
Expand All @@ -156,8 +156,8 @@ namespace BitFields {

static_assert(f()[0] + f()[1] + f()[2] == 0xc0 + 0xff + 0xee);
{
// ref-error@+2 {{initialized by a constant expression}}
// ref-note@+1 {{read of uninitialized object is not allowed in a constant expression}}
// expected-error@+2 {{initialized by a constant expression}}
// expected-note@+1 {{in call to}}
constexpr auto _bad = f()[3];
}

Expand All @@ -173,8 +173,8 @@ namespace BitFields {
};
static_assert(g().s0 + g().s1 + g().b0 + g().b1 == 0xc0 + 0xff + 0xe + 0xe);
{
// ref-error@+2 {{initialized by a constant expression}}
// ref-note@+1 {{read of uninitialized object is not allowed in a constant expression}}
// expected-error@+2 {{initialized by a constant expression}}
// expected-note@+1 {{read of uninitialized object is not allowed in a constant expression}}
constexpr auto _bad = g().b2;
}
}
Expand Down Expand Up @@ -457,4 +457,19 @@ namespace IndeterminateBits {
};
constexpr unsigned char B = __builtin_bit_cast(unsigned char, S2{3});
static_assert(B == (LITTLE_END ? 3 : 192));



struct S3 {
unsigned a : 13;
unsigned : 17;
unsigned b : 2;
};

struct D {
unsigned a;
};
constexpr D s = __builtin_bit_cast(D, S3{12, 3}); // expected-error {{must be initialized by a constant expression}} \
// expected-note {{indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte'; 'unsigned int' is invalid}}

}
Loading