From 171ba71128eec9f1859bb995c597dcff296ee730 Mon Sep 17 00:00:00 2001 From: Thrrreeee Date: Mon, 13 Jul 2026 10:59:06 +0800 Subject: [PATCH] [BOLT] fix DIE traversal incorrect loop termination condition. (#208450) The DIE traversal loop in partitionCUs() used an incorrect termination condition, causing it to read past the end of the CU. Fix the loop to stop at NextCUOffset so traversal no longer runs beyond the unit's boundary. Fixed [#208440](https://github.com/llvm/llvm-project/issues/208440). --- bolt/lib/Rewrite/DWARFRewriter.cpp | 14 +++++--------- bolt/test/X86/dwarf4-cross-cu-ranges.test | 4 +++- bolt/test/X86/dwarf5-debug-names-cross-cu.s | 4 +++- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp index 31bb74a6488a..6c322be095c9 100644 --- a/bolt/lib/Rewrite/DWARFRewriter.cpp +++ b/bolt/lib/Rewrite/DWARFRewriter.cpp @@ -590,11 +590,11 @@ static SmallVector> partitionCUs(DWARFContext &DwCtx, const uint64_t NextCUOffset = CU->getNextUnitOffset(); DWARFDataExtractor DebugInfoData = CU->getDebugInfoExtractor(); DWARFDebugInfoEntry DIEEntry; - SmallVector ParentIndex; - ParentIndex.push_back(UINT32_MAX); - do { + // extractFast() here only attributes are inspected here, so ParentIdx is + // passed as a dummy value. + while (DIEOffset < NextCUOffset) { if (!DIEEntry.extractFast(*CU, &DIEOffset, DebugInfoData, NextCUOffset, - ParentIndex.back())) + /*ParentIdx=*/0)) break; const DWARFAbbreviationDeclaration *Abbrev = DIEEntry.getAbbreviationDeclarationPtr(); @@ -617,12 +617,8 @@ static SmallVector> partitionCUs(DWARFContext &DwCtx, EC.unionSets(CU, TargetCU); } } - if (Abbrev->hasChildren()) - ParentIndex.push_back(0); - } else { - ParentIndex.pop_back(); } - } while (!ParentIndex.empty()); + } } DenseMap> MembersByLeader; diff --git a/bolt/test/X86/dwarf4-cross-cu-ranges.test b/bolt/test/X86/dwarf4-cross-cu-ranges.test index 2e784311c2af..860af97901fb 100644 --- a/bolt/test/X86/dwarf4-cross-cu-ranges.test +++ b/bolt/test/X86/dwarf4-cross-cu-ranges.test @@ -2,13 +2,15 @@ ; RUN: llvm-mc -dwarf-version=4 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf4-cross-cu-with-loclist.s -o %t.o ; RUN: %clang %cflags -nostdlib -no-pie %t.o -o %t.exe -; RUN: llvm-bolt %t.exe -o %t.exe.bolt --update-debug-sections --always-convert-to-ranges --debug-thread-count=4 +; RUN: llvm-bolt %t.exe -o %t.exe.bolt --update-debug-sections --always-convert-to-ranges --debug-thread-count=4 2>&1 | FileCheck %s --check-prefix=BOLT ; RUN: llvm-objdump --disassemble %t.exe.bolt > %t.txt ; RUN: llvm-dwarfdump --show-form --verbose --debug-info --debug-ranges %t.exe.bolt >> %t.txt ; RUN: FileCheck %s --input-file=%t.txt ;; Test that two cross-referenced CUs processed in the same bucket keep their own .debug_ranges entries. +; BOLT-NOT: warning: DWARF unit from offset {{.*}} incl. to offset {{.*}} excl. tries to read DIEs at offset {{.*}} + ; CHECK:
: ; CHECK-NEXT: [[#%.6x,MAIN:]] ; CHECK: <_Z4foo2i>: diff --git a/bolt/test/X86/dwarf5-debug-names-cross-cu.s b/bolt/test/X86/dwarf5-debug-names-cross-cu.s index 73c50d6d41db..3f4188240d69 100644 --- a/bolt/test/X86/dwarf5-debug-names-cross-cu.s +++ b/bolt/test/X86/dwarf5-debug-names-cross-cu.s @@ -3,12 +3,14 @@ # RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %s -o %tmain.o # RUN: %clang %cflags -dwarf-5 %tmain.o -o %t.exe -Wl,-q -# RUN: llvm-bolt %t.exe -o %t.bolt --update-debug-sections +# RUN: llvm-bolt %t.exe -o %t.bolt --update-debug-sections 2>&1 | FileCheck %s --check-prefix=BOLT # RUN: llvm-dwarfdump --debug-info -r 0 --debug-names %t.bolt > %t.txt # RUN: cat %t.txt | FileCheck --check-prefix=CHECK %s ## This test checks that BOLT generates Entries for DW_AT_abstract_origin when it has cross cu reference. +# BOLT-NOT: warning: DWARF unit from offset {{.*}} incl. to offset {{.*}} excl. tries to read DIEs at offset {{.*}} + # CHECK: [[OFFSET1:0x[0-9a-f]*]]: Compile Unit # CHECK: [[OFFSET2:0x[0-9a-f]*]]: Compile Unit # CHECK: Name Index @ 0x0 {