|
| 1 | +// ==-- PointerSizeAssumptionsChecker.cpp -------------------------*- C++ -*-=// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This checker detects record fields that will not have precise bounds when |
| 10 | +// compiled with |
| 11 | +// -cheri-bounds=subobject-safe |
| 12 | +// due to big size and underaligned offset, as narrowed capability will not |
| 13 | +// be representable |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "clang/AST/StmtVisitor.h" |
| 18 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 19 | +#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 20 | +#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 21 | +#include "clang/StaticAnalyzer/Core/Checker.h" |
| 22 | +#include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 23 | +#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
| 24 | +#include "llvm/ADT/SmallString.h" |
| 25 | +#include "llvm/CHERI/CompressedCapability.h" |
| 26 | +#include "llvm/Support/raw_ostream.h" |
| 27 | + |
| 28 | +using namespace clang; |
| 29 | +using namespace ento; |
| 30 | + |
| 31 | +namespace { |
| 32 | +class SubObjectRepresentabilityChecker |
| 33 | + : public Checker<check::ASTDecl<RecordDecl>> { |
| 34 | +public: |
| 35 | + void checkASTDecl(const RecordDecl *R, AnalysisManager &mgr, |
| 36 | + BugReporter &BR) const; |
| 37 | +}; |
| 38 | + |
| 39 | +} // namespace |
| 40 | + |
| 41 | +void SubObjectRepresentabilityChecker::checkASTDecl(const RecordDecl *R, |
| 42 | + AnalysisManager &mgr, |
| 43 | + BugReporter &BR) const { |
| 44 | + if (!R->isCompleteDefinition()) |
| 45 | + return; |
| 46 | + |
| 47 | + if (!R->getLocation().isValid()) |
| 48 | + return; |
| 49 | + |
| 50 | + /* |
| 51 | + SrcMgr::CharacteristicKind Kind = |
| 52 | + BR.getSourceManager().getFileCharacteristic(Location); |
| 53 | + // Ignore records in system headers |
| 54 | + if (Kind != SrcMgr::C_User) |
| 55 | + return; |
| 56 | + */ |
| 57 | + |
| 58 | + for (FieldDecl *D : R->fields()) { |
| 59 | + QualType T = D->getType(); |
| 60 | + |
| 61 | + ASTContext &ASTCtx = BR.getContext(); |
| 62 | + uint64_t Offset = ASTCtx.getFieldOffset(D) / 8; |
| 63 | + if (Offset > 0) { |
| 64 | + uint64_t Size = ASTCtx.getTypeSize(T) / 8; |
| 65 | + uint64_t ReqAlign = llvm::CompressedCapability::GetRequiredAlignment( |
| 66 | + Size, llvm::CompressedCapability::Cheri128) |
| 67 | + .value(); |
| 68 | + if (1 << llvm::countr_zero(Offset) < ReqAlign) { |
| 69 | + /* Emit warning */ |
| 70 | + SmallString<1024> Err; |
| 71 | + llvm::raw_svector_ostream OS(Err); |
| 72 | + const PrintingPolicy &PP = ASTCtx.getPrintingPolicy(); |
| 73 | + OS << "Field '"; |
| 74 | + D->getNameForDiagnostic(OS, PP, false); |
| 75 | + OS << "' of type '" << T.getAsString(PP) << "'"; |
| 76 | + OS << " (size " << Size << ")"; |
| 77 | + OS << " requires " << ReqAlign << " byte alignment for precise bounds;"; |
| 78 | + OS << " field offset is " << Offset; |
| 79 | + |
| 80 | + // Note that this will fire for every translation unit that uses this |
| 81 | + // class. This is suboptimal, but at least scan-build will merge |
| 82 | + // duplicate HTML reports. |
| 83 | + PathDiagnosticLocation L = |
| 84 | + PathDiagnosticLocation::createBegin(D, BR.getSourceManager()); |
| 85 | + BR.EmitBasicReport(R, this, "Field with imprecise subobject bounds", |
| 86 | + "CHERI portability", OS.str(), L); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void ento::registerSubObjectRepresentabilityChecker(CheckerManager &mgr) { |
| 93 | + mgr.registerChecker<SubObjectRepresentabilityChecker>(); |
| 94 | +} |
| 95 | + |
| 96 | +bool ento::shouldRegisterSubObjectRepresentabilityChecker( |
| 97 | + const CheckerManager &mgr) { |
| 98 | + return true; |
| 99 | +} |
0 commit comments