llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Steven Noonan (tycho)

<details>
<summary>Changes</summary>

`MicrosoftRecordLayoutBuilder::computeVtorDispSet()` only introduces new 
vtordisp fields when `hasUserDeclaredConstructor() || 
hasUserDeclaredDestructor()`. That follows the documented `/vd1` rule ("classes 
with a user-declared constructor or destructor"). I tested MSVC 19.44.35228 and 
19.51.36231 on x86 and x64, and its actual behavior is narrower:

* **Constructors:** a user-declared constructor triggers vtordisps, *unless*  
the class also declares a default constructor that is explicitly defaulted on 
its first declaration (and not deleted). That suppresses vtordisp introduction 
for *all* of the class's constructors, including user-provided ones such as a 
copy constructor. A default constructor that is defaulted out-of-line, or 
deleted, suppresses nothing.
* **Destructors:** a user-declared destructor triggers vtordisps only if it is 
user-provided. A destructor defaulted on its first declaration does not count.

## Impact

This produces object layouts that silently differ between clang-cl and MSVC 
whenever such a class has virtual bases. Clang allocates vtordisp fields where 
MSVC allocates none, which shifts every later virtual base subobject by at 
least 8 bytes (4 for the vtordisp plus padding).

Real-world example: Botan 3.10's `RSA_PublicKey` has a protected 
`RSA_PublicKey() = default;` alongside user-provided constructors. clang-cl 
computes `sizeof(Botan::RSA_PrivateKey) == 112`, MSVC computes 104, and they 
place the `Private_Key` virtual base at different offsets. A clang-cl-compiled 
program that calls into an MSVC-built Botan DLL through such an object reads a 
garbage vptr and crashes. This shows up as a KeePassXC crash in 
`Botan::Private_Key::private_key_info()` when the KeeShare settings page 
serializes a freshly generated key.

## Scope

I checked vtordisp requirements propagated from base classes against MSVC, and 
they already match, so only the constructor/destructor condition changes. 
`VTableBuilder` and CodeGen read `hasVtorDisp()` from the computed layout, so 
vtordisp thunks and vtordisp initialization in constructors and destructors 
follow the corrected layout automatically. MSVC likewise emits plain 
`this`-adjusting thunks in the affected vftable slots.

## Testing

Six new cases (`JA`–`JF`) in `clang/test/Layout/ms-x86-vtordisp.cpp` cover each 
part of the rule. Their expected layouts were checked against MSVC on both x86 
and x64.

On x86_64 Windows (Release build, assertions enabled), all 1,364 tests in 
`clang/test/Layout` and `clang/test/CodeGenCXX` pass.


---
Full diff: https://github.com/llvm/llvm-project/pull/244957.diff


2 Files Affected:

- (modified) clang/lib/AST/RecordLayoutBuilder.cpp (+25-5) 
- (modified) clang/test/Layout/ms-x86-vtordisp.cpp (+150) 


``````````diff
diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp 
b/clang/lib/AST/RecordLayoutBuilder.cpp
index 40b747da5f9bc3..ac594dec041022 100644
--- a/clang/lib/AST/RecordLayoutBuilder.cpp
+++ b/clang/lib/AST/RecordLayoutBuilder.cpp
@@ -3401,11 +3401,31 @@ void MicrosoftRecordLayoutBuilder::computeVtorDispSet(
       if (bi.second.hasVtorDisp())
         HasVtordispSet.insert(bi.first);
   }
-  // We don't introduce any additional vtordisps if either:
-  // * A user declared constructor or destructor aren't declared.
-  // * #pragma vtordisp(0) or the /vd0 flag are in use.
-  if ((!RD->hasUserDeclaredConstructor() && !RD->hasUserDeclaredDestructor()) 
||
-      RD->getMSVtorDispMode() == MSVtorDispMode::Never)
+  // #pragma vtordisp(0) or the /vd0 flag suppress any additional vtordisps.
+  if (RD->getMSVtorDispMode() == MSVtorDispMode::Never)
+    return;
+  // We only introduce additional vtordisps if the class has a constructor or
+  // destructor that could observe a partially constructed object. MSVC's
+  // notion of this is narrower than "any user-declared constructor or
+  // destructor" (verified against MSVC 19.44 and 19.51):
+  // * A user-declared constructor triggers vtordisps, unless the class also
+  //   has a default constructor that is explicitly defaulted on its first
+  //   declaration and not deleted. Such a default constructor suppresses
+  //   vtordisp creation for *all* of the class's constructors, even
+  //   user-provided ones (e.g. a copy constructor).
+  // * A user-declared destructor triggers vtordisps only if it is
+  //   user-provided; one defaulted on its first declaration does not count.
+  bool HasVtorDispTriggeringCtor = RD->hasUserDeclaredConstructor();
+  if (HasVtorDispTriggeringCtor)
+    for (const CXXConstructorDecl *Ctor : RD->ctors())
+      if (Ctor->isDefaultConstructor() && !Ctor->isUserProvided() &&
+          !Ctor->isDeleted()) {
+        HasVtorDispTriggeringCtor = false;
+        break;
+      }
+  bool HasVtorDispTriggeringDtor =
+      RD->hasUserDeclaredDestructor() && RD->getDestructor()->isUserProvided();
+  if (!HasVtorDispTriggeringCtor && !HasVtorDispTriggeringDtor)
     return;
   // /vd1 or #pragma vtordisp(1): Try to guess based on whether we think it's
   // possible for a partially constructed object with virtual base overrides to
diff --git a/clang/test/Layout/ms-x86-vtordisp.cpp 
b/clang/test/Layout/ms-x86-vtordisp.cpp
index 78bcece6930ff0..3f070acb354b02 100644
--- a/clang/test/Layout/ms-x86-vtordisp.cpp
+++ b/clang/test/Layout/ms-x86-vtordisp.cpp
@@ -375,6 +375,150 @@ struct __declspec(dllexport) IB : virtual IA {
 // CHECK-X64-NEXT:      | [sizeof=16, align=8
 // CHECK-X64-NEXT:      |  nvsize=8, nvalign=8]
 
+// MSVC does not add a vtordisp for a class with a default constructor that is
+// explicitly defaulted on its first declaration (and not deleted), even when
+// other user-provided constructors exist.
+struct JA : virtual B0 {
+       JA() = default;
+       JA(int) {}
+       virtual void f() { printf("JA"); }
+};
+
+// CHECK-LABEL:   0 | struct JA{{$}}
+// CHECK-NEXT:    0 |   (JA vbtable pointer)
+// CHECK-NEXT:    4 |   struct B0 (virtual base)
+// CHECK-NEXT:    4 |     (B0 vftable pointer)
+// CHECK-NEXT:    8 |     int a
+// CHECK-NEXT:      | [sizeof=12, align=4
+// CHECK-NEXT:      |  nvsize=4, nvalign=4]
+// CHECK-X64-LABEL:   0 | struct JA{{$}}
+// CHECK-X64-NEXT:    0 |   (JA vbtable pointer)
+// CHECK-X64-NEXT:    8 |   struct B0 (virtual base)
+// CHECK-X64-NEXT:    8 |     (B0 vftable pointer)
+// CHECK-X64-NEXT:   16 |     int a
+// CHECK-X64-NEXT:      | [sizeof=24, align=8
+// CHECK-X64-NEXT:      |  nvsize=8, nvalign=8]
+
+// A destructor defaulted on its first declaration doesn't add a vtordisp
+// either (it is not user-provided).
+struct JB : virtual B0 {
+       ~JB() = default;
+       virtual void f() { printf("JB"); }
+};
+
+// CHECK-LABEL:   0 | struct JB{{$}}
+// CHECK-NEXT:    0 |   (JB vbtable pointer)
+// CHECK-NEXT:    4 |   struct B0 (virtual base)
+// CHECK-NEXT:    4 |     (B0 vftable pointer)
+// CHECK-NEXT:    8 |     int a
+// CHECK-NEXT:      | [sizeof=12, align=4
+// CHECK-NEXT:      |  nvsize=4, nvalign=4]
+// CHECK-X64-LABEL:   0 | struct JB{{$}}
+// CHECK-X64-NEXT:    0 |   (JB vbtable pointer)
+// CHECK-X64-NEXT:    8 |   struct B0 (virtual base)
+// CHECK-X64-NEXT:    8 |     (B0 vftable pointer)
+// CHECK-X64-NEXT:   16 |     int a
+// CHECK-X64-NEXT:      | [sizeof=24, align=8
+// CHECK-X64-NEXT:      |  nvsize=8, nvalign=8]
+
+// The defaulted default constructor suppresses vtordisps for all of the
+// class's constructors, even a user-provided copy constructor.
+struct JC : virtual B0 {
+       JC() = default;
+       JC(const JC&) {}
+       virtual void f() { printf("JC"); }
+};
+
+// CHECK-LABEL:   0 | struct JC{{$}}
+// CHECK-NEXT:    0 |   (JC vbtable pointer)
+// CHECK-NEXT:    4 |   struct B0 (virtual base)
+// CHECK-NEXT:    4 |     (B0 vftable pointer)
+// CHECK-NEXT:    8 |     int a
+// CHECK-NEXT:      | [sizeof=12, align=4
+// CHECK-NEXT:      |  nvsize=4, nvalign=4]
+// CHECK-X64-LABEL:   0 | struct JC{{$}}
+// CHECK-X64-NEXT:    0 |   (JC vbtable pointer)
+// CHECK-X64-NEXT:    8 |   struct B0 (virtual base)
+// CHECK-X64-NEXT:    8 |     (B0 vftable pointer)
+// CHECK-X64-NEXT:   16 |     int a
+// CHECK-X64-NEXT:      | [sizeof=24, align=8
+// CHECK-X64-NEXT:      |  nvsize=8, nvalign=8]
+
+// A deleted default constructor does not suppress vtordisp creation.
+struct JD : virtual B0 {
+       JD() = delete;
+       JD(int) {}
+       virtual void f() { printf("JD"); }
+};
+
+// CHECK-LABEL:   0 | struct JD{{$}}
+// CHECK-NEXT:    0 |   (JD vbtable pointer)
+// CHECK-NEXT:    4 |   (vtordisp for vbase B0)
+// CHECK-NEXT:    8 |   struct B0 (virtual base)
+// CHECK-NEXT:    8 |     (B0 vftable pointer)
+// CHECK-NEXT:   12 |     int a
+// CHECK-NEXT:      | [sizeof=16, align=4
+// CHECK-NEXT:      |  nvsize=4, nvalign=4]
+// CHECK-X64-LABEL:   0 | struct JD{{$}}
+// CHECK-X64-NEXT:    0 |   (JD vbtable pointer)
+// CHECK-X64-NEXT:   12 |   (vtordisp for vbase B0)
+// CHECK-X64-NEXT:   16 |   struct B0 (virtual base)
+// CHECK-X64-NEXT:   16 |     (B0 vftable pointer)
+// CHECK-X64-NEXT:   24 |     int a
+// CHECK-X64-NEXT:      | [sizeof=32, align=8
+// CHECK-X64-NEXT:      |  nvsize=8, nvalign=8]
+
+// A default constructor defaulted out-of-line (not on its first declaration)
+// is user-provided and does not suppress vtordisp creation.
+struct JE : virtual B0 {
+       JE();
+       JE(int) {}
+       virtual void f() { printf("JE"); }
+};
+JE::JE() = default;
+
+// CHECK-LABEL:   0 | struct JE{{$}}
+// CHECK-NEXT:    0 |   (JE vbtable pointer)
+// CHECK-NEXT:    4 |   (vtordisp for vbase B0)
+// CHECK-NEXT:    8 |   struct B0 (virtual base)
+// CHECK-NEXT:    8 |     (B0 vftable pointer)
+// CHECK-NEXT:   12 |     int a
+// CHECK-NEXT:      | [sizeof=16, align=4
+// CHECK-NEXT:      |  nvsize=4, nvalign=4]
+// CHECK-X64-LABEL:   0 | struct JE{{$}}
+// CHECK-X64-NEXT:    0 |   (JE vbtable pointer)
+// CHECK-X64-NEXT:   12 |   (vtordisp for vbase B0)
+// CHECK-X64-NEXT:   16 |   struct B0 (virtual base)
+// CHECK-X64-NEXT:   16 |     (B0 vftable pointer)
+// CHECK-X64-NEXT:   24 |     int a
+// CHECK-X64-NEXT:      | [sizeof=32, align=8
+// CHECK-X64-NEXT:      |  nvsize=8, nvalign=8]
+
+// A user-provided destructor still adds vtordisps even when the defaulted
+// default constructor would otherwise suppress them.
+struct JF : virtual B0 {
+       JF() = default;
+       ~JF() {}
+       virtual void f() { printf("JF"); }
+};
+
+// CHECK-LABEL:   0 | struct JF{{$}}
+// CHECK-NEXT:    0 |   (JF vbtable pointer)
+// CHECK-NEXT:    4 |   (vtordisp for vbase B0)
+// CHECK-NEXT:    8 |   struct B0 (virtual base)
+// CHECK-NEXT:    8 |     (B0 vftable pointer)
+// CHECK-NEXT:   12 |     int a
+// CHECK-NEXT:      | [sizeof=16, align=4
+// CHECK-NEXT:      |  nvsize=4, nvalign=4]
+// CHECK-X64-LABEL:   0 | struct JF{{$}}
+// CHECK-X64-NEXT:    0 |   (JF vbtable pointer)
+// CHECK-X64-NEXT:   12 |   (vtordisp for vbase B0)
+// CHECK-X64-NEXT:   16 |   struct B0 (virtual base)
+// CHECK-X64-NEXT:   16 |     (B0 vftable pointer)
+// CHECK-X64-NEXT:   24 |     int a
+// CHECK-X64-NEXT:      | [sizeof=32, align=8
+// CHECK-X64-NEXT:      |  nvsize=8, nvalign=8]
+
 int a[
 sizeof(A)+
 sizeof(C)+
@@ -388,4 +532,10 @@ sizeof(pragma_test4::C)+
 sizeof(GD)+
 sizeof(HC)+
 sizeof(IB)+
+sizeof(JA)+
+sizeof(JB)+
+sizeof(JC)+
+sizeof(JD)+
+sizeof(JE)+
+sizeof(JF)+
 0];

``````````

</details>


https://github.com/llvm/llvm-project/pull/244957
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to