Skip to content

Commit d631924

Browse files
BertalanDtstellar
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 #54845. Differential Revision: https://reviews.llvm.org/D124736 (cherry picked from commit 93a8225)
1 parent daef311 commit d631924

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
@@ -1572,7 +1572,7 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
15721572
llvm::Value *allocSize =
15731573
EmitCXXNewAllocSize(*this, E, minElements, numElements,
15741574
allocSizeWithoutCookie);
1575-
CharUnits allocAlign = getContext().getPreferredTypeAlignInChars(allocType);
1575+
CharUnits allocAlign = getContext().getTypeAlignInChars(allocType);
15761576

15771577
// Emit the allocation call. If the allocator is a global placement
15781578
// 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)