Skip to content

Commit 6e614b0

Browse files
committed
[NFC][MSan] Round up OffsetPtr in PoisonMembers
getFieldOffset(layoutStartOffset) is expected to point to the first trivial field or the one which follows non-trivial. So it must be byte aligned already. However this is not obvious without assumptions about callers. This patch will avoid the need in such assumptions. Depends on D92727. Differential Revision: https://reviews.llvm.org/D92728
1 parent deac8b1 commit 6e614b0

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

clang/lib/CodeGen/CGClass.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "TargetInfo.h"
1919
#include "clang/AST/Attr.h"
2020
#include "clang/AST/CXXInheritance.h"
21+
#include "clang/AST/CharUnits.h"
2122
#include "clang/AST/DeclTemplate.h"
2223
#include "clang/AST/EvaluatedExprVisitor.h"
2324
#include "clang/AST/RecordLayout.h"
@@ -1729,37 +1730,35 @@ namespace {
17291730
/// \param layoutEndOffset index of the ASTRecordLayout field to
17301731
/// end poisoning (exclusive)
17311732
void PoisonMembers(CodeGenFunction &CGF, unsigned layoutStartOffset,
1732-
unsigned layoutEndOffset) {
1733+
unsigned layoutEndOffset) {
17331734
ASTContext &Context = CGF.getContext();
17341735
const ASTRecordLayout &Layout =
17351736
Context.getASTRecordLayout(Dtor->getParent());
17361737

1737-
llvm::ConstantInt *OffsetSizePtr = llvm::ConstantInt::get(
1738-
CGF.SizeTy,
1739-
Context.toCharUnitsFromBits(Layout.getFieldOffset(layoutStartOffset))
1740-
.getQuantity());
1738+
// It's a first trivia field so it should be at the begining of char,
1739+
// still round up start offset just in case.
1740+
CharUnits PoisonStart =
1741+
Context.toCharUnitsFromBits(Layout.getFieldOffset(layoutStartOffset) +
1742+
Context.getCharWidth() - 1);
1743+
llvm::ConstantInt *OffsetSizePtr =
1744+
llvm::ConstantInt::get(CGF.SizeTy, PoisonStart.getQuantity());
17411745

17421746
llvm::Value *OffsetPtr = CGF.Builder.CreateGEP(
17431747
CGF.Builder.CreateBitCast(CGF.LoadCXXThis(), CGF.Int8PtrTy),
17441748
OffsetSizePtr);
17451749

1746-
CharUnits::QuantityType PoisonSize;
1750+
CharUnits PoisonEnd;
17471751
if (layoutEndOffset >= Layout.getFieldCount()) {
1748-
PoisonSize = Layout.getNonVirtualSize().getQuantity() -
1749-
Context.toCharUnitsFromBits(
1750-
Layout.getFieldOffset(layoutStartOffset))
1751-
.getQuantity();
1752+
PoisonEnd = Layout.getNonVirtualSize();
17521753
} else {
1753-
PoisonSize = Context.toCharUnitsFromBits(
1754-
Layout.getFieldOffset(layoutEndOffset) -
1755-
Layout.getFieldOffset(layoutStartOffset))
1756-
.getQuantity();
1754+
PoisonEnd =
1755+
Context.toCharUnitsFromBits(Layout.getFieldOffset(layoutEndOffset));
17571756
}
1758-
1759-
if (PoisonSize == 0)
1757+
CharUnits PoisonSize = PoisonEnd - PoisonStart;
1758+
if (!PoisonSize.isPositive())
17601759
return;
17611760

1762-
EmitSanitizerDtorCallback(CGF, OffsetPtr, PoisonSize);
1761+
EmitSanitizerDtorCallback(CGF, OffsetPtr, PoisonSize.getQuantity());
17631762
}
17641763
};
17651764

0 commit comments

Comments
 (0)