-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][Interp] Fix diagnosing uninitialized nested union fields #102824
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesWe were calling initialize() unconditionally when copying the union. Full diff: https://github.com/llvm/llvm-project/pull/102824.diff 3 Files Affected:
diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index 89ac6938931133..c65d3789333852 100644
--- a/clang/lib/AST/Interp/Interp.cpp
+++ b/clang/lib/AST/Interp/Interp.cpp
@@ -125,12 +125,17 @@ static bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
if (Ptr.isActive())
return true;
+ assert(Ptr.isField() && Ptr.getField());
+
Pointer U = Ptr.getBase();
Pointer C = Ptr;
while (!U.isRoot() && U.inUnion() && !U.isActive()) {
- C = U;
+ if (U.getField())
+ C = U;
U = U.getBase();
}
+ assert(C.isField());
+
// Get the inactive field descriptor.
const FieldDecl *InactiveField = C.getField();
assert(InactiveField);
diff --git a/clang/lib/AST/Interp/InterpBuiltin.cpp b/clang/lib/AST/Interp/InterpBuiltin.cpp
index 1841a2a4714d89..c3370e2e5286e0 100644
--- a/clang/lib/AST/Interp/InterpBuiltin.cpp
+++ b/clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -1635,7 +1635,58 @@ bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC,
return true;
}
-bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest) {
+static bool copyComposite(InterpState &S, CodePtr OpPC, const Pointer &Src,
+ Pointer &Dest, bool Activate);
+static bool copyRecord(InterpState &S, CodePtr OpPC, const Pointer &Src,
+ Pointer &Dest, bool Activate = false) {
+ [[maybe_unused]] const Descriptor *SrcDesc = Src.getFieldDesc();
+ const Descriptor *DestDesc = Dest.getFieldDesc();
+
+ auto copyField = [&](const Record::Field &F, bool Activate) -> bool {
+ Pointer DestField = Dest.atField(F.Offset);
+ if (std::optional<PrimType> FT = S.Ctx.classify(F.Decl->getType())) {
+ TYPE_SWITCH(*FT, {
+ DestField.deref<T>() = Src.atField(F.Offset).deref<T>();
+ if (Src.atField(F.Offset).isInitialized())
+ DestField.initialize();
+ if (Activate)
+ DestField.activate();
+ });
+ return true;
+ }
+ // Composite field.
+ return copyComposite(S, OpPC, Src.atField(F.Offset), DestField, Activate);
+ };
+
+ assert(SrcDesc->isRecord());
+ assert(SrcDesc->ElemRecord == DestDesc->ElemRecord);
+ const Record *R = DestDesc->ElemRecord;
+ for (const Record::Field &F : R->fields()) {
+ if (R->isUnion()) {
+ // For unions, only copy the active field.
+ const Pointer &SrcField = Src.atField(F.Offset);
+ if (SrcField.isActive()) {
+ if (!copyField(F, /*Activate=*/true))
+ return false;
+ }
+ } else {
+ if (!copyField(F, Activate))
+ return false;
+ }
+ }
+
+ for (const Record::Base &B : R->bases()) {
+ Pointer DestBase = Dest.atField(B.Offset);
+ if (!copyRecord(S, OpPC, Src.atField(B.Offset), DestBase, Activate))
+ return false;
+ }
+
+ Dest.initialize();
+ return true;
+}
+
+static bool copyComposite(InterpState &S, CodePtr OpPC, const Pointer &Src,
+ Pointer &Dest, bool Activate = false) {
assert(Src.isLive() && Dest.isLive());
[[maybe_unused]] const Descriptor *SrcDesc = Src.getFieldDesc();
@@ -1657,44 +1708,14 @@ bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest) {
return true;
}
- if (DestDesc->isRecord()) {
- auto copyField = [&](const Record::Field &F, bool Activate) -> bool {
- Pointer DestField = Dest.atField(F.Offset);
- if (std::optional<PrimType> FT = S.Ctx.classify(F.Decl->getType())) {
- TYPE_SWITCH(*FT, {
- DestField.deref<T>() = Src.atField(F.Offset).deref<T>();
- DestField.initialize();
- if (Activate)
- DestField.activate();
- });
- return true;
- }
- return Invalid(S, OpPC);
- };
-
- assert(SrcDesc->isRecord());
- assert(SrcDesc->ElemRecord == DestDesc->ElemRecord);
- const Record *R = DestDesc->ElemRecord;
- for (const Record::Field &F : R->fields()) {
- if (R->isUnion()) {
- // For unions, only copy the active field.
- const Pointer &SrcField = Src.atField(F.Offset);
- if (SrcField.isActive()) {
- if (!copyField(F, /*Activate=*/true))
- return false;
- }
- } else {
- if (!copyField(F, /*Activate=*/false))
- return false;
- }
- }
- return true;
- }
-
- // FIXME: Composite types.
-
+ if (DestDesc->isRecord())
+ return copyRecord(S, OpPC, Src, Dest, Activate);
return Invalid(S, OpPC);
}
+bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest) {
+ return copyComposite(S, OpPC, Src, Dest);
+}
+
} // namespace interp
} // namespace clang
diff --git a/clang/test/AST/Interp/unions.cpp b/clang/test/AST/Interp/unions.cpp
index 996d29e143fe2c..a8db0597ec8e36 100644
--- a/clang/test/AST/Interp/unions.cpp
+++ b/clang/test/AST/Interp/unions.cpp
@@ -361,7 +361,7 @@ namespace CopyCtor {
namespace UnionInBase {
struct Base {
- int y;
+ int y; // both-note {{subobject declared here}}
};
struct A : Base {
int x;
@@ -380,5 +380,28 @@ namespace UnionInBase {
}
static_assert(read_wrong_member_indirect() == 1); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
+ constexpr int read_uninitialized() {
+ B b = {.b = 1};
+ int *p = &b.a.y;
+ b.a.x = 1;
+ return *p; // both-note {{read of uninitialized object}}
+ }
+ static_assert(read_uninitialized() == 0); // both-error {{constant}} \
+ // both-note {{in call}}
+ constexpr int write_uninitialized() {
+ B b = {.b = 1};
+ int *p = &b.a.y;
+ b.a.x = 1;
+ *p = 1;
+ return *p;
+ }
+
+ constexpr B return_uninit() {
+ B b = {.b = 1};
+ b.a.x = 2;
+ return b;
+ }
+ constexpr B uninit = return_uninit(); // both-error {{constant expression}} \
+ // both-note {{subobject 'y' is not initialized}}
}
#endif
|
618e8c1
to
d996308
Compare
We were calling initialize() unconditionally when copying the union.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We were calling initialize() unconditionally when copying the union.