llvm.org GIT mirror llvm / e608d69
[PM] Make the verifier work independently of any pass manager. This makes the 'verifyFunction' and 'verifyModule' functions totally independent operations on the LLVM IR. It also cleans up their API a bit by lifting the abort behavior into their clients and just using an optional raw_ostream parameter to control printing. The implementation of the verifier is now just an InstVisitor with no multiple inheritance. It also is significantly more const-correct, and hides the const violations internally. The two layers that force us to break const correctness are building a DomTree and dispatching through the InstVisitor. A new VerifierPass is used to implement the legacy pass manager interface in terms of the other pieces. The error messages produced may be slightly different now, and we may have slightly different short circuiting behavior with different usage models of the verifier, but generally everything works equivalently and this unblocks wiring the verifier up to the new pass manager. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199569 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 5 years ago
8 changed file(s) with 213 addition(s) and 211 deletion(s). Raw diff Collapse all Expand all
2727 class FunctionPass;
2828 class Module;
2929 class Function;
30
31 /// \brief An enumeration to specify the action to be taken if errors found.
32 ///
33 /// This enumeration is used in the functions below to indicate what should
34 /// happen if the verifier finds errors. Each of the functions that uses
35 /// this enumeration as an argument provides a default value for it. The
36 /// actions are listed below.
37 enum VerifierFailureAction {
38 AbortProcessAction, ///< verifyModule will print to stderr and abort()
39 PrintMessageAction, ///< verifyModule will print to stderr and return true
40 ReturnStatusAction ///< verifyModule will just return true
41 };
42
43 /// \brief Create a verifier pass.
44 ///
45 /// Check a module or function for validity. When the pass is used, the
46 /// action indicated by the \p action argument will be used if errors are
47 /// found.
48 FunctionPass *
49 createVerifierPass(VerifierFailureAction action = AbortProcessAction);
30 class raw_ostream;
5031
5132 /// \brief Check a function for errors, useful for use when debugging a
5233 /// pass.
5334 ///
5435 /// If there are no errors, the function returns false. If an error is found,
55 /// the action taken depends on the \p action parameter.
56 bool verifyFunction(const Function &F,
57 VerifierFailureAction action = AbortProcessAction);
36 /// a message describing the error is written to OS (if non-null) and false is
37 /// returned.
38 bool verifyFunction(const Function &F, raw_ostream *OS = 0);
5839
5940 /// \brief Check a module for errors.
6041 ///
6142 /// If there are no errors, the function returns false. If an error is found,
62 /// the action taken depends on the \p action parameter.
63 /// This should only be used for debugging, because it plays games with
64 /// PassManagers and stuff.
65 bool verifyModule(const Module &M,
66 VerifierFailureAction action = AbortProcessAction,
67 std::string *ErrorInfo = 0);
43 /// a message describing the error is written to OS (if non-null) and false is
44 /// returned.
45 bool verifyModule(const Module &M, raw_ostream *OS = 0);
46
47 /// \brief Create a verifier pass.
48 ///
49 /// Check a module or function for validity. This is essentially a pass wrapped
50 /// around the above verifyFunction and verifyModule routines and
51 /// functionality. When the pass detects a verification error it is always
52 /// printed to stderr, and by default they are fatal. You can override that by
53 /// passing \c false to \p FatalErrors.
54 FunctionPass *createVerifierPass(bool FatalErrors = true);
6855
6956 } // End llvm namespace
7057
255255 void initializeUnifyFunctionExitNodesPass(PassRegistry&);
256256 void initializeUnreachableBlockElimPass(PassRegistry&);
257257 void initializeUnreachableMachineBlockElimPass(PassRegistry&);
258 void initializeVerifierPass(PassRegistry&);
258 void initializeVerifierPassPass(PassRegistry&);
259259 void initializeVirtRegMapPass(PassRegistry&);
260260 void initializeVirtRegRewriterPass(PassRegistry&);
261261 void initializeInstSimplifierPass(PassRegistry&);
1010 #include "llvm-c/Initialization.h"
1111 #include "llvm/IR/Module.h"
1212 #include "llvm/IR/Verifier.h"
13 #include "llvm/Support/raw_ostream.h"
1314 #include "llvm/InitializePasses.h"
1415 #include "llvm/PassRegistry.h"
1516 #include
7172
7273 LLVMBool LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action,
7374 char **OutMessages) {
75 raw_ostream *DebugOS = Action != LLVMReturnStatusAction ? &errs() : 0;
7476 std::string Messages;
77 raw_string_ostream MsgsOS(Messages);
7578
76 LLVMBool Result = verifyModule(*unwrap(M),
77 static_cast(Action),
78 OutMessages? &Messages : 0);
79 LLVMBool Result = verifyModule(*unwrap(M), OutMessages ? &MsgsOS : DebugOS);
80
81 // Duplicate the output to stderr.
82 if (DebugOS && OutMessages)
83 *DebugOS << MsgsOS.str();
84
85 if (Action == LLVMAbortProcessAction && Result)
86 report_fatal_error("Broken module found, compilation aborted!");
7987
8088 if (OutMessages)
81 *OutMessages = strdup(Messages.c_str());
89 *OutMessages = strdup(MsgsOS.str().c_str());
8290
8391 return Result;
8492 }
8593
8694 LLVMBool LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action) {
87 return verifyFunction(*unwrap(Fn),
88 static_cast(Action));
95 LLVMBool Result = verifyFunction(
96 *unwrap(Fn), Action != LLVMReturnStatusAction ? &errs() : 0);
97
98 if (Action == LLVMAbortProcessAction && Result)
99 report_fatal_error("Broken function found, compilation aborted!");
100
101 return Result;
89102 }
90103
91104 void LLVMViewFunctionCFG(LLVMValueRef Fn) {
4343 initializePrintModulePassWrapperPass(Registry);
4444 initializePrintFunctionPassWrapperPass(Registry);
4545 initializePrintBasicBlockPassPass(Registry);
46 initializeVerifierPass(Registry);
46 initializeVerifierPassPass(Registry);
4747 }
4848
4949 void LLVMInitializeCore(LLVMPassRegistryRef R) {
7979 cl::init(true));
8080
8181 namespace {
82 struct Verifier : public FunctionPass, public InstVisitor {
83 static char ID;
84
85 // What to do if verification fails.
86 VerifierFailureAction Action;
87
88 Module *M;
82 class Verifier : public InstVisitor {
83 friend class InstVisitor;
84
85 raw_ostream &OS;
86 const Module *M;
8987 LLVMContext *Context;
9088 const DataLayout *DL;
9189 DominatorTree DT;
92
93 bool Broken;
94 std::string Messages;
95 raw_string_ostream MessagesStr;
9690
9791 /// \brief When verifying a basic block, keep track of all of the
9892 /// instructions we have seen so far.
112106 /// \brief Finder keeps track of all debug info MDNodes in a Module.
113107 DebugInfoFinder Finder;
114108
115 Verifier()
116 : FunctionPass(ID), Action(AbortProcessAction), M(0), Context(0), DL(0),
117 Broken(false), MessagesStr(Messages), PersonalityFn(0) {
118 initializeVerifierPass(*PassRegistry::getPassRegistry());
119 }
120 explicit Verifier(VerifierFailureAction Action)
121 : FunctionPass(ID), Action(Action), M(0), Context(0), DL(0),
122 Broken(false), MessagesStr(Messages), PersonalityFn(0) {
123 initializeVerifierPass(*PassRegistry::getPassRegistry());
124 }
125
126 bool doInitialization(Module &M) {
127 this->M = &M;
128 Context = &M.getContext();
129
130 DL = getAnalysisIfAvailable();
131
132 // We must abort before returning back to the pass manager, or else the
133 // pass manager may try to run other passes on the broken module.
134 return abortIfBroken();
135 }
136
137 bool runOnFunction(Function &F) {
138 Broken = false;
109 /// \brief Track the brokenness of the module while recursively visiting.
110 bool Broken;
111
112 public:
113 explicit Verifier(raw_ostream &OS = dbgs())
114 : OS(OS), M(0), Context(0), DL(0), PersonalityFn(0), Broken(false) {}
115
116 bool verify(const Function &F) {
117 M = F.getParent();
118 Context = &M->getContext();
139119
140120 // First ensure the function is well-enough formed to compute dominance
141121 // information.
142 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
122 if (F.empty()) {
123 OS << "Function '" << F.getName()
124 << "' does not contain an entry block!\n";
125 return false;
126 }
127 for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I) {
143128 if (I->empty() || !I->back().isTerminator()) {
144 dbgs() << "Basic Block in function '" << F.getName()
145 << "' does not have terminator!\n";
146 I->printAsOperand(dbgs(), true);
147 dbgs() << "\n";
148 Broken = true;
129 OS << "Basic Block in function '" << F.getName()
130 << "' does not have terminator!\n";
131 I->printAsOperand(OS, true);
132 OS << "\n";
133 return false;
149134 }
150135 }
151 if (Broken)
152 return abortIfBroken();
153136
154137 // Now directly compute a dominance tree. We don't rely on the pass
155138 // manager to provide this as it isolates us from a potentially
156139 // out-of-date dominator tree and makes it significantly more complex to
157140 // run this code outside of a pass manager.
158 DT.recalculate(F);
159
160 M = F.getParent();
161 if (!Context)
162 Context = &F.getContext();
141 // FIXME: It's really gross that we have to cast away constness here.
142 DT.recalculate(const_cast(F));
163143
164144 Finder.reset();
165 visit(F);
145 Broken = false;
146 // FIXME: We strip const here because the inst visitor strips const.
147 visit(const_cast(F));
166148 InstsInThisBlock.clear();
167149 PersonalityFn = 0;
168150
170152 // Verify Debug Info.
171153 verifyDebugInfo();
172154
173 // We must abort before returning back to the pass manager, or else the
174 // pass manager may try to run other passes on the broken module.
175 return abortIfBroken();
176 }
177
178 bool doFinalization(Module &M) {
155 return !Broken;
156 }
157
158 bool verify(const Module &M) {
159 this->M = &M;
160 Context = &M.getContext();
161 Finder.reset();
162 Broken = false;
163
179164 // Scan through, checking all of the external function's linkage now...
180 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
165 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
181166 visitGlobalValue(*I);
182167
183168 // Check to make sure function prototypes are okay.
185170 visitFunction(*I);
186171 }
187172
188 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
173 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
189174 I != E; ++I)
190175 visitGlobalVariable(*I);
191176
192 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
193 ++I)
177 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
178 I != E; ++I)
194179 visitGlobalAlias(*I);
195180
196 for (Module::named_metadata_iterator I = M.named_metadata_begin(),
197 E = M.named_metadata_end();
181 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
182 E = M.named_metadata_end();
198183 I != E; ++I)
199184 visitNamedMDNode(*I);
200185
208193 verifyDebugInfo();
209194 }
210195
211 // If the module is broken, abort at this time.
212 return abortIfBroken();
213 }
214
215 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
216 AU.setPreservesAll();
217 }
218
219 /// abortIfBroken - If the module is broken and we are supposed to abort on
220 /// this condition, do so.
221 ///
222 bool abortIfBroken() {
223 if (!Broken)
224 return false;
225 MessagesStr << "Broken module found, ";
226 switch (Action) {
227 case AbortProcessAction:
228 MessagesStr << "compilation aborted!\n";
229 dbgs() << MessagesStr.str();
230 // Client should choose different reaction if abort is not desired
231 abort();
232 case PrintMessageAction:
233 MessagesStr << "verification continues.\n";
234 dbgs() << MessagesStr.str();
235 return false;
236 case ReturnStatusAction:
237 MessagesStr << "compilation terminated.\n";
238 return true;
239 }
240 llvm_unreachable("Invalid action");
241 }
242
196 return !Broken;
197 }
198
199 private:
243200 // Verification methods...
244 void visitGlobalValue(GlobalValue &GV);
245 void visitGlobalVariable(GlobalVariable &GV);
246 void visitGlobalAlias(GlobalAlias &GA);
247 void visitNamedMDNode(NamedMDNode &NMD);
201 void visitGlobalValue(const GlobalValue &GV);
202 void visitGlobalVariable(const GlobalVariable &GV);
203 void visitGlobalAlias(const GlobalAlias &GA);
204 void visitNamedMDNode(const NamedMDNode &NMD);
248205 void visitMDNode(MDNode &MD, Function *F);
249 void visitModuleIdents(Module &M);
250 void visitModuleFlags(Module &M);
251 void visitModuleFlag(MDNode *Op, DenseMap &SeenIDs,
252 SmallVectorImpl &Requirements);
253 void visitFunction(Function &F);
206 void visitModuleIdents(const Module &M);
207 void visitModuleFlags(const Module &M);
208 void visitModuleFlag(const MDNode *Op,
209 DenseMap &SeenIDs,
210 SmallVectorImpl &Requirements);
211 void visitFunction(const Function &F);
254212 void visitBasicBlock(BasicBlock &BB);
213
214 // InstVisitor overrides...
255215 using InstVisitor::visit;
256
257216 void visit(Instruction &I);
258217
259218 void visitTruncInst(TruncInst &I);
325284 if (!V)
326285 return;
327286 if (isa(V)) {
328 MessagesStr << *V << '\n';
287 OS << *V << '\n';
329288 } else {
330 V->printAsOperand(MessagesStr, true, M);
331 MessagesStr << '\n';
289 V->printAsOperand(OS, true, M);
290 OS << '\n';
332291 }
333292 }
334293
335294 void WriteType(Type *T) {
336295 if (!T)
337296 return;
338 MessagesStr << ' ' << *T;
297 OS << ' ' << *T;
339298 }
340299
341300 // CheckFailed - A check failed, so print out the condition and the message
344303 void CheckFailed(const Twine &Message, const Value *V1 = 0,
345304 const Value *V2 = 0, const Value *V3 = 0,
346305 const Value *V4 = 0) {
347 MessagesStr << Message.str() << "\n";
306 OS << Message.str() << "\n";
348307 WriteValue(V1);
349308 WriteValue(V2);
350309 WriteValue(V3);
354313
355314 void CheckFailed(const Twine &Message, const Value *V1, Type *T2,
356315 const Value *V3 = 0) {
357 MessagesStr << Message.str() << "\n";
316 OS << Message.str() << "\n";
358317 WriteValue(V1);
359318 WriteType(T2);
360319 WriteValue(V3);
362321 }
363322
364323 void CheckFailed(const Twine &Message, Type *T1, Type *T2 = 0, Type *T3 = 0) {
365 MessagesStr << Message.str() << "\n";
324 OS << Message.str() << "\n";
366325 WriteType(T1);
367326 WriteType(T2);
368327 WriteType(T3);
370329 }
371330 };
372331 } // End anonymous namespace
373
374 char Verifier::ID = 0;
375 INITIALIZE_PASS(Verifier, "verify", "Module Verifier", false, false)
376332
377333 // Assert - We know that cond should be true, if not print an error message.
378334 #define Assert(C, M) \
393349 }
394350
395351
396 void Verifier::visitGlobalValue(GlobalValue &GV) {
352 void Verifier::visitGlobalValue(const GlobalValue &GV) {
397353 Assert1(!GV.isDeclaration() ||
398354 GV.isMaterializable() ||
399355 GV.hasExternalLinkage() ||
407363 "Only global variables can have appending linkage!", &GV);
408364
409365 if (GV.hasAppendingLinkage()) {
410 GlobalVariable *GVar = dyn_cast(&GV);
366 const GlobalVariable *GVar = dyn_cast(&GV);
411367 Assert1(GVar && GVar->getType()->getElementType()->isArrayTy(),
412368 "Only global arrays can have appending linkage!", GVar);
413369 }
414370 }
415371
416 void Verifier::visitGlobalVariable(GlobalVariable &GV) {
372 void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
417373 if (GV.hasInitializer()) {
418374 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
419375 "Global variable initializer type does not match global "
458414 PointerType *PTy = dyn_cast(ATy->getElementType());
459415 Assert1(PTy, "wrong type for intrinsic global variable", &GV);
460416 if (GV.hasInitializer()) {
461 Constant *Init = GV.getInitializer();
462 ConstantArray *InitArray = dyn_cast(Init);
417 const Constant *Init = GV.getInitializer();
418 const ConstantArray *InitArray = dyn_cast(Init);
463419 Assert1(InitArray, "wrong initalizer for intrinsic global variable",
464420 Init);
465421 for (unsigned i = 0, e = InitArray->getNumOperands(); i != e; ++i) {
508464 visitGlobalValue(GV);
509465 }
510466
511 void Verifier::visitGlobalAlias(GlobalAlias &GA) {
467 void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
512468 Assert1(!GA.getName().empty(),
513469 "Alias name cannot be empty!", &GA);
514470 Assert1(GlobalAlias::isValidLinkage(GA.getLinkage()),
519475 "Alias and aliasee types should match!", &GA);
520476 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
521477
522 Constant *Aliasee = GA.getAliasee();
478 const Constant *Aliasee = GA.getAliasee();
523479
524480 if (!isa(Aliasee)) {
525 ConstantExpr *CE = dyn_cast(Aliasee);
481 const ConstantExpr *CE = dyn_cast(Aliasee);
526482 Assert1(CE &&
527483 (CE->getOpcode() == Instruction::BitCast ||
528484 CE->getOpcode() == Instruction::AddrSpaceCast ||
549505 visitGlobalValue(GA);
550506 }
551507
552 void Verifier::visitNamedMDNode(NamedMDNode &NMD) {
508 void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
553509 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) {
554510 MDNode *MD = NMD.getOperand(i);
555511 if (!MD)
597553 }
598554 }
599555
600 void Verifier::visitModuleIdents(Module &M) {
556 void Verifier::visitModuleIdents(const Module &M) {
601557 const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident");
602558 if (!Idents)
603559 return;
615571 }
616572 }
617573
618 void Verifier::visitModuleFlags(Module &M) {
574 void Verifier::visitModuleFlags(const Module &M) {
619575 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
620576 if (!Flags) return;
621577
622578 // Scan each flag, and track the flags and requirements.
623 DenseMap SeenIDs;
624 SmallVector Requirements;
579 DenseMap SeenIDs;
580 SmallVector Requirements;
625581 for (unsigned I = 0, E = Flags->getNumOperands(); I != E; ++I) {
626582 visitModuleFlag(Flags->getOperand(I), SeenIDs, Requirements);
627583 }
628584
629585 // Validate that the requirements in the module are valid.
630586 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
631 MDNode *Requirement = Requirements[I];
632 MDString *Flag = cast(Requirement->getOperand(0));
633 Value *ReqValue = Requirement->getOperand(1);
634
635 MDNode *Op = SeenIDs.lookup(Flag);
587 const MDNode *Requirement = Requirements[I];
588 const MDString *Flag = cast(Requirement->getOperand(0));
589 const Value *ReqValue = Requirement->getOperand(1);
590
591 const MDNode *Op = SeenIDs.lookup(Flag);
636592 if (!Op) {
637593 CheckFailed("invalid requirement on flag, flag is not present in module",
638594 Flag);
648604 }
649605 }
650606
651 void Verifier::visitModuleFlag(MDNode *Op, DenseMap&SeenIDs,
652 SmallVectorImpl &Requirements) {
607 void
608 Verifier::visitModuleFlag(const MDNode *Op,
609 DenseMap &SeenIDs,
610 SmallVectorImpl &Requirements) {
653611 // Each module flag should have three arguments, the merge behavior (a
654612 // constant int), the flag ID (an MDString), and the value.
655613 Assert1(Op->getNumOperands() == 3,
986944
987945 // visitFunction - Verify that a function is ok.
988946 //
989 void Verifier::visitFunction(Function &F) {
947 void Verifier::visitFunction(const Function &F) {
990948 // Check function arguments.
991949 FunctionType *FT = F.getFunctionType();
992950 unsigned NumArgs = F.arg_size();
10441002
10451003 // Check that the argument values match the function type for this function...
10461004 unsigned i = 0;
1047 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
1048 I != E; ++I, ++i) {
1005 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
1006 ++I, ++i) {
10491007 Assert2(I->getType() == FT->getParamType(i),
10501008 "Argument value does not match function argument type!",
10511009 I, FT->getParamType(i));
10671025 Assert1(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F);
10681026
10691027 // Check the entry node
1070 BasicBlock *Entry = &F.getEntryBlock();
1028 const BasicBlock *Entry = &F.getEntryBlock();
10711029 Assert1(pred_begin(Entry) == pred_end(Entry),
10721030 "Entry block to function must not have predecessors!", Entry);
10731031
10741032 // The address of the entry block cannot be taken, unless it is dead.
10751033 if (Entry->hasAddressTaken()) {
1076 Assert1(!BlockAddress::get(Entry)->isConstantUsed(),
1034 Assert1(!BlockAddress::lookup(Entry)->isConstantUsed(),
10771035 "blockaddress may not be used with the entry block!", Entry);
10781036 }
10791037 }
24012359 // Implement the public interfaces to this file...
24022360 //===----------------------------------------------------------------------===//
24032361
2404 FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
2405 return new Verifier(action);
2406 }
2407
2408 bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
2362 bool llvm::verifyFunction(const Function &f, raw_ostream *OS) {
24092363 Function &F = const_cast(f);
24102364 assert(!F.isDeclaration() && "Cannot verify external functions");
24112365
2412 FunctionPassManager FPM(F.getParent());
2413 Verifier *V = new Verifier(action);
2414 FPM.add(V);
2415 FPM.doInitialization();
2416 FPM.run(F);
2417 return V->Broken;
2418 }
2419
2420 bool llvm::verifyModule(const Module &M, VerifierFailureAction action,
2421 std::string *ErrorInfo) {
2422 PassManager PM;
2423 Verifier *V = new Verifier(action);
2424 PM.add(V);
2425 PM.run(const_cast(M));
2426
2427 if (ErrorInfo && V->Broken)
2428 *ErrorInfo = V->MessagesStr.str();
2429 return V->Broken;
2430 }
2366 raw_null_ostream NullStr;
2367 Verifier V(OS ? *OS : NullStr);
2368
2369 // Note that this function's return value is inverted from what you would
2370 // expect of a function called "verify".
2371 return !V.verify(F);
2372 }
2373
2374 bool llvm::verifyModule(const Module &M, raw_ostream *OS) {
2375 raw_null_ostream NullStr;
2376 Verifier V(OS ? *OS : NullStr);
2377
2378 bool Broken = false;
2379 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
2380 if (!I->isDeclaration())
2381 Broken |= !V.verify(*I);
2382
2383 // Note that this function's return value is inverted from what you would
2384 // expect of a function called "verify".
2385 return !V.verify(M) || Broken;
2386 }
2387
2388 namespace {
2389 struct VerifierPass : public FunctionPass {
2390 static char ID;
2391
2392 Verifier V;
2393 bool FatalErrors;
2394
2395 VerifierPass() : FunctionPass(ID), FatalErrors(true) {
2396 initializeVerifierPassPass(*PassRegistry::getPassRegistry());
2397 }
2398 explicit VerifierPass(bool FatalErrors)
2399 : FunctionPass(ID), V(dbgs()), FatalErrors(FatalErrors) {
2400 initializeVerifierPassPass(*PassRegistry::getPassRegistry());
2401 }
2402
2403 bool runOnFunction(Function &F) {
2404 if (!V.verify(F) && FatalErrors)
2405 report_fatal_error("Broken function found, compilation aborted!");
2406
2407 return false;
2408 }
2409
2410 bool doFinalization(Module &M) {
2411 if (!V.verify(M) && FatalErrors)
2412 report_fatal_error("Broken module found, compilation aborted!");
2413
2414 return false;
2415 }
2416
2417 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
2418 AU.setPreservesAll();
2419 }
2420 };
2421 }
2422
2423 char VerifierPass::ID = 0;
2424 INITIALIZE_PASS(VerifierPass, "verify", "Module Verifier", false, false)
2425
2426 FunctionPass *llvm::createVerifierPass(bool FatalErrors) {
2427 return new VerifierPass(FatalErrors);
2428 }
2429
None ; RUN: not llvm-as < %s 2> %t
1 ; RUN: FileCheck %s --input-file=%t
2 ; CHECK: Broken module
0 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
1 ; CHECK: assembly parsed, but does not verify as correct
32 ; PR7316
43
54 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:32"
9999 }
100100
101101 if (!DisableVerify) {
102 std::string Err;
103 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
102 std::string ErrorStr;
103 raw_string_ostream OS(ErrorStr);
104 if (verifyModule(*M.get(), &OS)) {
104105 errs() << argv[0]
105106 << ": assembly parsed, but does not verify as correct!\n";
106 errs() << Err;
107 errs() << OS.str();
107108 return 1;
108109 }
109110 }
4141 Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0);
4242 BI->setOperand(0, Zero32);
4343
44 EXPECT_TRUE(verifyFunction(*F, ReturnStatusAction));
44 EXPECT_TRUE(verifyFunction(*F));
4545 }
4646
4747 TEST(VerifierTest, AliasUnnamedAddr) {
5757 "bar", Aliasee, &M);
5858 GA->setUnnamedAddr(true);
5959 std::string Error;
60 EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error));
61 EXPECT_TRUE(StringRef(Error).startswith("Alias cannot have unnamed_addr"));
60 raw_string_ostream ErrorOS(Error);
61 EXPECT_TRUE(verifyModule(M, &ErrorOS));
62 EXPECT_TRUE(
63 StringRef(ErrorOS.str()).startswith("Alias cannot have unnamed_addr"));
6264 }
6365
6466 TEST(VerifierTest, InvalidRetAttribute) {
7173 Attribute::UWTable));
7274
7375 std::string Error;
74 EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error));
75 EXPECT_TRUE(StringRef(Error).
76 startswith("Attribute 'uwtable' only applies to functions!"));
76 raw_string_ostream ErrorOS(Error);
77 EXPECT_TRUE(verifyModule(M, &ErrorOS));
78 EXPECT_TRUE(StringRef(ErrorOS.str()).startswith(
79 "Attribute 'uwtable' only applies to functions!"));
7780 }
7881
7982 }