LLVM  4.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/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;
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> {
104  static AnalysisKey Key;
105 
106 public:
107  struct Result {
109  };
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 
137 };
138 
139 
140 } // End llvm namespace
141 
142 #endif
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
Result run(Module &M, ModuleAnalysisManager &)
Definition: Verifier.cpp:4796
FunctionPass * createVerifierPass(bool FatalErrors=true)
Definition: Verifier.cpp:4791
Metadata node.
Definition: Metadata.h:830
Check a module for errors, and report separate error states for IR and debug info errors...
Definition: Verifier.h:102
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: Verifier.cpp:4808
#define F(x, y, z)
Definition: MD5.cpp:51
VerifierPass(bool FatalErrors=true)
Definition: Verifier.h:133
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:311
TBAAVerifier(VerifierSupport *Diagnostic=nullptr)
Definition: Verifier.h:70
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:328
uint32_t Offset
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:4707
Create a verifier pass.
Definition: Verifier.h:129
Class for arbitrary precision integers.
Definition: APInt.h:77
bool verifyModule(const Module &M, raw_ostream *OS=nullptr, bool *BrokenDebugInfo=nullptr)
Check a module for errors.
Definition: Verifier.cpp:4441
#define I(x, y, z)
Definition: MD5.cpp:54
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
Definition: Verifier.cpp:4430
Verify that the TBAA Metadatas are valid.
Definition: Verifier.h:36
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:64