LLVM  5.0.0
Verifier.h
Go to the documentation of this file.
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>;
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> {
108 
109  static AnalysisKey Key;
110 
111 public:
112  struct Result {
114  };
115 
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 
143 };
144 
145 } // end namespace llvm
146 
147 #endif // LLVM_IR_VERIFIER_H
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
Result run(Module &M, ModuleAnalysisManager &)
Definition: Verifier.cpp:4979
FunctionPass * createVerifierPass(bool FatalErrors=true)
Definition: Verifier.cpp:4974
Metadata node.
Definition: Metadata.h:862
Check a module for errors, and report separate error states for IR and debug info errors...
Definition: Verifier.h:106
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: Verifier.cpp:4991
#define F(x, y, z)
Definition: MD5.cpp:55
VerifierPass(bool FatalErrors=true)
Definition: Verifier.h:139
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
TBAAVerifier(VerifierSupport *Diagnostic=nullptr)
Definition: Verifier.h:74
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:382
bool visitTBAAMetadata(Instruction &I, const MDNode *MD)
Visit an instruction and return true if it is valid, return false if an invalid TBAA is attached...
Definition: Verifier.cpp:4890
Create a verifier pass.
Definition: Verifier.h:135
Class for arbitrary precision integers.
Definition: APInt.h:69
bool verifyModule(const Module &M, raw_ostream *OS=nullptr, bool *BrokenDebugInfo=nullptr)
Check a module for errors.
Definition: Verifier.cpp:4624
#define I(x, y, z)
Definition: MD5.cpp:58
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
Definition: Verifier.cpp:4613
Verify that the TBAA Metadatas are valid.
Definition: Verifier.h:40
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70