summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorPeter Collingbourne <peter@pcc.me.uk>2019-07-22 18:50:45 +0000
committerPeter Collingbourne <peter@pcc.me.uk>2019-07-22 18:50:45 +0000
commitef5cfc2dae0725226254561b42134308720e57ab (patch)
tree51643c26c4ce1ac61059ca60f431303f4464174c /llvm/lib
parent324d33dd4ed653d54466a45ba0e397015c900a5b (diff)
downloadbcm5719-llvm-ef5cfc2dae0725226254561b42134308720e57ab.tar.gz
bcm5719-llvm-ef5cfc2dae0725226254561b42134308720e57ab.zip
WholeProgramDevirt: Teach the pass to respect the global's alignment.
The bytes inserted before an overaligned global need to be padded according to the alignment set on the original global in order for the initializer to meet the global's alignment requirements. The previous implementation that padded to the pointer width happened to be correct for vtables on most platforms but may do the wrong thing if the vtable has a larger alignment. This issue is visible with a prototype implementation of HWASAN for globals, which will overalign all globals including vtables to 16 bytes. There is also no padding requirement for the bytes inserted after the global because they are never read from nor are they significant for alignment purposes, so stop inserting padding there. Differential Revision: https://reviews.llvm.org/D65031 llvm-svn: 366725
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp11
1 files changed, 7 insertions, 4 deletions
diff --git a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
index 6b6dd6194e1..6bbcba94b00 100644
--- a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
+++ b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
@@ -1302,10 +1302,12 @@ void DevirtModule::rebuildGlobal(VTableBits &B) {
if (B.Before.Bytes.empty() && B.After.Bytes.empty())
return;
- // Align each byte array to pointer width.
- unsigned PointerSize = M.getDataLayout().getPointerSize();
- B.Before.Bytes.resize(alignTo(B.Before.Bytes.size(), PointerSize));
- B.After.Bytes.resize(alignTo(B.After.Bytes.size(), PointerSize));
+ // Align the before byte array to the global's minimum alignment so that we
+ // don't break any alignment requirements on the global.
+ unsigned Align = B.GV->getAlignment();
+ if (Align == 0)
+ Align = M.getDataLayout().getABITypeAlignment(B.GV->getValueType());
+ B.Before.Bytes.resize(alignTo(B.Before.Bytes.size(), Align));
// Before was stored in reverse order; flip it now.
for (size_t I = 0, Size = B.Before.Bytes.size(); I != Size / 2; ++I)
@@ -1322,6 +1324,7 @@ void DevirtModule::rebuildGlobal(VTableBits &B) {
GlobalVariable::PrivateLinkage, NewInit, "", B.GV);
NewGV->setSection(B.GV->getSection());
NewGV->setComdat(B.GV->getComdat());
+ NewGV->setAlignment(B.GV->getAlignment());
// Copy the original vtable's metadata to the anonymous global, adjusting
// offsets as required.
OpenPOWER on IntegriCloud