LLVM  8.0.1
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, bool IsNewFormat);
65  TBAAVerifier::TBAABaseNodeSummary verifyTBAABaseNode(Instruction &I,
66  const MDNode *BaseNode,
67  bool IsNewFormat);
68  TBAABaseNodeSummary verifyTBAABaseNodeImpl(Instruction &I,
69  const MDNode *BaseNode,
70  bool IsNewFormat);
71 
72  bool isValidScalarTBAANode(const MDNode *MD);
73  /// @}
74 
75 public:
76  TBAAVerifier(VerifierSupport *Diagnostic = nullptr)
77  : Diagnostic(Diagnostic) {}
78  /// Visit an instruction and return true if it is valid, return false if an
79  /// invalid TBAA is attached.
80  bool visitTBAAMetadata(Instruction &I, const MDNode *MD);
81 };
82 
83 /// Check a function for errors, useful for use when debugging a
84 /// pass.
85 ///
86 /// If there are no errors, the function returns false. If an error is found,
87 /// a message describing the error is written to OS (if non-null) and true is
88 /// returned.
89 bool verifyFunction(const Function &F, raw_ostream *OS = nullptr);
90 
91 /// Check a module for errors.
92 ///
93 /// If there are no errors, the function returns false. If an error is
94 /// found, a message describing the error is written to OS (if
95 /// non-null) and true is returned.
96 ///
97 /// \return true if the module is broken. If BrokenDebugInfo is
98 /// supplied, DebugInfo verification failures won't be considered as
99 /// error and instead *BrokenDebugInfo will be set to true. Debug
100 /// info errors can be "recovered" from by stripping the debug info.
101 bool verifyModule(const Module &M, raw_ostream *OS = nullptr,
102  bool *BrokenDebugInfo = nullptr);
103 
104 FunctionPass *createVerifierPass(bool FatalErrors = true);
105 
106 /// Check a module for errors, and report separate error states for IR
107 /// and debug info errors.
108 class VerifierAnalysis : public AnalysisInfoMixin<VerifierAnalysis> {
110 
111  static AnalysisKey Key;
112 
113 public:
114  struct Result {
115  bool IRBroken, DebugInfoBroken;
116  };
117 
120 };
121 
122 /// Check a module for errors, but report debug info errors separately.
123 /// Otherwise behaves as the normal verifyModule. Debug info errors can be
124 /// "recovered" from by stripping the debug info.
125 bool verifyModule(bool &BrokenDebugInfo, const Module &M, raw_ostream *OS);
126 
127 /// Create a verifier pass.
128 ///
129 /// Check a module or function for validity. This is essentially a pass wrapped
130 /// around the above verifyFunction and verifyModule routines and
131 /// functionality. When the pass detects a verification error it is always
132 /// printed to stderr, and by default they are fatal. You can override that by
133 /// passing \c false to \p FatalErrors.
134 ///
135 /// Note that this creates a pass suitable for the legacy pass manager. It has
136 /// nothing to do with \c VerifierPass.
137 class VerifierPass : public PassInfoMixin<VerifierPass> {
138  bool FatalErrors;
139 
140 public:
141  explicit VerifierPass(bool FatalErrors = true) : FatalErrors(FatalErrors) {}
142 
145 };
146 
147 } // end namespace llvm
148 
149 #endif // LLVM_IR_VERIFIER_H
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
FunctionPass * createVerifierPass(bool FatalErrors=true)
Definition: Verifier.cpp:5236
Metadata node.
Definition: Metadata.h:864
F(f)
Check a module for errors, and report separate error states for IR and debug info errors...
Definition: Verifier.h:108
Key
PAL metadata keys.
VerifierPass(bool FatalErrors=true)
Definition: Verifier.h:141
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:366
TBAAVerifier(VerifierSupport *Diagnostic=nullptr)
Definition: Verifier.h:76
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:383
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:5128
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
Create a verifier pass.
Definition: Verifier.h:137
Class for arbitrary precision integers.
Definition: APInt.h:70
bool verifyModule(const Module &M, raw_ostream *OS=nullptr, bool *BrokenDebugInfo=nullptr)
Check a module for errors.
Definition: Verifier.cpp:4820
#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:4809
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:46
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:71
constexpr char Args[]
Key for Kernel::Metadata::mArgs.