1 //===- Verifier.h - LLVM IR Verifier ----------------------------*- 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 the function verifier interface, that can be used for some 11 // sanity checking of input to the system, and for checking that transformations 12 // haven't done something bad. 13 // 14 // Note that this does not provide full 'java style' security and verifications, 15 // instead it just tries to ensure that code is well formed. 16 // 17 // To see what specifically is checked, look at the top of Verifier.cpp 18 // 19 //===----------------------------------------------------------------------===// 20 21 #ifndef LLVM_IR_VERIFIER_H 22 #define LLVM_IR_VERIFIER_H 23 24 #include "llvm/IR/PassManager.h" 25 26 namespace llvm { 27 28 class Function; 29 class FunctionPass; 30 class ModulePass; 31 class Module; 32 class raw_ostream; 33 struct VerifierSupport; 34 35 /// Verify that the TBAA Metadatas are valid. 36 class TBAAVerifier { 37 VerifierSupport *Diagnostic = nullptr; 38 39 /// Helper to diagnose a failure 40 template <typename... Tys> void CheckFailed(Tys &&... Args); 41 42 /// Cache of TBAA base nodes that have already been visited. This cachce maps 43 /// a node that has been visited to a pair (IsInvalid, BitWidth) where 44 /// 45 /// \c IsInvalid is true iff the node is invalid. 46 /// \c BitWidth, if non-zero, is the bitwidth of the integer used to denoting 47 /// the offset of the access. If zero, only a zero offset is allowed. 48 /// 49 /// \c BitWidth has no meaning if \c IsInvalid is true. 50 typedef std::pair<bool, unsigned> TBAABaseNodeSummary; 51 DenseMap<const MDNode *, TBAABaseNodeSummary> TBAABaseNodes; 52 53 /// Maps an alleged scalar TBAA node to a boolean that is true if the said 54 /// TBAA node is a valid scalar TBAA node or false otherwise. 55 DenseMap<const MDNode *, bool> TBAAScalarNodes; 56 57 /// \name Helper functions used by \c visitTBAAMetadata. 58 /// @{ 59 MDNode *getFieldNodeFromTBAABaseNode(Instruction &I, const MDNode *BaseNode, 60 APInt &Offset); 61 TBAAVerifier::TBAABaseNodeSummary verifyTBAABaseNode(Instruction &I, 62 const MDNode *BaseNode); 63 TBAABaseNodeSummary verifyTBAABaseNodeImpl(Instruction &I, 64 const MDNode *BaseNode); 65 66 bool isValidScalarTBAANode(const MDNode *MD); 67 /// @} 68 69 public: 70 TBAAVerifier(VerifierSupport *Diagnostic = nullptr) 71 : Diagnostic(Diagnostic) {} 72 /// Visit an instruction and return true if it is valid, return false if an 73 /// invalid TBAA is attached. 74 bool visitTBAAMetadata(Instruction &I, const MDNode *MD); 75 }; 76 77 /// \brief Check a function for errors, useful for use when debugging a 78 /// pass. 79 /// 80 /// If there are no errors, the function returns false. If an error is found, 81 /// a message describing the error is written to OS (if non-null) and true is 82 /// returned. 83 bool verifyFunction(const Function &F, raw_ostream *OS = nullptr); 84 85 /// \brief Check a module for errors. 86 /// 87 /// If there are no errors, the function returns false. If an error is 88 /// found, a message describing the error is written to OS (if 89 /// non-null) and true is returned. 90 /// 91 /// \return true if the module is broken. If BrokenDebugInfo is 92 /// supplied, DebugInfo verification failures won't be considered as 93 /// error and instead *BrokenDebugInfo will be set to true. Debug 94 /// info errors can be "recovered" from by stripping the debug info. 95 bool verifyModule(const Module &M, raw_ostream *OS = nullptr, 96 bool *BrokenDebugInfo = nullptr); 97 98 FunctionPass *createVerifierPass(bool FatalErrors = true); 99 100 /// Check a module for errors, and report separate error states for IR 101 /// and debug info errors. 102 class VerifierAnalysis : public AnalysisInfoMixin<VerifierAnalysis> { 103 friend AnalysisInfoMixin<VerifierAnalysis>; 104 static AnalysisKey Key; 105 106 public: 107 struct Result { 108 bool IRBroken, DebugInfoBroken; 109 }; 110 Result run(Module &M, ModuleAnalysisManager &); 111 Result run(Function &F, FunctionAnalysisManager &); 112 }; 113 114 /// Check a module for errors, but report debug info errors separately. 115 /// Otherwise behaves as the normal verifyModule. Debug info errors can be 116 /// "recovered" from by stripping the debug info. 117 bool verifyModule(bool &BrokenDebugInfo, const Module &M, raw_ostream *OS); 118 119 /// \brief Create a verifier pass. 120 /// 121 /// Check a module or function for validity. This is essentially a pass wrapped 122 /// around the above verifyFunction and verifyModule routines and 123 /// functionality. When the pass detects a verification error it is always 124 /// printed to stderr, and by default they are fatal. You can override that by 125 /// passing \c false to \p FatalErrors. 126 /// 127 /// Note that this creates a pass suitable for the legacy pass manager. It has 128 /// nothing to do with \c VerifierPass. 129 class VerifierPass : public PassInfoMixin<VerifierPass> { 130 bool FatalErrors; 131 132 public: 133 explicit VerifierPass(bool FatalErrors = true) : FatalErrors(FatalErrors) {} 134 135 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 136 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 137 }; 138 139 140 } // End llvm namespace 141 142 #endif 143