[PATCH] D124736: [CodeGen] Use ABI alignment for placement new

Daniel Bertalan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun May 1 02:34:38 PDT 2022

BertalanD created this revision.
Herald added a project: All.
BertalanD requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

If we do not know the alignment of the operand, we can't assume it has
the preferred alignment. It might be e.g. a pointer to a struct member
which follows ABI alignment rules.

This makes UBSAN no longer report "constructor call on misaligned
address" when constructing a double into a struct field of type double
on i686. The psABI specifies an alignment of 4 bytes, but the preferred
alignment used by Clang is 8 bytes.

Fixes #54845

  rG LLVM Github Monorepo



Index: clang/lib/CodeGen/CGExprCXX.cpp
--- clang/lib/CodeGen/CGExprCXX.cpp
+++ clang/lib/CodeGen/CGExprCXX.cpp
@@ -1573,7 +1573,7 @@
   llvm::Value *allocSize =
     EmitCXXNewAllocSize(*this, E, minElements, numElements,
-  CharUnits allocAlign = getContext().getPreferredTypeAlignInChars(allocType);
+  CharUnits allocAlign;
   // Emit the allocation call.  If the allocator is a global placement
   // operator, just "inline" it directly.
@@ -1583,6 +1583,8 @@
     assert(E->getNumPlacementArgs() == 1);
     const Expr *arg = *E->placement_arguments().begin();
+    allocAlign = getContext().getTypeAlignInChars(allocType);
     LValueBaseInfo BaseInfo;
     allocation = EmitPointerWithAlignment(arg, &BaseInfo);
@@ -1605,6 +1607,8 @@
     unsigned ParamsToSkip = 0;
+    allocAlign = getContext().getPreferredTypeAlignInChars(allocType);
     // The allocation size is the first argument.
     QualType sizeType = getContext().getSizeType();
     allocatorArgs.add(RValue::get(allocSize), sizeType);

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124736.426280.patch
Type: text/x-patch
Size: 1195 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220501/39cb7773/attachment.bin>

More information about the cfe-commits mailing list