[llvm-branch-commits] [clang] d631924 - [CodeGen] Use ABI alignment for C++ new expressions
Tom Stellard via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed May 18 20:12:01 PDT 2022
Author: Daniel Bertalan
Date: 2022-05-18T20:10:10-07:00
New Revision: d6319246f89b2f47c9176e23be3fbe23c7c7d2c4
URL: https://github.com/llvm/llvm-project/commit/d6319246f89b2f47c9176e23be3fbe23c7c7d2c4
DIFF: https://github.com/llvm/llvm-project/commit/d6319246f89b2f47c9176e23be3fbe23c7c7d2c4.diff
LOG: [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 93a8225da1f983cccb3e4b5e762369121aaa7dd5)
Added:
clang/test/CodeGenCXX/pr54845.cpp
Modified:
clang/lib/CodeGen/CGExprCXX.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp
index f06d218617400..3a3acf98f3094 100644
--- a/clang/lib/CodeGen/CGExprCXX.cpp
+++ b/clang/lib/CodeGen/CGExprCXX.cpp
@@ -1572,7 +1572,7 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
llvm::Value *allocSize =
EmitCXXNewAllocSize(*this, E, minElements, numElements,
allocSizeWithoutCookie);
- CharUnits allocAlign = getContext().getPreferredTypeAlignInChars(allocType);
+ CharUnits allocAlign = getContext().getTypeAlignInChars(allocType);
// Emit the allocation call. If the allocator is a global placement
// operator, just "inline" it directly.
diff --git a/clang/test/CodeGenCXX/pr54845.cpp b/clang/test/CodeGenCXX/pr54845.cpp
new file mode 100644
index 0000000000000..acf2c3121900b
--- /dev/null
+++ b/clang/test/CodeGenCXX/pr54845.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple i686-linux-gnu -emit-llvm %s -o - | FileCheck %s
+// https://github.com/llvm/llvm-project/issues/54845
+
+void *operator new(unsigned int, void *);
+
+void test(double *d) {
+ // This store used to have an alignment of 8, which was incorrect as
+ // the i386 psABI only guarantees a 4-byte alignment for doubles.
+
+ // CHECK: store double 0.000000e+00, {{.*}}, align 4
+ new (d) double(0);
+}
More information about the llvm-branch-commits
mailing list