Skip to content

Commit 93a8225

Browse files
BertalanDnico
authored andcommitted
[CodeGen] Use ABI alignment for C++ new expressions
In case of placement new, 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. We now use ABI alignment for allocating new as well, as the preferred alignment should be used for over-aligning e.g. local variables, which isn't relevant for ABI code dealing with operator new. AFAICT there wouldn't be problems either way though. Fixes llvm#54845. Differential Revision: https://reviews.llvm.org/D124736
1 parent f1f05a9 commit 93a8225

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,7 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
15731573
llvm::Value *allocSize =
15741574
EmitCXXNewAllocSize(*this, E, minElements, numElements,
15751575
allocSizeWithoutCookie);
1576-
CharUnits allocAlign = getContext().getPreferredTypeAlignInChars(allocType);
1576+
CharUnits allocAlign = getContext().getTypeAlignInChars(allocType);
15771577

15781578
// Emit the allocation call. If the allocator is a global placement
15791579
// operator, just "inline" it directly.

clang/test/CodeGenCXX/pr54845.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 -triple i686-linux-gnu -emit-llvm %s -o - | FileCheck %s
2+
// https://github.com/llvm/llvm-project/issues/54845
3+
4+
void *operator new(unsigned int, void *);
5+
6+
void test(double *d) {
7+
// This store used to have an alignment of 8, which was incorrect as
8+
// the i386 psABI only guarantees a 4-byte alignment for doubles.
9+
10+
// CHECK: store double 0.000000e+00, {{.*}}, align 4
11+
new (d) double(0);
12+
}

0 commit comments

Comments
 (0)