Skip to content

Commit 8b4306c

Browse files
committed
Debug Info: drop debug info via upgrading path if version number does not match.
Add a helper function getDebugInfoVersionFromModule to return the debug info version number for a module. "Verifier/module-flags-1.ll" checks for verification errors. It will seg fault when calling getDebugInfoVersionFromModule because of the incorrect format for module flags in the testing case. We make getModuleFlagsMetadata more robust by checking for error conditions. PR17982 llvm-svn: 196158
1 parent e601b50 commit 8b4306c

File tree

8 files changed

+64
-5
lines changed

8 files changed

+64
-5
lines changed

llvm/include/llvm/AutoUpgrade.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ namespace llvm {
5757
/// with different address spaces: the instruction is replaced by a pair
5858
/// ptrtoint+inttoptr.
5959
Value *UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy);
60+
61+
/// Check the debug info version number, if it is out-dated, drop the debug
62+
/// info. Return true if module is modified.
63+
bool UpgradeDebugInfo(Module &M);
6064
} // End llvm namespace
6165

6266
#endif

llvm/include/llvm/DebugInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,9 @@ DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
759759
/// Return true if module is modified.
760760
bool StripDebugInfo(Module &M);
761761

762+
/// Return Debug Info Version by checking module flags.
763+
unsigned getDebugInfoVersionFromModule(const Module &M);
764+
762765
/// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
763766
/// list debug info MDNodes used by an instruction, DebugInfoFinder uses
764767
/// processDeclare, processValue and processLocation to handle DbgDeclareInst,

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ bool LLParser::ValidateEndOfModule() {
182182
for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
183183
UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
184184

185+
UpgradeDebugInfo(*M);
186+
185187
return false;
186188
}
187189

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,6 +3152,7 @@ error_code BitcodeReader::MaterializeModule(Module *M) {
31523152
for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
31533153
UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
31543154

3155+
UpgradeDebugInfo(*M);
31553156
return error_code::success();
31563157
}
31573158

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "llvm/AutoUpgrade.h"
15+
#include "llvm/DebugInfo.h"
1516
#include "llvm/IR/Constants.h"
1617
#include "llvm/IR/Function.h"
1718
#include "llvm/IR/IRBuilder.h"
@@ -489,3 +490,12 @@ Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
489490

490491
return 0;
491492
}
493+
494+
/// Check the debug info version number, if it is out-dated, drop the debug
495+
/// info. Return true if module is modified.
496+
bool llvm::UpgradeDebugInfo(Module &M) {
497+
if (getDebugInfoVersionFromModule(M) == DEBUG_METADATA_VERSION)
498+
return false;
499+
500+
return StripDebugInfo(M);
501+
}

llvm/lib/IR/DebugInfo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,3 +1477,11 @@ bool llvm::StripDebugInfo(Module &M) {
14771477

14781478
return Changed;
14791479
}
1480+
1481+
/// Return Debug Info Version by checking module flags.
1482+
unsigned llvm::getDebugInfoVersionFromModule(const Module &M) {
1483+
Value *Val = M.getModuleFlag("Debug Info Version");
1484+
if (!Val)
1485+
return 0;
1486+
return cast<ConstantInt>(Val)->getZExtValue();
1487+
}

llvm/lib/IR/Module.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,16 @@ getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
318318

319319
for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) {
320320
MDNode *Flag = ModFlags->getOperand(i);
321-
ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0));
322-
MDString *Key = cast<MDString>(Flag->getOperand(1));
323-
Value *Val = Flag->getOperand(2);
324-
Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()),
325-
Key, Val));
321+
if (Flag->getNumOperands() >= 3 && isa<ConstantInt>(Flag->getOperand(0)) &&
322+
isa<MDString>(Flag->getOperand(1))) {
323+
// Check the operands of the MDNode before accessing the operands.
324+
// The verifier will actually catch these failures.
325+
ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0));
326+
MDString *Key = cast<MDString>(Flag->getOperand(1));
327+
Value *Val = Flag->getOperand(2);
328+
Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()),
329+
Key, Val));
330+
}
326331
}
327332
}
328333

llvm/test/Bitcode/drop-debug-info.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; RUN: llvm-as < %s | llvm-dis | FileCheck %s
2+
3+
define i32 @main() {
4+
entry:
5+
%retval = alloca i32, align 4
6+
store i32 0, i32* %retval
7+
ret i32 0, !dbg !12
8+
}
9+
10+
!llvm.dbg.cu = !{!0}
11+
!llvm.module.flags = !{!9}
12+
13+
!0 = metadata !{i32 786449, metadata !1, i32 12, metadata !"clang version 3.5 (trunk 195495) (llvm/trunk 195495:195504M)", i1 false, metadata !"", i32 0, metadata !2, metadata !2, metadata !3, metadata !2, metadata !2, metadata !""} ; [ DW_TAG_compile_unit ] [/Users/manmanren/llvm_gmail/release/../llvm/tools/clang/test/CodeGen/debug-info-version.c] [DW_LANG_C99]
14+
!1 = metadata !{metadata !"../llvm/tools/clang/test/CodeGen/debug-info-version.c", metadata !"/Users/manmanren/llvm_gmail/release"}
15+
!2 = metadata !{i32 0}
16+
!3 = metadata !{metadata !4}
17+
!4 = metadata !{i32 786478, metadata !1, metadata !5, metadata !"main", metadata !"main", metadata !"", i32 3, metadata !6, i1 false, i1 true, i32 0, i32 0, null, i32 256, i1 false, i32 ()* @main, null, null, metadata !2, i32 3} ; [ DW_TAG_subprogram ] [line 3] [def] [main]
18+
!5 = metadata !{i32 786473, metadata !1} ; [ DW_TAG_file_type ] [/Users/manmanren/llvm_gmail/release/../llvm/tools/clang/test/CodeGen/debug-info-version.c]
19+
!6 = metadata !{i32 786453, i32 0, null, metadata !"", i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !7, i32 0, null, null, null} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ]
20+
!7 = metadata !{metadata !8}
21+
!8 = metadata !{i32 786468, null, null, metadata !"int", i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ] [int] [line 0, size 32, align 32, offset 0, enc DW_ATE_signed]
22+
!9 = metadata !{i32 2, metadata !"Dwarf Version", i32 2}
23+
!12 = metadata !{i32 4, i32 0, metadata !4, null}
24+
25+
; CHECK-NOT: !dbg
26+
; CHECK-NOT: !llvm.dbg.cu

0 commit comments

Comments
 (0)