This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Use ABI alignment for C++ new expressions
ClosedPublic

Authored by BertalanD on May 1 2022, 2:34 AM.

Details

Summary

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

I don't have commit rights, so if this patch is accepted, I'd like someone to commit it for me.
Author info: "Daniel Bertalan <dani@danielbertalan.dev>"

Diff Detail

Event Timeline

BertalanD created this revision.May 1 2022, 2:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 1 2022, 2:34 AM
BertalanD requested review of this revision.May 1 2022, 2:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 1 2022, 2:34 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
BertalanD updated this revision to Diff 426301.May 1 2022, 11:27 AM

Added a test case.

BertalanD edited the summary of this revision. (Show Details)May 1 2022, 11:28 AM
BertalanD updated this revision to Diff 426303.May 1 2022, 11:54 AM

Removed accidental execute permission from the test.

BertalanD edited the summary of this revision. (Show Details)May 1 2022, 1:49 PM

It should probably be the ABI alignment in all cases; I think the preferred alignment is just supposed to be used to opportunistically over-align things like e.g. local variables, which doesn't seem relevant for the ABI code involving a call to operator new.

It should probably be the ABI alignment in all cases; I think the preferred alignment is just supposed to be used to opportunistically over-align things like e.g. local variables, which doesn't seem relevant for the ABI code involving a call to operator new.

Right, that makes sense. I'll update this patch tomorrow. Thanks for the review :)

BertalanD updated this revision to Diff 428225.May 9 2022, 3:52 PM

Use ABI alignment for allocating operator new as well.

Sorry for the delay.

BertalanD edited the summary of this revision. (Show Details)May 9 2022, 3:53 PM
rjmccall accepted this revision.May 9 2022, 4:00 PM

LGTM

This revision is now accepted and ready to land.May 9 2022, 4:00 PM

Could you please commit this for me, @rjmccall?

I'm wondering if there's a chance this could be backported to the LLVM 14 branch. We've been running into the linked issue in production, it would be great if it were fixed in the next point release.

BertalanD retitled this revision from [CodeGen] Use ABI alignment for placement new to [CodeGen] Use ABI alignment for C++ new expressions.May 10 2022, 7:56 AM
This revision was automatically updated to reflect the committed changes.