[CodeGen] Use ABI alignment for C++ new expressions
authorDaniel Bertalan <dani@danielbertalan.dev>
Tue, 10 May 2022 14:52:52 +0000 (15:52 +0100)
committerNico Weber <thakis@chromium.org>
Tue, 10 May 2022 15:02:23 +0000 (16:02 +0100)
commit93a8225da1f983cccb3e4b5e762369121aaa7dd5
tree984e68e1e816cb56fb2ee25443833bda8d4f977b
parentf1f05a91cacb13635cac252e53e50f9f962d4627
[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
clang/lib/CodeGen/CGExprCXX.cpp
clang/test/CodeGenCXX/pr54845.cpp [new file with mode: 0644]