Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:11

0001 //===- Verifier.h - LLVM IR Verifier ----------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the function verifier interface, that can be used for
0010 // validation checking of input to the system, and for checking that
0011 // transformations haven't done something bad.
0012 //
0013 // Note that this does not provide full 'java style' security and verifications,
0014 // instead it just tries to ensure that code is well formed.
0015 //
0016 // To see what specifically is checked, look at the top of Verifier.cpp
0017 //
0018 //===----------------------------------------------------------------------===//
0019 
0020 #ifndef LLVM_IR_VERIFIER_H
0021 #define LLVM_IR_VERIFIER_H
0022 
0023 #include "llvm/ADT/DenseMap.h"
0024 #include "llvm/IR/PassManager.h"
0025 #include <utility>
0026 
0027 namespace llvm {
0028 
0029 class APInt;
0030 class Function;
0031 class FunctionPass;
0032 class Instruction;
0033 class MDNode;
0034 class Module;
0035 class raw_ostream;
0036 struct VerifierSupport;
0037 
0038 /// Verify that the TBAA Metadatas are valid.
0039 class TBAAVerifier {
0040   VerifierSupport *Diagnostic = nullptr;
0041 
0042   /// Helper to diagnose a failure
0043   template <typename... Tys> void CheckFailed(Tys &&... Args);
0044 
0045   /// Cache of TBAA base nodes that have already been visited.  This cachce maps
0046   /// a node that has been visited to a pair (IsInvalid, BitWidth) where
0047   ///
0048   ///  \c IsInvalid is true iff the node is invalid.
0049   ///  \c BitWidth, if non-zero, is the bitwidth of the integer used to denoting
0050   ///    the offset of the access.  If zero, only a zero offset is allowed.
0051   ///
0052   /// \c BitWidth has no meaning if \c IsInvalid is true.
0053   using TBAABaseNodeSummary = std::pair<bool, unsigned>;
0054   DenseMap<const MDNode *, TBAABaseNodeSummary> TBAABaseNodes;
0055 
0056   /// Maps an alleged scalar TBAA node to a boolean that is true if the said
0057   /// TBAA node is a valid scalar TBAA node or false otherwise.
0058   DenseMap<const MDNode *, bool> TBAAScalarNodes;
0059 
0060   /// \name Helper functions used by \c visitTBAAMetadata.
0061   /// @{
0062   MDNode *getFieldNodeFromTBAABaseNode(Instruction &I, const MDNode *BaseNode,
0063                                        APInt &Offset, bool IsNewFormat);
0064   TBAAVerifier::TBAABaseNodeSummary verifyTBAABaseNode(Instruction &I,
0065                                                        const MDNode *BaseNode,
0066                                                        bool IsNewFormat);
0067   TBAABaseNodeSummary verifyTBAABaseNodeImpl(Instruction &I,
0068                                              const MDNode *BaseNode,
0069                                              bool IsNewFormat);
0070 
0071   bool isValidScalarTBAANode(const MDNode *MD);
0072   /// @}
0073 
0074 public:
0075   TBAAVerifier(VerifierSupport *Diagnostic = nullptr)
0076       : Diagnostic(Diagnostic) {}
0077   /// Visit an instruction and return true if it is valid, return false if an
0078   /// invalid TBAA is attached.
0079   bool visitTBAAMetadata(Instruction &I, const MDNode *MD);
0080 };
0081 
0082 /// Check a function for errors, useful for use when debugging a
0083 /// pass.
0084 ///
0085 /// If there are no errors, the function returns false. If an error is found,
0086 /// a message describing the error is written to OS (if non-null) and true is
0087 /// returned.
0088 bool verifyFunction(const Function &F, raw_ostream *OS = nullptr);
0089 
0090 /// Check a module for errors.
0091 ///
0092 /// If there are no errors, the function returns false. If an error is
0093 /// found, a message describing the error is written to OS (if
0094 /// non-null) and true is returned.
0095 ///
0096 /// \return true if the module is broken. If BrokenDebugInfo is
0097 /// supplied, DebugInfo verification failures won't be considered as
0098 /// error and instead *BrokenDebugInfo will be set to true. Debug
0099 /// info errors can be "recovered" from by stripping the debug info.
0100 bool verifyModule(const Module &M, raw_ostream *OS = nullptr,
0101                   bool *BrokenDebugInfo = nullptr);
0102 
0103 FunctionPass *createVerifierPass(bool FatalErrors = true);
0104 
0105 /// Check a module for errors, and report separate error states for IR
0106 /// and debug info errors.
0107 class VerifierAnalysis : public AnalysisInfoMixin<VerifierAnalysis> {
0108   friend AnalysisInfoMixin<VerifierAnalysis>;
0109 
0110   static AnalysisKey Key;
0111 
0112 public:
0113   struct Result {
0114     bool IRBroken, DebugInfoBroken;
0115   };
0116 
0117   Result run(Module &M, ModuleAnalysisManager &);
0118   Result run(Function &F, FunctionAnalysisManager &);
0119   static bool isRequired() { return true; }
0120 };
0121 
0122 /// Create a verifier pass.
0123 ///
0124 /// Check a module or function for validity. This is essentially a pass wrapped
0125 /// around the above verifyFunction and verifyModule routines and
0126 /// functionality. When the pass detects a verification error it is always
0127 /// printed to stderr, and by default they are fatal. You can override that by
0128 /// passing \c false to \p FatalErrors.
0129 ///
0130 /// Note that this creates a pass suitable for the legacy pass manager. It has
0131 /// nothing to do with \c VerifierPass.
0132 class VerifierPass : public PassInfoMixin<VerifierPass> {
0133   bool FatalErrors;
0134 
0135 public:
0136   explicit VerifierPass(bool FatalErrors = true) : FatalErrors(FatalErrors) {}
0137 
0138   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0139   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
0140   static bool isRequired() { return true; }
0141 };
0142 
0143 } // end namespace llvm
0144 
0145 #endif // LLVM_IR_VERIFIER_H