17330f729Sjoerg //=======- PaddingChecker.cpp ------------------------------------*- C++ -*-==//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines a checker that checks for padding that could be
107330f729Sjoerg // removed by re-ordering members.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg
147330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
157330f729Sjoerg #include "clang/AST/CharUnits.h"
167330f729Sjoerg #include "clang/AST/DeclTemplate.h"
177330f729Sjoerg #include "clang/AST/RecordLayout.h"
187330f729Sjoerg #include "clang/AST/RecursiveASTVisitor.h"
197330f729Sjoerg #include "clang/Driver/DriverDiagnostic.h"
207330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
217330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
227330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
237330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
247330f729Sjoerg #include "llvm/ADT/SmallString.h"
257330f729Sjoerg #include "llvm/Support/MathExtras.h"
267330f729Sjoerg #include "llvm/Support/raw_ostream.h"
277330f729Sjoerg #include <numeric>
287330f729Sjoerg
297330f729Sjoerg using namespace clang;
307330f729Sjoerg using namespace ento;
317330f729Sjoerg
327330f729Sjoerg namespace {
337330f729Sjoerg class PaddingChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
347330f729Sjoerg private:
357330f729Sjoerg mutable std::unique_ptr<BugType> PaddingBug;
367330f729Sjoerg mutable BugReporter *BR;
377330f729Sjoerg
387330f729Sjoerg public:
397330f729Sjoerg int64_t AllowedPad;
407330f729Sjoerg
checkASTDecl(const TranslationUnitDecl * TUD,AnalysisManager & MGR,BugReporter & BRArg) const417330f729Sjoerg void checkASTDecl(const TranslationUnitDecl *TUD, AnalysisManager &MGR,
427330f729Sjoerg BugReporter &BRArg) const {
437330f729Sjoerg BR = &BRArg;
447330f729Sjoerg
457330f729Sjoerg // The calls to checkAST* from AnalysisConsumer don't
467330f729Sjoerg // visit template instantiations or lambda classes. We
477330f729Sjoerg // want to visit those, so we make our own RecursiveASTVisitor.
487330f729Sjoerg struct LocalVisitor : public RecursiveASTVisitor<LocalVisitor> {
497330f729Sjoerg const PaddingChecker *Checker;
507330f729Sjoerg bool shouldVisitTemplateInstantiations() const { return true; }
517330f729Sjoerg bool shouldVisitImplicitCode() const { return true; }
527330f729Sjoerg explicit LocalVisitor(const PaddingChecker *Checker) : Checker(Checker) {}
537330f729Sjoerg bool VisitRecordDecl(const RecordDecl *RD) {
547330f729Sjoerg Checker->visitRecord(RD);
557330f729Sjoerg return true;
567330f729Sjoerg }
577330f729Sjoerg bool VisitVarDecl(const VarDecl *VD) {
587330f729Sjoerg Checker->visitVariable(VD);
597330f729Sjoerg return true;
607330f729Sjoerg }
617330f729Sjoerg // TODO: Visit array new and mallocs for arrays.
627330f729Sjoerg };
637330f729Sjoerg
647330f729Sjoerg LocalVisitor visitor(this);
657330f729Sjoerg visitor.TraverseDecl(const_cast<TranslationUnitDecl *>(TUD));
667330f729Sjoerg }
677330f729Sjoerg
687330f729Sjoerg /// Look for records of overly padded types. If padding *
697330f729Sjoerg /// PadMultiplier exceeds AllowedPad, then generate a report.
707330f729Sjoerg /// PadMultiplier is used to share code with the array padding
717330f729Sjoerg /// checker.
visitRecord(const RecordDecl * RD,uint64_t PadMultiplier=1) const727330f729Sjoerg void visitRecord(const RecordDecl *RD, uint64_t PadMultiplier = 1) const {
737330f729Sjoerg if (shouldSkipDecl(RD))
747330f729Sjoerg return;
757330f729Sjoerg
767330f729Sjoerg // TODO: Figure out why we are going through declarations and not only
777330f729Sjoerg // definitions.
787330f729Sjoerg if (!(RD = RD->getDefinition()))
797330f729Sjoerg return;
807330f729Sjoerg
817330f729Sjoerg // This is the simplest correct case: a class with no fields and one base
827330f729Sjoerg // class. Other cases are more complicated because of how the base classes
837330f729Sjoerg // & fields might interact, so we don't bother dealing with them.
847330f729Sjoerg // TODO: Support other combinations of base classes and fields.
857330f729Sjoerg if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
867330f729Sjoerg if (CXXRD->field_empty() && CXXRD->getNumBases() == 1)
877330f729Sjoerg return visitRecord(CXXRD->bases().begin()->getType()->getAsRecordDecl(),
887330f729Sjoerg PadMultiplier);
897330f729Sjoerg
907330f729Sjoerg auto &ASTContext = RD->getASTContext();
917330f729Sjoerg const ASTRecordLayout &RL = ASTContext.getASTRecordLayout(RD);
927330f729Sjoerg assert(llvm::isPowerOf2_64(RL.getAlignment().getQuantity()));
937330f729Sjoerg
947330f729Sjoerg CharUnits BaselinePad = calculateBaselinePad(RD, ASTContext, RL);
957330f729Sjoerg if (BaselinePad.isZero())
967330f729Sjoerg return;
977330f729Sjoerg
987330f729Sjoerg CharUnits OptimalPad;
997330f729Sjoerg SmallVector<const FieldDecl *, 20> OptimalFieldsOrder;
1007330f729Sjoerg std::tie(OptimalPad, OptimalFieldsOrder) =
1017330f729Sjoerg calculateOptimalPad(RD, ASTContext, RL);
1027330f729Sjoerg
1037330f729Sjoerg CharUnits DiffPad = PadMultiplier * (BaselinePad - OptimalPad);
1047330f729Sjoerg if (DiffPad.getQuantity() <= AllowedPad) {
1057330f729Sjoerg assert(!DiffPad.isNegative() && "DiffPad should not be negative");
1067330f729Sjoerg // There is not enough excess padding to trigger a warning.
1077330f729Sjoerg return;
1087330f729Sjoerg }
1097330f729Sjoerg reportRecord(RD, BaselinePad, OptimalPad, OptimalFieldsOrder);
1107330f729Sjoerg }
1117330f729Sjoerg
1127330f729Sjoerg /// Look for arrays of overly padded types. If the padding of the
1137330f729Sjoerg /// array type exceeds AllowedPad, then generate a report.
visitVariable(const VarDecl * VD) const1147330f729Sjoerg void visitVariable(const VarDecl *VD) const {
1157330f729Sjoerg const ArrayType *ArrTy = VD->getType()->getAsArrayTypeUnsafe();
1167330f729Sjoerg if (ArrTy == nullptr)
1177330f729Sjoerg return;
1187330f729Sjoerg uint64_t Elts = 0;
1197330f729Sjoerg if (const ConstantArrayType *CArrTy = dyn_cast<ConstantArrayType>(ArrTy))
1207330f729Sjoerg Elts = CArrTy->getSize().getZExtValue();
1217330f729Sjoerg if (Elts == 0)
1227330f729Sjoerg return;
1237330f729Sjoerg const RecordType *RT = ArrTy->getElementType()->getAs<RecordType>();
1247330f729Sjoerg if (RT == nullptr)
1257330f729Sjoerg return;
1267330f729Sjoerg
1277330f729Sjoerg // TODO: Recurse into the fields to see if they have excess padding.
1287330f729Sjoerg visitRecord(RT->getDecl(), Elts);
1297330f729Sjoerg }
1307330f729Sjoerg
shouldSkipDecl(const RecordDecl * RD) const1317330f729Sjoerg bool shouldSkipDecl(const RecordDecl *RD) const {
1327330f729Sjoerg // TODO: Figure out why we are going through declarations and not only
1337330f729Sjoerg // definitions.
1347330f729Sjoerg if (!(RD = RD->getDefinition()))
1357330f729Sjoerg return true;
1367330f729Sjoerg auto Location = RD->getLocation();
1377330f729Sjoerg // If the construct doesn't have a source file, then it's not something
1387330f729Sjoerg // we want to diagnose.
1397330f729Sjoerg if (!Location.isValid())
1407330f729Sjoerg return true;
1417330f729Sjoerg SrcMgr::CharacteristicKind Kind =
1427330f729Sjoerg BR->getSourceManager().getFileCharacteristic(Location);
1437330f729Sjoerg // Throw out all records that come from system headers.
1447330f729Sjoerg if (Kind != SrcMgr::C_User)
1457330f729Sjoerg return true;
1467330f729Sjoerg
1477330f729Sjoerg // Not going to attempt to optimize unions.
1487330f729Sjoerg if (RD->isUnion())
1497330f729Sjoerg return true;
1507330f729Sjoerg if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1517330f729Sjoerg // Tail padding with base classes ends up being very complicated.
1527330f729Sjoerg // We will skip objects with base classes for now, unless they do not
1537330f729Sjoerg // have fields.
1547330f729Sjoerg // TODO: Handle more base class scenarios.
1557330f729Sjoerg if (!CXXRD->field_empty() && CXXRD->getNumBases() != 0)
1567330f729Sjoerg return true;
1577330f729Sjoerg if (CXXRD->field_empty() && CXXRD->getNumBases() != 1)
1587330f729Sjoerg return true;
1597330f729Sjoerg // Virtual bases are complicated, skipping those for now.
1607330f729Sjoerg if (CXXRD->getNumVBases() != 0)
1617330f729Sjoerg return true;
1627330f729Sjoerg // Can't layout a template, so skip it. We do still layout the
1637330f729Sjoerg // instantiations though.
1647330f729Sjoerg if (CXXRD->getTypeForDecl()->isDependentType())
1657330f729Sjoerg return true;
1667330f729Sjoerg if (CXXRD->getTypeForDecl()->isInstantiationDependentType())
1677330f729Sjoerg return true;
1687330f729Sjoerg }
1697330f729Sjoerg // How do you reorder fields if you haven't got any?
1707330f729Sjoerg else if (RD->field_empty())
1717330f729Sjoerg return true;
1727330f729Sjoerg
1737330f729Sjoerg auto IsTrickyField = [](const FieldDecl *FD) -> bool {
1747330f729Sjoerg // Bitfield layout is hard.
1757330f729Sjoerg if (FD->isBitField())
1767330f729Sjoerg return true;
1777330f729Sjoerg
1787330f729Sjoerg // Variable length arrays are tricky too.
1797330f729Sjoerg QualType Ty = FD->getType();
1807330f729Sjoerg if (Ty->isIncompleteArrayType())
1817330f729Sjoerg return true;
1827330f729Sjoerg return false;
1837330f729Sjoerg };
1847330f729Sjoerg
1857330f729Sjoerg if (std::any_of(RD->field_begin(), RD->field_end(), IsTrickyField))
1867330f729Sjoerg return true;
1877330f729Sjoerg return false;
1887330f729Sjoerg }
1897330f729Sjoerg
calculateBaselinePad(const RecordDecl * RD,const ASTContext & ASTContext,const ASTRecordLayout & RL)1907330f729Sjoerg static CharUnits calculateBaselinePad(const RecordDecl *RD,
1917330f729Sjoerg const ASTContext &ASTContext,
1927330f729Sjoerg const ASTRecordLayout &RL) {
1937330f729Sjoerg CharUnits PaddingSum;
1947330f729Sjoerg CharUnits Offset = ASTContext.toCharUnitsFromBits(RL.getFieldOffset(0));
1957330f729Sjoerg for (const FieldDecl *FD : RD->fields()) {
1967330f729Sjoerg // This checker only cares about the padded size of the
1977330f729Sjoerg // field, and not the data size. If the field is a record
1987330f729Sjoerg // with tail padding, then we won't put that number in our
1997330f729Sjoerg // total because reordering fields won't fix that problem.
2007330f729Sjoerg CharUnits FieldSize = ASTContext.getTypeSizeInChars(FD->getType());
2017330f729Sjoerg auto FieldOffsetBits = RL.getFieldOffset(FD->getFieldIndex());
2027330f729Sjoerg CharUnits FieldOffset = ASTContext.toCharUnitsFromBits(FieldOffsetBits);
2037330f729Sjoerg PaddingSum += (FieldOffset - Offset);
2047330f729Sjoerg Offset = FieldOffset + FieldSize;
2057330f729Sjoerg }
2067330f729Sjoerg PaddingSum += RL.getSize() - Offset;
2077330f729Sjoerg return PaddingSum;
2087330f729Sjoerg }
2097330f729Sjoerg
2107330f729Sjoerg /// Optimal padding overview:
2117330f729Sjoerg /// 1. Find a close approximation to where we can place our first field.
2127330f729Sjoerg /// This will usually be at offset 0.
2137330f729Sjoerg /// 2. Try to find the best field that can legally be placed at the current
2147330f729Sjoerg /// offset.
2157330f729Sjoerg /// a. "Best" is the largest alignment that is legal, but smallest size.
2167330f729Sjoerg /// This is to account for overly aligned types.
2177330f729Sjoerg /// 3. If no fields can fit, pad by rounding the current offset up to the
2187330f729Sjoerg /// smallest alignment requirement of our fields. Measure and track the
2197330f729Sjoerg // amount of padding added. Go back to 2.
2207330f729Sjoerg /// 4. Increment the current offset by the size of the chosen field.
2217330f729Sjoerg /// 5. Remove the chosen field from the set of future possibilities.
2227330f729Sjoerg /// 6. Go back to 2 if there are still unplaced fields.
2237330f729Sjoerg /// 7. Add tail padding by rounding the current offset up to the structure
2247330f729Sjoerg /// alignment. Track the amount of padding added.
2257330f729Sjoerg
2267330f729Sjoerg static std::pair<CharUnits, SmallVector<const FieldDecl *, 20>>
calculateOptimalPad(const RecordDecl * RD,const ASTContext & ASTContext,const ASTRecordLayout & RL)2277330f729Sjoerg calculateOptimalPad(const RecordDecl *RD, const ASTContext &ASTContext,
2287330f729Sjoerg const ASTRecordLayout &RL) {
2297330f729Sjoerg struct FieldInfo {
2307330f729Sjoerg CharUnits Align;
2317330f729Sjoerg CharUnits Size;
2327330f729Sjoerg const FieldDecl *Field;
2337330f729Sjoerg bool operator<(const FieldInfo &RHS) const {
2347330f729Sjoerg // Order from small alignments to large alignments,
2357330f729Sjoerg // then large sizes to small sizes.
2367330f729Sjoerg // then large field indices to small field indices
2377330f729Sjoerg return std::make_tuple(Align, -Size,
2387330f729Sjoerg Field ? -static_cast<int>(Field->getFieldIndex())
2397330f729Sjoerg : 0) <
2407330f729Sjoerg std::make_tuple(
2417330f729Sjoerg RHS.Align, -RHS.Size,
2427330f729Sjoerg RHS.Field ? -static_cast<int>(RHS.Field->getFieldIndex())
2437330f729Sjoerg : 0);
2447330f729Sjoerg }
2457330f729Sjoerg };
2467330f729Sjoerg SmallVector<FieldInfo, 20> Fields;
2477330f729Sjoerg auto GatherSizesAndAlignments = [](const FieldDecl *FD) {
2487330f729Sjoerg FieldInfo RetVal;
2497330f729Sjoerg RetVal.Field = FD;
2507330f729Sjoerg auto &Ctx = FD->getASTContext();
251*e038c9c4Sjoerg auto Info = Ctx.getTypeInfoInChars(FD->getType());
252*e038c9c4Sjoerg RetVal.Size = Info.Width;
253*e038c9c4Sjoerg RetVal.Align = Info.Align;
2547330f729Sjoerg assert(llvm::isPowerOf2_64(RetVal.Align.getQuantity()));
2557330f729Sjoerg if (auto Max = FD->getMaxAlignment())
2567330f729Sjoerg RetVal.Align = std::max(Ctx.toCharUnitsFromBits(Max), RetVal.Align);
2577330f729Sjoerg return RetVal;
2587330f729Sjoerg };
2597330f729Sjoerg std::transform(RD->field_begin(), RD->field_end(),
2607330f729Sjoerg std::back_inserter(Fields), GatherSizesAndAlignments);
2617330f729Sjoerg llvm::sort(Fields);
2627330f729Sjoerg // This lets us skip over vptrs and non-virtual bases,
2637330f729Sjoerg // so that we can just worry about the fields in our object.
2647330f729Sjoerg // Note that this does cause us to miss some cases where we
2657330f729Sjoerg // could pack more bytes in to a base class's tail padding.
2667330f729Sjoerg CharUnits NewOffset = ASTContext.toCharUnitsFromBits(RL.getFieldOffset(0));
2677330f729Sjoerg CharUnits NewPad;
2687330f729Sjoerg SmallVector<const FieldDecl *, 20> OptimalFieldsOrder;
2697330f729Sjoerg while (!Fields.empty()) {
2707330f729Sjoerg unsigned TrailingZeros =
2717330f729Sjoerg llvm::countTrailingZeros((unsigned long long)NewOffset.getQuantity());
2727330f729Sjoerg // If NewOffset is zero, then countTrailingZeros will be 64. Shifting
2737330f729Sjoerg // 64 will overflow our unsigned long long. Shifting 63 will turn
2747330f729Sjoerg // our long long (and CharUnits internal type) negative. So shift 62.
2757330f729Sjoerg long long CurAlignmentBits = 1ull << (std::min)(TrailingZeros, 62u);
2767330f729Sjoerg CharUnits CurAlignment = CharUnits::fromQuantity(CurAlignmentBits);
2777330f729Sjoerg FieldInfo InsertPoint = {CurAlignment, CharUnits::Zero(), nullptr};
2787330f729Sjoerg
2797330f729Sjoerg // In the typical case, this will find the last element
2807330f729Sjoerg // of the vector. We won't find a middle element unless
2817330f729Sjoerg // we started on a poorly aligned address or have an overly
2827330f729Sjoerg // aligned field.
2837330f729Sjoerg auto Iter = llvm::upper_bound(Fields, InsertPoint);
2847330f729Sjoerg if (Iter != Fields.begin()) {
2857330f729Sjoerg // We found a field that we can layout with the current alignment.
2867330f729Sjoerg --Iter;
2877330f729Sjoerg NewOffset += Iter->Size;
2887330f729Sjoerg OptimalFieldsOrder.push_back(Iter->Field);
2897330f729Sjoerg Fields.erase(Iter);
2907330f729Sjoerg } else {
2917330f729Sjoerg // We are poorly aligned, and we need to pad in order to layout another
2927330f729Sjoerg // field. Round up to at least the smallest field alignment that we
2937330f729Sjoerg // currently have.
2947330f729Sjoerg CharUnits NextOffset = NewOffset.alignTo(Fields[0].Align);
2957330f729Sjoerg NewPad += NextOffset - NewOffset;
2967330f729Sjoerg NewOffset = NextOffset;
2977330f729Sjoerg }
2987330f729Sjoerg }
2997330f729Sjoerg // Calculate tail padding.
3007330f729Sjoerg CharUnits NewSize = NewOffset.alignTo(RL.getAlignment());
3017330f729Sjoerg NewPad += NewSize - NewOffset;
3027330f729Sjoerg return {NewPad, std::move(OptimalFieldsOrder)};
3037330f729Sjoerg }
3047330f729Sjoerg
reportRecord(const RecordDecl * RD,CharUnits BaselinePad,CharUnits OptimalPad,const SmallVector<const FieldDecl *,20> & OptimalFieldsOrder) const3057330f729Sjoerg void reportRecord(
3067330f729Sjoerg const RecordDecl *RD, CharUnits BaselinePad, CharUnits OptimalPad,
3077330f729Sjoerg const SmallVector<const FieldDecl *, 20> &OptimalFieldsOrder) const {
3087330f729Sjoerg if (!PaddingBug)
3097330f729Sjoerg PaddingBug =
3107330f729Sjoerg std::make_unique<BugType>(this, "Excessive Padding", "Performance");
3117330f729Sjoerg
3127330f729Sjoerg SmallString<100> Buf;
3137330f729Sjoerg llvm::raw_svector_ostream Os(Buf);
3147330f729Sjoerg Os << "Excessive padding in '";
3157330f729Sjoerg Os << QualType::getAsString(RD->getTypeForDecl(), Qualifiers(),
3167330f729Sjoerg LangOptions())
3177330f729Sjoerg << "'";
3187330f729Sjoerg
3197330f729Sjoerg if (auto *TSD = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
3207330f729Sjoerg // TODO: make this show up better in the console output and in
3217330f729Sjoerg // the HTML. Maybe just make it show up in HTML like the path
3227330f729Sjoerg // diagnostics show.
3237330f729Sjoerg SourceLocation ILoc = TSD->getPointOfInstantiation();
3247330f729Sjoerg if (ILoc.isValid())
3257330f729Sjoerg Os << " instantiated here: "
3267330f729Sjoerg << ILoc.printToString(BR->getSourceManager());
3277330f729Sjoerg }
3287330f729Sjoerg
3297330f729Sjoerg Os << " (" << BaselinePad.getQuantity() << " padding bytes, where "
3307330f729Sjoerg << OptimalPad.getQuantity() << " is optimal). \n"
3317330f729Sjoerg << "Optimal fields order: \n";
3327330f729Sjoerg for (const auto *FD : OptimalFieldsOrder)
3337330f729Sjoerg Os << FD->getName() << ", \n";
3347330f729Sjoerg Os << "consider reordering the fields or adding explicit padding "
3357330f729Sjoerg "members.";
3367330f729Sjoerg
3377330f729Sjoerg PathDiagnosticLocation CELoc =
3387330f729Sjoerg PathDiagnosticLocation::create(RD, BR->getSourceManager());
3397330f729Sjoerg auto Report =
3407330f729Sjoerg std::make_unique<BasicBugReport>(*PaddingBug, Os.str(), CELoc);
3417330f729Sjoerg Report->setDeclWithIssue(RD);
3427330f729Sjoerg Report->addRange(RD->getSourceRange());
3437330f729Sjoerg BR->emitReport(std::move(Report));
3447330f729Sjoerg }
3457330f729Sjoerg };
3467330f729Sjoerg } // namespace
3477330f729Sjoerg
registerPaddingChecker(CheckerManager & Mgr)3487330f729Sjoerg void ento::registerPaddingChecker(CheckerManager &Mgr) {
3497330f729Sjoerg auto *Checker = Mgr.registerChecker<PaddingChecker>();
3507330f729Sjoerg Checker->AllowedPad = Mgr.getAnalyzerOptions()
3517330f729Sjoerg .getCheckerIntegerOption(Checker, "AllowedPad");
3527330f729Sjoerg if (Checker->AllowedPad < 0)
3537330f729Sjoerg Mgr.reportInvalidCheckerOptionValue(
3547330f729Sjoerg Checker, "AllowedPad", "a non-negative value");
3557330f729Sjoerg }
3567330f729Sjoerg
shouldRegisterPaddingChecker(const CheckerManager & mgr)357*e038c9c4Sjoerg bool ento::shouldRegisterPaddingChecker(const CheckerManager &mgr) {
3587330f729Sjoerg return true;
3597330f729Sjoerg }
360