Skip to content

Commit 94a903e

Browse files
committed
Port the strip-invalid-debuginfo logic to the legacy verifier pass, too.
Since r268966 the modern Verifier pass defaults to stripping invalid debug info in nonasserts builds. This patch ports this behavior back to the legacy Verifier pass as well. The primary motivation is that the clang frontend accepts bitcode files as input but is still using the legacy pass pipeline. Background: The problem I'm trying to solve with this sequence of patches is that historically we've done a really bad job at verifying debug info. We want to be able to make the verifier stricter without having to worry about breaking bitcode compatibility with existing producers. For example, we don't necessarily want IR produced by an older version of clang to be rejected by an LTO link just because of malformed debug info, and rather provide an option to strip it. Note that merely outdated (but well-formed) debug info would continue to be auto-upgraded in this scenario. http://reviews.llvm.org/D20629 <rdar://problem/26448800> llvm-svn: 270768
1 parent d6469e3 commit 94a903e

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

llvm/lib/IR/Verifier.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4460,17 +4460,16 @@ struct VerifierLegacyPass : public FunctionPass {
44604460
static char ID;
44614461

44624462
Verifier V;
4463-
bool FatalErrors;
4463+
bool FatalErrors = true;
44644464

44654465
VerifierLegacyPass()
44664466
: FunctionPass(ID),
4467-
V(&dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/true),
4468-
FatalErrors(true) {
4467+
V(&dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false) {
44694468
initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
44704469
}
44714470
explicit VerifierLegacyPass(bool FatalErrors)
44724471
: FunctionPass(ID),
4473-
V(&dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/true),
4472+
V(&dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false),
44744473
FatalErrors(FatalErrors) {
44754474
initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
44764475
}
@@ -4483,9 +4482,20 @@ struct VerifierLegacyPass : public FunctionPass {
44834482
}
44844483

44854484
bool doFinalization(Module &M) override {
4486-
if (!V.verify(M) && FatalErrors)
4487-
report_fatal_error("Broken module found, compilation aborted!");
4485+
bool HasErrors = !V.verify(M);
4486+
if (FatalErrors) {
4487+
if (HasErrors)
4488+
report_fatal_error("Broken module found, compilation aborted!");
4489+
assert(!V.hasBrokenDebugInfo() && "Module contains invalid debug info");
4490+
}
44884491

4492+
// Strip broken debug info.
4493+
if (V.hasBrokenDebugInfo()) {
4494+
DiagnosticInfoIgnoringInvalidDebugMetadata DiagInvalid(M);
4495+
M.getContext().diagnose(DiagInvalid);
4496+
if (!StripDebugInfo(M))
4497+
report_fatal_error("Failed to strip malformed debug info");
4498+
}
44894499
return false;
44904500
}
44914501

llvm/unittests/IR/VerifierTest.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
#include "llvm/IR/Verifier.h"
1110
#include "llvm/IR/Constants.h"
1211
#include "llvm/IR/DIBuilder.h"
1312
#include "llvm/IR/DerivedTypes.h"
@@ -16,7 +15,9 @@
1615
#include "llvm/IR/GlobalVariable.h"
1716
#include "llvm/IR/Instructions.h"
1817
#include "llvm/IR/LLVMContext.h"
18+
#include "llvm/IR/LegacyPassManager.h"
1919
#include "llvm/IR/Module.h"
20+
#include "llvm/IR/Verifier.h"
2021
#include "gtest/gtest.h"
2122

2223
namespace llvm {
@@ -198,5 +199,26 @@ TEST(VerifierTest, StripInvalidDebugInfo) {
198199
}
199200
#endif
200201

202+
TEST(VerifierTest, StripInvalidDebugInfoLegacy) {
203+
LLVMContext C;
204+
Module M("M", C);
205+
DIBuilder DIB(M);
206+
DIB.createCompileUnit(dwarf::DW_LANG_C89, "broken.c", "/",
207+
"unittest", false, "", 0);
208+
DIB.finalize();
209+
EXPECT_FALSE(verifyModule(M));
210+
211+
// Now break it.
212+
auto *File = DIB.createFile("not-a-CU.f", ".");
213+
NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
214+
NMD->addOperand(File);
215+
EXPECT_TRUE(verifyModule(M));
216+
217+
legacy::PassManager Passes;
218+
Passes.add(createVerifierPass(false));
219+
Passes.run(M);
220+
EXPECT_FALSE(verifyModule(M));
221+
}
222+
201223
} // end anonymous namespace
202224
} // end namespace llvm

0 commit comments

Comments
 (0)