Skip to content

Commit 780fb5f

Browse files
[IR] Fix range-based for loop over MDOperands bug
With e851278 the for loop that iterates over MDNode operands was changed to a range-based for loop. This change surfaces a bug where if the result of MD->operands() is an ArrayRef that has a size of 0, then iterating over that ArrayRef leads to a segmentation fault, due to accessing invalid addresses. This patch fixes that issue.
1 parent bdc5a87 commit 780fb5f

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

llvm/lib/IR/Verifier.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,9 +2912,11 @@ void Verifier::visitFunction(const Function &F) {
29122912
for (auto &I : BB) {
29132913
VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
29142914
// The llvm.loop annotations also contain two DILocations.
2915-
if (auto MD = I.getMetadata(LLVMContext::MD_loop))
2916-
for (const MDOperand &MDO : llvm::drop_begin(MD->operands()))
2917-
VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MDO));
2915+
if (auto MD = I.getMetadata(LLVMContext::MD_loop)) {
2916+
if (MD->getNumOperands())
2917+
for (const MDOperand &MDO : llvm::drop_begin(MD->operands()))
2918+
VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MDO));
2919+
}
29182920
if (BrokenDebugInfo)
29192921
return;
29202922
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; RUN: llc --filetype=obj %s -o -
2+
%"class.llvm::StringRef" = type { ptr, i64 }
3+
define internal void @_ZL30tokenizeWindowsCommandLineImplN4llvm9StringRefERNS_11StringSaverENS_12function_refIFvS0_EEEbNS3_IFvvEEEb() #0 !dbg !12 {
4+
%7 = alloca %"class.llvm::StringRef", align 8
5+
%21 = call noundef i64 @_ZNK4llvm9StringRef4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %7), !dbg !264
6+
br label %22, !dbg !265
7+
br label %22, !llvm.loop !284
8+
}
9+
define linkonce_odr noundef i64 @_ZNK4llvm9StringRef4sizeEv() #0 align 2 !dbg !340 {
10+
%2 = alloca ptr, align 8
11+
%3 = load ptr, ptr %2, align 8
12+
%4 = getelementptr inbounds %"class.llvm::StringRef", ptr %3, !dbg !344
13+
%5 = load i64, ptr %4, !dbg !344
14+
ret i64 %5, !dbg !345
15+
}
16+
!llvm.module.flags = !{!2, !6}
17+
!llvm.dbg.cu = !{!7}
18+
!2 = !{i32 2, !"Debug Info Version", i32 3}
19+
!6 = !{i32 7, !"frame-pointer", i32 1}
20+
!7 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !8, sdk: "MacOSX14.0.sdk")
21+
!8 = !DIFile(filename: "file.cpp", directory: "/Users/Dev", checksumkind: CSK_MD5, checksum: "ed7ae158f20f7914bc5fb843291e80da")
22+
!12 = distinct !DISubprogram(unit: !7, retainedNodes: !36)
23+
!36 = !{}
24+
!260 = distinct !DILexicalBlock(scope: !12, line: 412, column: 3)
25+
!264 = !DILocation(scope: !260)
26+
!265 = !DILocation(scope: !260, column: 20)
27+
!284 = distinct !{}
28+
!340 = distinct !DISubprogram(unit: !7, retainedNodes: !36)
29+
!344 = !DILocation(scope: !340)
30+
!345 = !DILocation(scope: !340)

0 commit comments

Comments
 (0)