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