Skip to content

[clang][bytecode] Fix comparing the addresses of union members #133852

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
Apr 1, 2025
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
11 changes: 10 additions & 1 deletion clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1070,9 +1070,18 @@ inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
}

if (Pointer::hasSameBase(LHS, RHS)) {
if (LHS.inUnion() && RHS.inUnion()) {
// If the pointers point into a union, things are a little more
// complicated since the offset we save in interp::Pointer can't be used
// to compare the pointers directly.
size_t A = LHS.computeOffsetForComparison();
size_t B = RHS.computeOffsetForComparison();
S.Stk.push<BoolT>(BoolT::from(Fn(Compare(A, B))));
return true;
}

unsigned VL = LHS.getByteOffset();
unsigned VR = RHS.getByteOffset();

// In our Pointer class, a pointer to an array and a pointer to the first
// element in the same array are NOT equal. They have the same Base value,
// but a different Offset. This is a pretty rare case, so we fix this here
Expand Down
33 changes: 33 additions & 0 deletions clang/lib/AST/ByteCode/Pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,39 @@ void Pointer::print(llvm::raw_ostream &OS) const {
}
}

/// Compute an integer that can be used to compare this pointer to
/// another one.
size_t Pointer::computeOffsetForComparison() const {
if (!isBlockPointer())
return Offset;

size_t Result = 0;
Pointer P = *this;
while (!P.isRoot()) {
if (P.isArrayRoot()) {
P = P.getBase();
continue;
}
if (P.isArrayElement()) {
P = P.expand();
Result += (P.getIndex() * P.elemSize());
P = P.getArray();
continue;
}

if (const Record *R = P.getBase().getRecord(); R && R->isUnion()) {
// Direct child of a union - all have offset 0.
P = P.getBase();
continue;
}

Result += P.getInlineDesc()->Offset;
P = P.getBase();
}

return Result;
}

std::string Pointer::toDiagnosticString(const ASTContext &Ctx) const {
if (isZero())
return "nullptr";
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/AST/ByteCode/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class Pointer {
return false;
}
bool inUnion() const {
if (isBlockPointer())
if (isBlockPointer() && asBlockPointer().Base >= sizeof(InlineDescriptor))
return getInlineDesc()->InUnion;
return false;
};
Expand Down Expand Up @@ -727,6 +727,8 @@ class Pointer {
/// Prints the pointer.
void print(llvm::raw_ostream &OS) const;

size_t computeOffsetForComparison() const;

private:
friend class Block;
friend class DeadBlock;
Expand Down
38 changes: 38 additions & 0 deletions clang/test/AST/ByteCode/unions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,41 @@ namespace MoveOrAssignOp {
static_assert(foo());
}
#endif

namespace AddressComparison {
union {
int a;
int c;
} U;
static_assert(__builtin_addressof(U.a) == (void*)__builtin_addressof(U.c));
static_assert(&U.a == &U.c);


struct {
union {
struct {
int a;
int b;
} a;
struct {
int b;
int a;
}b;
} u;
int b;
} S;

static_assert(&S.u.a.a == &S.u.b.b);
static_assert(&S.u.a.b != &S.u.b.b);
static_assert(&S.u.a.b == &S.u.b.b); // both-error {{failed}}


union {
int a[2];
int b[2];
} U2;

static_assert(&U2.a[0] == &U2.b[0]);
static_assert(&U2.a[0] != &U2.b[1]);
static_assert(&U2.a[0] == &U2.b[1]); // both-error {{failed}}
}