This patch teaches ubsan to insert an alignment check for the 'this'
pointer at the start of each method/lambda. This allows clang to emit
significantly fewer alignment checks overall, because if 'this' is
aligned, so are its fields.
This is essentially the same thing r295515 does, but for the alignment
check instead of the null check. One difference is that we keep the
alignment checks on member expressions where the base is a DeclRefExpr.
There's an opportunity to diagnose unaligned accesses in this situation
(as pointed out by Eli, see PR32630).
Testing: check-clang, check-ubsan, and a stage2 ubsan build.
Along with the patch from D30285, this roughly halves the amount of
alignment checks we emit when compiling X86FastISel.cpp. Here are the
numbers from patched/unpatched clangs based on r298160.
Setup | # of alignment checks | |
unpatched, -O0 | 24326 | |
patched, -O0 | 12717 | (-47.7%) |
There are a few possible follow-ups:
- Don't add the per method/lambda check in delegating constructors.
- Don't instrument accesses to fields with alignment = 1.
Probably a good idea to also test extern globals which aren't arrays; arrays are sort of a special-case due to array->pointer decay.