[llvm] 686cc00 - [llvm-profgen] Fix an out-of-range error during unwinding

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 22 18:34:06 PDT 2021


Author: wlei
Date: 2021-09-22T18:33:27-07:00
New Revision: 686cc0006793b06fd15d08cc0188b071f49ae990

URL: https://github.com/llvm/llvm-project/commit/686cc0006793b06fd15d08cc0188b071f49ae990
DIFF: https://github.com/llvm/llvm-project/commit/686cc0006793b06fd15d08cc0188b071f49ae990.diff

LOG: [llvm-profgen] Fix an out-of-range error during unwinding

It happened that the LBR entry target can be the first address of text section which causes an out-of-range crash. So here add a boundary check.

Reviewed By: hoy, wenlei

Differential Revision: https://reviews.llvm.org/D110271

Added: 
    

Modified: 
    llvm/tools/llvm-profgen/PerfReader.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp
index f892d6bf4a2a..75d93f6f260b 100644
--- a/llvm/tools/llvm-profgen/PerfReader.cpp
+++ b/llvm/tools/llvm-profgen/PerfReader.cpp
@@ -60,20 +60,33 @@ void VirtualUnwinder::unwindLinear(UnwindState &State, uint64_t Repeat) {
     // converted to a list of pseudo probes to report in ProfileGenerator.
     State.getParentFrame()->recordRangeCount(Target, End, Repeat);
   } else {
-    // Unwind linear execution part
-    uint64_t LeafAddr = State.CurrentLeafFrame->Address;
-    while (IP.Address >= Target) {
+    // Unwind linear execution part.
+    // Split and record the range by 
diff erent inline context. For example:
+    // [0x01] ... main:1          # Target
+    // [0x02] ... main:2
+    // [0x03] ... main:3 @ foo:1
+    // [0x04] ... main:3 @ foo:2
+    // [0x05] ... main:3 @ foo:3
+    // [0x06] ... main:4
+    // [0x07] ... main:5          # End
+    // It will be recorded:
+    // [main:*]         : [0x06, 0x07], [0x01, 0x02]
+    // [main:3 @ foo:*] : [0x03, 0x05]
+    while (IP.Address > Target) {
       uint64_t PrevIP = IP.Address;
       IP.backward();
       // Break into segments for implicit call/return due to inlining
       bool SameInlinee = Binary->inlineContextEqual(PrevIP, IP.Address);
-      if (!SameInlinee || PrevIP == Target) {
-        State.switchToFrame(LeafAddr);
+      if (!SameInlinee) {
+        State.switchToFrame(PrevIP);
         State.CurrentLeafFrame->recordRangeCount(PrevIP, End, Repeat);
         End = IP.Address;
       }
-      LeafAddr = IP.Address;
     }
+    assert(IP.Address == Target && "The last one must be the target address.");
+    // Record the remaining range, [0x01, 0x02] in the example
+    State.switchToFrame(IP.Address);
+    State.CurrentLeafFrame->recordRangeCount(IP.Address, End, Repeat);
   }
 }
 


        


More information about the llvm-commits mailing list