Skip to content

[clang][bytecode] Initialize bases when bitcasting #117179

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 4, 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
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/BitcastBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct Bits {
size_t getOffsetInByte() const { return N % 8; }
bool isFullByte() const { return N % 8 == 0; }
bool nonZero() const { return N != 0; }
bool isZero() const { return N == 0; }

Bits operator-(Bits Other) { return Bits(N - Other.N); }
Bits operator+(Bits Other) { return Bits(N + Other.N); }
Expand Down
38 changes: 24 additions & 14 deletions clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ static bool enumerateData(const Pointer &P, const Context &Ctx, Bits Offset,
bool Ok = true;

for (const Record::Field &Fi : R->fields()) {
if (Fi.isUnnamedBitField())
continue;
Pointer Elem = P.atField(Fi.Offset);
Bits BitOffset =
Offset + Bits(Layout.getFieldOffset(Fi.Decl->getFieldIndex()));
Expand All @@ -138,6 +140,10 @@ static bool enumerateData(const Pointer &P, const Context &Ctx, Bits Offset,
Layout.getBaseClassOffset(cast<CXXRecordDecl>(B.Decl));
Bits BitOffset = Offset + Bits(Ctx.getASTContext().toBits(ByteOffset));
Ok = Ok && enumerateData(Elem, Ctx, BitOffset, BitsToRead, F);
// FIXME: We should only (need to) do this when bitcasting OUT of the
// buffer, not when copying data into it.
if (Ok)
Elem.initialize();
}

return Ok;
Expand Down Expand Up @@ -229,19 +235,29 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
FromPtr, Ctx, Buffer.size(),
[&](const Pointer &P, PrimType T, Bits BitOffset,
bool PackedBools) -> bool {
// if (!P.isInitialized()) {
// assert(false && "Implement uninitialized value tracking");
// return ReturnOnUninit;
// }
CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(P.getType());
Bits BitWidth = Bits(ASTCtx.toBits(ObjectReprChars));
Bits FullBitWidth = BitWidth;

if (const FieldDecl *FD = P.getField(); FD && FD->isBitField()) {
BitWidth = Bits(std::min(FD->getBitWidthValue(ASTCtx),
(unsigned)FullBitWidth.getQuantity()));
} else if (T == PT_Bool && PackedBools)
BitWidth = Bits(1);

// assert(P.isInitialized());
if (BitWidth.isZero())
return true;

if (!P.isInitialized()) {
assert(false && "Implement uninitialized value tracking");
return ReturnOnUninit;
}

assert(P.isInitialized());
// nullptr_t is a PT_Ptr for us, but it's still not std::is_pointer_v.
if (T == PT_Ptr)
assert(false && "Implement casting to pointer types");

CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(P.getType());
Bits BitWidth = Bits(ASTCtx.toBits(ObjectReprChars));
Bits FullBitWidth = BitWidth;
auto Buff =
std::make_unique<std::byte[]>(ObjectReprChars.getQuantity());
// Work around floating point types that contain unused padding bytes.
Expand All @@ -260,12 +276,6 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
swapBytes(Buff.get(), NumBits.roundToBytes());

} else {
if (const FieldDecl *FD = P.getField(); FD && FD->isBitField())
BitWidth = Bits(std::min(FD->getBitWidthValue(ASTCtx),
(unsigned)FullBitWidth.getQuantity()));
else if (T == PT_Bool && PackedBools)
BitWidth = Bits(1);

BITCAST_TYPE_SWITCH(T, { P.deref<T>().bitcastToMemory(Buff.get()); });

if (llvm::sys::IsBigEndianHost)
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Record final {
unsigned Offset;
const Descriptor *Desc;
bool isBitField() const { return Decl->isBitField(); }
bool isUnnamedBitField() const { return Decl->isUnnamedBitField(); }
};

/// Describes a base class.
Expand Down
5 changes: 2 additions & 3 deletions clang/test/AST/ByteCode/builtin-bit-cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,8 @@ void test_record() {
static_assert(t4 == tuple4{1, 2, 3, 4});
static_assert(check_round_trip<tuple4>(b));

/// FIXME: We need to initialize the base pointers in the pointer we're bitcasting to.
// constexpr auto b2 = bit_cast<bases>(t4);
// static_assert(t4 == b2);
constexpr auto b2 = bit_cast<bases>(t4);
static_assert(t4 == b2);
}

void test_partially_initialized() {
Expand Down
Loading