[llvm] 8bbdc73 - [llvm-profgen] Harden perf script invocation (#212253)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 5 05:44:22 PDT 2026


Author: Sergey Shcherbinin
Date: 2026-08-05T16:44:17+04:00
New Revision: 8bbdc73b846a3360cd02f619a3d500dbeb64ffae

URL: https://github.com/llvm/llvm-project/commit/8bbdc73b846a3360cd02f619a3d500dbeb64ffae
DIFF: https://github.com/llvm/llvm-project/commit/8bbdc73b846a3360cd02f619a3d500dbeb64ffae.diff

LOG: [llvm-profgen] Harden perf script invocation (#212253)

Harden ``llvm-profgen``'s ``perf script`` invocation for ``--perfdata``:
report launch/exit failures, and clear redirect files between the two
invocations so stale stdout/stderr cannot leak. Cover both with lit
tests that inject a mock ``perf`` via ``PATH``.

Assisted by GPT-5

Added: 
    llvm/test/tools/llvm-profgen/X86/perf-invocation-error.test
    llvm/test/tools/llvm-profgen/X86/perfdata-redirect.test

Modified: 
    llvm/test/tools/llvm-profgen/lit.local.cfg
    llvm/tools/llvm-profgen/PerfReader.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-profgen/X86/perf-invocation-error.test b/llvm/test/tools/llvm-profgen/X86/perf-invocation-error.test
new file mode 100644
index 0000000000000..8fa36b5a5b24b
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/X86/perf-invocation-error.test
@@ -0,0 +1,62 @@
+# REQUIRES: system-linux
+
+# RUN: split-file %s %t
+# RUN: yaml2obj %t/binary.yaml -o %t/perf-invocation-error.exe
+# RUN: touch %t/perf.data
+# RUN: chmod +x %t/exit/perf
+
+## Verify that a nonzero perf exit status and its stderr are reported.
+# RUN: not env PATH="%t/exit%{pathsep}%{PATH}" \
+# RUN:   llvm-profgen --binary=%t/perf-invocation-error.exe \
+# RUN:   --perfdata=%t/perf.data --output=/dev/null \
+# RUN:   2>&1 | FileCheck %s --check-prefix=EXIT
+## Use a PATH that contains only the non-executable mock, so discovery cannot
+## fall through to a system perf if lookup ever requires executability.
+# RUN: not env PATH="%t/exec" \
+# RUN:   llvm-profgen --binary=%t/perf-invocation-error.exe \
+# RUN:   --perfdata=%t/perf.data \
+# RUN:   --output=/dev/null 2>&1 | FileCheck %s --check-prefix=EXEC
+
+# EXIT: error: Perf script failed with exit code 1
+# EXIT-NEXT: mock perf failed
+# EXEC: error: Failed to execute perf script
+
+#--- exit/perf
+#!/bin/sh
+echo "mock perf failed" >&2
+exit 1
+
+#--- exec/perf
+This file is deliberately not executable.
+
+#--- binary.yaml
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_X86_64
+Sections:
+  - Name:         .text
+    Type:         SHT_PROGBITS
+    Flags:        [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:      0x1000
+    Offset:       0x1000
+    AddressAlign: 0x1
+    ## ret
+    Content:      C3
+ProgramHeaders:
+  - Type:     PT_LOAD
+    Flags:    [ PF_X, PF_R ]
+    Offset:   0
+    VAddr:    0
+    Align:    0x1000
+    FirstSec: .text
+    LastSec:  .text
+Symbols:
+  - Name:    foo
+    Type:    STT_FUNC
+    Section: .text
+    Binding: STB_GLOBAL
+    Value:   0x1000
+    Size:    0x1

diff  --git a/llvm/test/tools/llvm-profgen/X86/perfdata-redirect.test b/llvm/test/tools/llvm-profgen/X86/perfdata-redirect.test
new file mode 100644
index 0000000000000..0a51303a94866
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/X86/perfdata-redirect.test
@@ -0,0 +1,127 @@
+# REQUIRES: system-linux
+
+# RUN: split-file %s %t
+# RUN: yaml2obj %t/binary.yaml -o %t/perfdata-redirect.exe
+# RUN: touch %t/perf.data
+# RUN: chmod +x %t/success/perf %t/failure/perf
+# RUN: env PATH="%t/success%{pathsep}%{PATH}" \
+# RUN:   llvm-profgen --binary=%t/perfdata-redirect.exe \
+# RUN:   --perfdata=%t/perf.data \
+# RUN:   --skip-symbolization --use-offset=0 --format=text --output=%t/profile
+# RUN: FileCheck %s --check-prefix=PROFILE --input-file=%t/profile
+# RUN: not env PATH="%t/failure%{pathsep}%{PATH}" \
+# RUN:   llvm-profgen --binary=%t/perfdata-redirect.exe \
+# RUN:   --perfdata=%t/perf.data --output=/dev/null 2>&1 \
+# RUN:   | FileCheck %s --check-prefix=SECOND-ERROR
+
+## The first mock invocation deliberately writes the complete second output plus a
+## duplicate sample. Without truncating the shared redirect file, that stale
+## sample survives the second invocation and doubles every profile count.
+# PROFILE:      1
+# PROFILE-NEXT: 1008-100c:1
+# PROFILE-NEXT: 2
+# PROFILE-NEXT: 1000->1008:1
+# PROFILE-NEXT: 100c->1000:1
+
+## The first invocation also leaves a longer stderr message. Verify that an
+## error from the second invocation contains only its own stderr.
+# SECOND-ERROR: error: Perf script failed with exit code 7
+# SECOND-ERROR-NEXT: second invocation failed
+# SECOND-ERROR-NOT: STALE-FIRST-STDERR
+
+#--- success/perf
+#!/bin/sh
+
+if [ "$1" != "script" ] || [ "$2" != "--show-mmap-events" ] || \
+   [ "$3" != "-F" ] || [ "$5" != "-i" ] || [ ! -f "$6" ]; then
+  echo "unexpected perf arguments: $*" >&2
+  exit 2
+fi
+
+emit_sample() {
+  printf '%s\n' 'PERF_RECORD_MMAP2 1/1: [0x700000000000(0x2000) @ 0 00:00 0 0]: r-xp /tmp/perfdata-redirect.exe'
+  printf '%s\n' '70000000100c 0x70000000100c/0x700000001000/P/-/-/0 0x700000001000/0x700000001008/P/-/-/0'
+}
+
+case "$4" in
+  comm,pid)
+    if [ "$#" -ne 6 ]; then
+      echo "unexpected mmap arguments: $*" >&2
+      exit 2
+    fi
+    emit_sample
+    printf '%s\n' '70000000100c 0x70000000100c/0x700000001000/P/-/-/0 0x700000001000/0x700000001008/P/-/-/0'
+    ;;
+  ip,brstack)
+    if [ "$#" -ne 8 ] || [ "$7" != "--pid" ] || [ "$8" != "1" ]; then
+      echo "unexpected sample arguments: $*" >&2
+      exit 2
+    fi
+    emit_sample
+    ;;
+  *)
+    echo "unexpected perf fields: $4" >&2
+    exit 2
+    ;;
+esac
+
+#--- failure/perf
+#!/bin/sh
+
+if [ "$1" != "script" ] || [ "$2" != "--show-mmap-events" ] || \
+   [ "$3" != "-F" ] || [ "$5" != "-i" ] || [ ! -f "$6" ]; then
+  echo "unexpected perf arguments: $*" >&2
+  exit 2
+fi
+
+case "$4" in
+  comm,pid)
+    printf '%s\n' 'PERF_RECORD_MMAP2 1/1: [0x700000000000(0x2000) @ 0 00:00 0 0]: r-xp /tmp/perfdata-redirect.exe'
+    printf '%s\n' 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx STALE-FIRST-STDERR' >&2
+    ;;
+  ip,brstack)
+    printf '%s\n' 'second invocation failed' >&2
+    exit 7
+    ;;
+  *)
+    echo "unexpected perf fields: $4" >&2
+    exit 2
+    ;;
+esac
+
+#--- binary.yaml
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_X86_64
+  Entry:   0x1000
+Sections:
+  - Name:         .text
+    Type:         SHT_PROGBITS
+    Flags:        [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:      0x1000
+    Offset:       0x1000
+    AddressAlign: 0x1
+    ## 1000: jmp 0x1008
+    ## 1002: nop*6
+    ## 1008: nop*4
+    ## 100c: jmp 0x1000
+    ## 100e: ret
+    Content:      EB0690909090909090909090EBF2C3
+ProgramHeaders:
+  - Type:     PT_LOAD
+    Flags:    [ PF_X, PF_R ]
+    Offset:   0
+    VAddr:    0
+    FileSize: 0x100F
+    MemSize:  0x100F
+    Align:    0x1000
+Symbols:
+  - Name:    foo
+    Type:    STT_FUNC
+    Section: .text
+    Binding: STB_GLOBAL
+    Value:   0x1000
+    Size:    0xF

diff  --git a/llvm/test/tools/llvm-profgen/lit.local.cfg b/llvm/test/tools/llvm-profgen/lit.local.cfg
index 8373a11e3f394..860e1e9557a7f 100644
--- a/llvm/test/tools/llvm-profgen/lit.local.cfg
+++ b/llvm/test/tools/llvm-profgen/lit.local.cfg
@@ -1 +1,4 @@
 config.suffixes = [".test", ".ll", ".s", ".yaml"]
+
+# Allow tests to prepend a mock directory onto the real PATH.
+config.substitutions.append(("%{PATH}", config.environment["PATH"]))

diff  --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp
index 5fb1c6d252915..c4fc4fb25166f 100644
--- a/llvm/tools/llvm-profgen/PerfReader.cpp
+++ b/llvm/tools/llvm-profgen/PerfReader.cpp
@@ -18,6 +18,7 @@
 #include "llvm/Support/Process.h"
 #include "llvm/Support/Timer.h"
 #include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/raw_ostream.h"
 #include "llvm/TargetParser/Triple.h"
 
 #define DEBUG_TYPE "perf-reader"
@@ -466,7 +467,7 @@ PerfScriptReader::convertPerfDataToTrace(ProfiledBinary *Binary, bool SkipPID,
   if (!PerfExecutable) {
     exitWithError("Perf not found.");
   }
-  std::string PerfPath = *PerfExecutable;
+  std::string PerfExecutablePath = *PerfExecutable;
   SmallString<128> PerfTraceFile;
   sys::fs::createUniquePath("perf-script-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%.tmp",
                             PerfTraceFile, /*MakeAbsolute=*/true);
@@ -477,12 +478,53 @@ PerfScriptReader::convertPerfDataToTrace(ProfiledBinary *Binary, bool SkipPID,
   PerfScriptReader::TempFileCleanups.emplace_back(PerfTraceFile);
   PerfScriptReader::TempFileCleanups.emplace_back(ErrorFile);
 
+  auto RunPerfScript = [&](ArrayRef<StringRef> Args) {
+    // ExecuteAndWait does not truncate redirected output files on Unix. Remove
+    // both files so a shorter invocation cannot retain output from the
+    // previous perf script invocation.
+    for (StringRef Path : {StringRef(PerfTraceFile), StringRef(ErrorFile)}) {
+      if (std::error_code EC = sys::fs::remove(Path))
+        exitWithError(EC, Path);
+    }
+
+    std::string ExecutionError;
+    bool ExecutionFailed = false;
+    int ExitCode =
+        sys::ExecuteAndWait(PerfExecutablePath, Args, std::nullopt, Redirects,
+                            /*SecondsToWait=*/0, /*MemoryLimit=*/0,
+                            &ExecutionError, &ExecutionFailed);
+    if (!ExecutionFailed && ExitCode == 0)
+      return;
+
+    std::string Message;
+    raw_string_ostream OS(Message);
+    if (ExecutionFailed || ExitCode == -1)
+      OS << "Failed to execute perf script";
+    else if (ExitCode == -2)
+      OS << "Perf script terminated abnormally";
+    else
+      OS << "Perf script failed with exit code " << ExitCode;
+    if (!ExecutionError.empty())
+      OS << ": " << ExecutionError;
+
+    if (auto ErrorBuffer = MemoryBuffer::getFile(ErrorFile)) {
+      StringRef Stderr = ErrorBuffer.get()->getBuffer().trim();
+      if (!Stderr.empty())
+        OS << "\n" << Stderr;
+    }
+    exitWithError(OS.str());
+  };
+
   std::string PIDs;
   if (!SkipPID) {
-    StringRef ScriptMMapArgs[] = {PerfPath, "script",   "--show-mmap-events",
-                                  "-F",     "comm,pid", "-i",
+    StringRef ScriptMMapArgs[] = {PerfExecutablePath,
+                                  "script",
+                                  "--show-mmap-events",
+                                  "-F",
+                                  "comm,pid",
+                                  "-i",
                                   PerfData};
-    sys::ExecuteAndWait(PerfPath, ScriptMMapArgs, std::nullopt, Redirects);
+    RunPerfScript(ScriptMMapArgs);
 
     // Collect the PIDs
     TraceStream TraceIt(PerfTraceFile);
@@ -509,7 +551,7 @@ PerfScriptReader::convertPerfDataToTrace(ProfiledBinary *Binary, bool SkipPID,
 
   // Run perf script again to retrieve events for PIDs collected above
   SmallVector<StringRef, 8> ScriptSampleArgs;
-  ScriptSampleArgs.push_back(PerfPath);
+  ScriptSampleArgs.push_back(PerfExecutablePath);
   ScriptSampleArgs.push_back("script");
   ScriptSampleArgs.push_back("--show-mmap-events");
   ScriptSampleArgs.push_back("-F");
@@ -520,7 +562,7 @@ PerfScriptReader::convertPerfDataToTrace(ProfiledBinary *Binary, bool SkipPID,
     ScriptSampleArgs.push_back("--pid");
     ScriptSampleArgs.push_back(PIDs);
   }
-  sys::ExecuteAndWait(PerfPath, ScriptSampleArgs, std::nullopt, Redirects);
+  RunPerfScript(ScriptSampleArgs);
 
   return {std::string(PerfTraceFile), InputFormat::PerfScript,
           PerfContent::UnknownContent};


        


More information about the llvm-commits mailing list