clang  3.9.0
PaddingChecker.cpp
Go to the documentation of this file.
1 //=======- PaddingChecker.cpp ------------------------------------*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a checker that checks for padding that could be
11 // removed by re-ordering members.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ClangSACheckers.h"
16 #include "clang/AST/CharUnits.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/RecordLayout.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <numeric>
28 
29 using namespace clang;
30 using namespace ento;
31 
32 namespace {
33 class PaddingChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
34 private:
35  mutable std::unique_ptr<BugType> PaddingBug;
36  mutable int64_t AllowedPad;
37  mutable BugReporter *BR;
38 
39 public:
40  void checkASTDecl(const TranslationUnitDecl *TUD, AnalysisManager &MGR,
41  BugReporter &BRArg) const {
42  BR = &BRArg;
43  AllowedPad =
44  MGR.getAnalyzerOptions().getOptionAsInteger("AllowedPad", 24, this);
45  assert(AllowedPad >= 0 && "AllowedPad option should be non-negative");
46 
47  // The calls to checkAST* from AnalysisConsumer don't
48  // visit template instantiations or lambda classes. We
49  // want to visit those, so we make our own RecursiveASTVisitor.
50  struct LocalVisitor : public RecursiveASTVisitor<LocalVisitor> {
51  const PaddingChecker *Checker;
52  bool shouldVisitTemplateInstantiations() const { return true; }
53  bool shouldVisitImplicitCode() const { return true; }
54  explicit LocalVisitor(const PaddingChecker *Checker) : Checker(Checker) {}
55  bool VisitRecordDecl(const RecordDecl *RD) {
56  Checker->visitRecord(RD);
57  return true;
58  }
59  bool VisitVarDecl(const VarDecl *VD) {
60  Checker->visitVariable(VD);
61  return true;
62  }
63  // TODO: Visit array new and mallocs for arrays.
64  };
65 
66  LocalVisitor visitor(this);
67  visitor.TraverseDecl(const_cast<TranslationUnitDecl *>(TUD));
68  }
69 
70  /// \brief Look for records of overly padded types. If padding *
71  /// PadMultiplier exceeds AllowedPad, then generate a report.
72  /// PadMultiplier is used to share code with the array padding
73  /// checker.
74  void visitRecord(const RecordDecl *RD, uint64_t PadMultiplier = 1) const {
75  if (shouldSkipDecl(RD))
76  return;
77 
78  auto &ASTContext = RD->getASTContext();
80  assert(llvm::isPowerOf2_64(RL.getAlignment().getQuantity()));
81 
82  CharUnits BaselinePad = calculateBaselinePad(RD, ASTContext, RL);
83  if (BaselinePad.isZero())
84  return;
85  CharUnits OptimalPad = calculateOptimalPad(RD, ASTContext, RL);
86 
87  CharUnits DiffPad = PadMultiplier * (BaselinePad - OptimalPad);
88  if (DiffPad.getQuantity() <= AllowedPad) {
89  assert(!DiffPad.isNegative() && "DiffPad should not be negative");
90  // There is not enough excess padding to trigger a warning.
91  return;
92  }
93  reportRecord(RD, BaselinePad, OptimalPad);
94  }
95 
96  /// \brief Look for arrays of overly padded types. If the padding of the
97  /// array type exceeds AllowedPad, then generate a report.
98  void visitVariable(const VarDecl *VD) const {
99  const ArrayType *ArrTy = VD->getType()->getAsArrayTypeUnsafe();
100  if (ArrTy == nullptr)
101  return;
102  uint64_t Elts = 0;
103  if (const ConstantArrayType *CArrTy = dyn_cast<ConstantArrayType>(ArrTy))
104  Elts = CArrTy->getSize().getZExtValue();
105  if (Elts == 0)
106  return;
107  const RecordType *RT = ArrTy->getElementType()->getAs<RecordType>();
108  if (RT == nullptr)
109  return;
110 
111  // TODO: Recurse into the fields and base classes to see if any
112  // of those have excess padding.
113  visitRecord(RT->getDecl(), Elts);
114  }
115 
116  bool shouldSkipDecl(const RecordDecl *RD) const {
117  auto Location = RD->getLocation();
118  // If the construct doesn't have a source file, then it's not something
119  // we want to diagnose.
120  if (!Location.isValid())
121  return true;
123  BR->getSourceManager().getFileCharacteristic(Location);
124  // Throw out all records that come from system headers.
125  if (Kind != SrcMgr::C_User)
126  return true;
127 
128  // Not going to attempt to optimize unions.
129  if (RD->isUnion())
130  return true;
131  // How do you reorder fields if you haven't got any?
132  if (RD->field_empty())
133  return true;
134  if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
135  // Tail padding with base classes ends up being very complicated.
136  // We will skip objects with base classes for now.
137  if (CXXRD->getNumBases() != 0)
138  return true;
139  // Virtual bases are complicated, skipping those for now.
140  if (CXXRD->getNumVBases() != 0)
141  return true;
142  // Can't layout a template, so skip it. We do still layout the
143  // instantiations though.
144  if (CXXRD->getTypeForDecl()->isDependentType())
145  return true;
146  if (CXXRD->getTypeForDecl()->isInstantiationDependentType())
147  return true;
148  }
149  auto IsTrickyField = [](const FieldDecl *FD) -> bool {
150  // Bitfield layout is hard.
151  if (FD->isBitField())
152  return true;
153 
154  // Variable length arrays are tricky too.
155  QualType Ty = FD->getType();
156  if (Ty->isIncompleteArrayType())
157  return true;
158  return false;
159  };
160 
161  if (std::any_of(RD->field_begin(), RD->field_end(), IsTrickyField))
162  return true;
163  return false;
164  }
165 
166  static CharUnits calculateBaselinePad(const RecordDecl *RD,
167  const ASTContext &ASTContext,
168  const ASTRecordLayout &RL) {
169  CharUnits PaddingSum;
171  for (const FieldDecl *FD : RD->fields()) {
172  // This checker only cares about the padded size of the
173  // field, and not the data size. If the field is a record
174  // with tail padding, then we won't put that number in our
175  // total because reordering fields won't fix that problem.
176  CharUnits FieldSize = ASTContext.getTypeSizeInChars(FD->getType());
177  auto FieldOffsetBits = RL.getFieldOffset(FD->getFieldIndex());
178  CharUnits FieldOffset = ASTContext.toCharUnitsFromBits(FieldOffsetBits);
179  PaddingSum += (FieldOffset - Offset);
180  Offset = FieldOffset + FieldSize;
181  }
182  PaddingSum += RL.getSize() - Offset;
183  return PaddingSum;
184  }
185 
186  /// Optimal padding overview:
187  /// 1. Find a close approximation to where we can place our first field.
188  /// This will usually be at offset 0.
189  /// 2. Try to find the best field that can legally be placed at the current
190  /// offset.
191  /// a. "Best" is the largest alignment that is legal, but smallest size.
192  /// This is to account for overly aligned types.
193  /// 3. If no fields can fit, pad by rounding the current offset up to the
194  /// smallest alignment requirement of our fields. Measure and track the
195  // amount of padding added. Go back to 2.
196  /// 4. Increment the current offset by the size of the chosen field.
197  /// 5. Remove the chosen field from the set of future possibilities.
198  /// 6. Go back to 2 if there are still unplaced fields.
199  /// 7. Add tail padding by rounding the current offset up to the structure
200  /// alignment. Track the amount of padding added.
201 
202  static CharUnits calculateOptimalPad(const RecordDecl *RD,
203  const ASTContext &ASTContext,
204  const ASTRecordLayout &RL) {
205  struct CharUnitPair {
206  CharUnits Align;
207  CharUnits Size;
208  bool operator<(const CharUnitPair &RHS) const {
209  // Order from small alignments to large alignments,
210  // then large sizes to small sizes.
211  return std::make_pair(Align, -Size) <
212  std::make_pair(RHS.Align, -RHS.Size);
213  }
214  };
216  auto GatherSizesAndAlignments = [](const FieldDecl *FD) {
217  CharUnitPair RetVal;
218  auto &Ctx = FD->getASTContext();
219  std::tie(RetVal.Size, RetVal.Align) =
220  Ctx.getTypeInfoInChars(FD->getType());
221  assert(llvm::isPowerOf2_64(RetVal.Align.getQuantity()));
222  if (auto Max = FD->getMaxAlignment())
223  RetVal.Align = std::max(Ctx.toCharUnitsFromBits(Max), RetVal.Align);
224  return RetVal;
225  };
226  std::transform(RD->field_begin(), RD->field_end(),
227  std::back_inserter(Fields), GatherSizesAndAlignments);
228  std::sort(Fields.begin(), Fields.end());
229 
230  // This lets us skip over vptrs and non-virtual bases,
231  // so that we can just worry about the fields in our object.
232  // Note that this does cause us to miss some cases where we
233  // could pack more bytes in to a base class's tail padding.
234  CharUnits NewOffset = ASTContext.toCharUnitsFromBits(RL.getFieldOffset(0));
235  CharUnits NewPad;
236 
237  while (!Fields.empty()) {
238  unsigned TrailingZeros =
239  llvm::countTrailingZeros((unsigned long long)NewOffset.getQuantity());
240  // If NewOffset is zero, then countTrailingZeros will be 64. Shifting
241  // 64 will overflow our unsigned long long. Shifting 63 will turn
242  // our long long (and CharUnits internal type) negative. So shift 62.
243  long long CurAlignmentBits = 1ull << (std::min)(TrailingZeros, 62u);
244  CharUnits CurAlignment = CharUnits::fromQuantity(CurAlignmentBits);
245  CharUnitPair InsertPoint = {CurAlignment, CharUnits::Zero()};
246  auto CurBegin = Fields.begin();
247  auto CurEnd = Fields.end();
248 
249  // In the typical case, this will find the last element
250  // of the vector. We won't find a middle element unless
251  // we started on a poorly aligned address or have an overly
252  // aligned field.
253  auto Iter = std::upper_bound(CurBegin, CurEnd, InsertPoint);
254  if (Iter != CurBegin) {
255  // We found a field that we can layout with the current alignment.
256  --Iter;
257  NewOffset += Iter->Size;
258  Fields.erase(Iter);
259  } else {
260  // We are poorly aligned, and we need to pad in order to layout another
261  // field. Round up to at least the smallest field alignment that we
262  // currently have.
263  CharUnits NextOffset = NewOffset.alignTo(Fields[0].Align);
264  NewPad += NextOffset - NewOffset;
265  NewOffset = NextOffset;
266  }
267  }
268  // Calculate tail padding.
269  CharUnits NewSize = NewOffset.alignTo(RL.getAlignment());
270  NewPad += NewSize - NewOffset;
271  return NewPad;
272  }
273 
274  void reportRecord(const RecordDecl *RD, CharUnits BaselinePad,
275  CharUnits TargetPad) const {
276  if (!PaddingBug)
277  PaddingBug =
278  llvm::make_unique<BugType>(this, "Excessive Padding", "Performance");
279 
280  SmallString<100> Buf;
281  llvm::raw_svector_ostream Os(Buf);
282 
283  Os << "Excessive padding in '";
284  Os << QualType::getAsString(RD->getTypeForDecl(), Qualifiers()) << "'";
285 
286  if (auto *TSD = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
287  // TODO: make this show up better in the console output and in
288  // the HTML. Maybe just make it show up in HTML like the path
289  // diagnostics show.
290  SourceLocation ILoc = TSD->getPointOfInstantiation();
291  if (ILoc.isValid())
292  Os << " instantiated here: "
293  << ILoc.printToString(BR->getSourceManager());
294  }
295 
296  Os << " (" << BaselinePad.getQuantity() << " padding bytes, where "
297  << TargetPad.getQuantity() << " is optimal). Consider reordering "
298  << "the fields or adding explicit padding members.";
299 
300  PathDiagnosticLocation CELoc =
301  PathDiagnosticLocation::create(RD, BR->getSourceManager());
302 
303  auto Report = llvm::make_unique<BugReport>(*PaddingBug, Os.str(), CELoc);
304  Report->setDeclWithIssue(RD);
305  Report->addRange(RD->getSourceRange());
306 
307  BR->emitReport(std::move(Report));
308  }
309 };
310 }
311 
312 void ento::registerPaddingChecker(CheckerManager &Mgr) {
313  Mgr.registerChecker<PaddingChecker>();
314 }
bool isNegative() const
isNegative - Test whether the quantity is less than zero.
Definition: CharUnits.h:125
std::string printToString(const SourceManager &SM) const
A (possibly-)qualified type.
Definition: Type.h:598
CharUnits getAlignment() const
getAlignment - Get the record alignment in characters.
Definition: RecordLayout.h:167
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
Defines the C++ template declaration subclasses.
std::string getAsString() const
Definition: Type.h:924
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2456
CharUnits alignTo(const CharUnits &Align) const
alignTo - Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is ...
Definition: CharUnits.h:184
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:768
field_iterator field_begin() const
Definition: Decl.cpp:3767
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:78
The collection of all-type qualifiers we support.
Definition: Type.h:117
RecordDecl - Represents a struct/union/class.
Definition: Decl.h:3253
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
Definition: Decl.h:2293
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
uint32_t Offset
Definition: CacheTokens.cpp:44
field_range fields() const
Definition: Decl.h:3382
RecordDecl * getDecl() const
Definition: Type.h:3716
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
Definition: RecordLayout.h:177
static PathDiagnosticLocation create(const Decl *D, const SourceManager &SM)
Create a location corresponding to the given declaration.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
A class that does preordor or postorder depth-first traversal on the entire Clang AST and visits each...
QualType getType() const
Definition: Decl.h:599
field_iterator field_end() const
Definition: Decl.h:3385
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type...
Definition: Type.h:5835
bool isUnion() const
Definition: Decl.h:2939
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:34
const Type * getTypeForDecl() const
Definition: Decl.h:2590
AnalyzerOptions & getAnalyzerOptions() override
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
char __ovld __cnfn min(char x, char y)
Returns y if y < x, otherwise it returns x.
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
BugReporter is a utility class for generating PathDiagnostics for analysis.
Definition: BugReporter.h:388
Kind
CHECKER * registerChecker()
Used to register checkers.
Encodes a location in the source.
CharUnits getSize() const
getSize - Get the record size in characters.
Definition: RecordLayout.h:170
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceRange getSourceRange() const override LLVM_READONLY
Definition: Decl.cpp:3527
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
bool field_empty() const
Definition: Decl.h:3391
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:3707
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:5818
char __ovld __cnfn max(char x, char y)
Returns y if x < y, otherwise it returns x.
bool isIncompleteArrayType() const
Definition: Type.h:5527
TranslationUnitDecl - The top declaration context.
Definition: Decl.h:80
QualType getElementType() const
Definition: Type.h:2490
int getOptionAsInteger(StringRef Name, int DefaultVal, const ento::CheckerBase *C=nullptr, bool SearchInParents=false)
Interprets an option's string value as an integer value.
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:2512