[clang] dcea10c - Fix miscompile with [[no_unique_address]] struct fields.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 29 13:08:51 PDT 2022


Author: Richard Smith
Date: 2022-06-29T13:08:40-07:00
New Revision: dcea10c3c6ac963387e53f918c3616b64cb82e8d

URL: https://github.com/llvm/llvm-project/commit/dcea10c3c6ac963387e53f918c3616b64cb82e8d
DIFF: https://github.com/llvm/llvm-project/commit/dcea10c3c6ac963387e53f918c3616b64cb82e8d.diff

LOG: Fix miscompile with [[no_unique_address]] struct fields.

If a zero-sized field has a non-trivial initializer, it should prevent
the overall struct initialization from being folded to a constant during
IR generation. Don't just ignore zero-sized fields entirely in IR
constant emission.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprConstant.cpp
    clang/test/CodeGenCXX/no-unique-address.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index 91fdb88d6bb7a..b83a874432508 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -703,8 +703,8 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) {
         !declaresSameEntity(ILE->getInitializedFieldInUnion(), Field))
       continue;
 
-    // Don't emit anonymous bitfields or zero-sized fields.
-    if (Field->isUnnamedBitfield() || Field->isZeroSize(CGM.getContext()))
+    // Don't emit anonymous bitfields.
+    if (Field->isUnnamedBitfield())
       continue;
 
     // Get the initializer.  A struct can include fields without initializers,
@@ -715,6 +715,14 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) {
     if (Init && isa<NoInitExpr>(Init))
       continue;
 
+    // Zero-sized fields are not emitted, but their initializers may still
+    // prevent emission of this struct as a constant.
+    if (Field->isZeroSize(CGM.getContext())) {
+      if (Init->HasSideEffects(CGM.getContext()))
+        return false;
+      continue;
+    }
+
     // When emitting a DesignatedInitUpdateExpr, a nested InitListExpr
     // represents additional overwriting of our current constant value, and not
     // a new constant to emit independently.

diff  --git a/clang/test/CodeGenCXX/no-unique-address.cpp b/clang/test/CodeGenCXX/no-unique-address.cpp
index 8ecbd69aa54bb..c0678aca5b0c8 100644
--- a/clang/test/CodeGenCXX/no-unique-address.cpp
+++ b/clang/test/CodeGenCXX/no-unique-address.cpp
@@ -93,3 +93,14 @@ int loadWhereLaterDeclaredFieldHasLowerOffset(LaterDeclaredFieldHasLowerOffset &
 // Note, never emit TBAA for zero-size fields.
 // CHECK-OPT: ![[TBAA_AB]] = !{![[TBAA_A:[0-9]*]], ![[TBAA_INT:[0-9]*]], i64 4}
 // CHECK-OPT: ![[TBAA_A]] = !{!"_ZTS32LaterDeclaredFieldHasLowerOffset", ![[TBAA_INT]], i64 0, ![[TBAA_INT]], i64 4}
+
+struct NonTrivialInit {
+  NonTrivialInit();
+};
+struct HasZeroSizedFieldWithNonTrivialInit {
+  int a;
+  [[no_unique_address]] NonTrivialInit b;
+};
+HasZeroSizedFieldWithNonTrivialInit testHasZeroSizedFieldWithNonTrivialInit = {.a = 1};
+// CHECK-LABEL: define {{.*}}cxx_global_var_init
+// CHECK: call {{.*}}@_ZN14NonTrivialInitC1Ev({{.*}}@testHasZeroSizedFieldWithNonTrivialInit


        


More information about the cfe-commits mailing list