[llvm] [llvm-exegesis] Close file descriptors after use (PR #86584)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 27 12:31:38 PDT 2024


https://github.com/boomanaiden154 updated https://github.com/llvm/llvm-project/pull/86584

>From cb5917b2dc762c1f7cb691d0d5de1136d697f4de Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Mon, 25 Mar 2024 14:28:14 -0700
Subject: [PATCH 1/2] [llvm-exegesis] Close file descriptors after use

There are several insstances in the subprocess executor in llvm-exegesis
where we fail to close file descriptors after using them. This leaves
them open, which can cause issues later on if a third-party binary is
using the exegesis libraries and executing many blocks before exiting.
Leaving the descriptors open until process exit is also bad practice.
This patch fixes that by explicitly calling close() when we are done
with a specific file descriptor.

This patch fixes #86583.
---
 llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp  | 1 +
 llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp | 3 +++
 2 files changed, 4 insertions(+)

diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
index 498308e2edbe1f..7b01b8c69cdfb2 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
@@ -317,6 +317,7 @@ class SubProcessFunctionExecutorImpl
     int CounterFileDescriptor = Counter->getFileDescriptor();
     Error SendError =
         sendFileDescriptorThroughSocket(WriteFD, CounterFileDescriptor);
+    close(WriteFD);
 
     if (SendError)
       return SendError;
diff --git a/llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp b/llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp
index 1fd81bd407becb..06d589821071ad 100644
--- a/llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp
+++ b/llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp
@@ -50,6 +50,7 @@ Error SubprocessMemory::initializeSubprocessMemory(pid_t ProcessID) {
                                Twine(strerror(errno)));
   }
   SharedMemoryNames.push_back(AuxiliaryMemoryName);
+  close(AuxiliaryMemoryFD);
   return Error::success();
 }
 
@@ -87,6 +88,8 @@ Error SubprocessMemory::addMemoryDefinition(
           "Unmapping a memory definition in the parent failed: " +
           Twine(strerror(errno)));
     }
+
+    close(SharedMemoryFD);
   }
   return Error::success();
 }

>From 69ab786f51d45c6013fc7ba965c1ea867d21cf85 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Wed, 27 Mar 2024 12:29:25 -0700
Subject: [PATCH 2/2] Use make_scope_exit

---
 llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp  | 3 ++-
 llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp | 8 +++++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
index 7b01b8c69cdfb2..ed53f8fabb1751 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
@@ -18,6 +18,7 @@
 #include "PerfHelper.h"
 #include "SubprocessMemory.h"
 #include "Target.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
@@ -283,6 +284,7 @@ class SubProcessFunctionExecutorImpl
                    SmallVectorImpl<int64_t> &CounterValues,
                    ArrayRef<const char *> ValidationCounters,
                    SmallVectorImpl<int64_t> &ValidationCounterValues) const {
+    auto WriteFDClose = make_scope_exit([WriteFD]() { close(WriteFD); });
     const ExegesisTarget &ET = State.getExegesisTarget();
     auto CounterOrError =
         ET.createCounter(CounterName, State, ValidationCounters, ChildPID);
@@ -317,7 +319,6 @@ class SubProcessFunctionExecutorImpl
     int CounterFileDescriptor = Counter->getFileDescriptor();
     Error SendError =
         sendFileDescriptorThroughSocket(WriteFD, CounterFileDescriptor);
-    close(WriteFD);
 
     if (SendError)
       return SendError;
diff --git a/llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp b/llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp
index 06d589821071ad..d3eb30ffd497fa 100644
--- a/llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp
+++ b/llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp
@@ -8,6 +8,7 @@
 
 #include "SubprocessMemory.h"
 #include "Error.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FormatVariadic.h"
 #include <cerrno>
@@ -45,12 +46,13 @@ Error SubprocessMemory::initializeSubprocessMemory(pid_t ProcessID) {
     return make_error<Failure>(
         "Failed to create shared memory object for auxiliary memory: " +
         Twine(strerror(errno)));
+  auto AuxiliaryMemoryFDClose =
+      make_scope_exit([AuxiliaryMemoryFD]() { close(AuxiliaryMemoryFD); });
   if (ftruncate(AuxiliaryMemoryFD, AuxiliaryMemorySize) != 0) {
     return make_error<Failure>("Truncating the auxiliary memory failed: " +
                                Twine(strerror(errno)));
   }
   SharedMemoryNames.push_back(AuxiliaryMemoryName);
-  close(AuxiliaryMemoryFD);
   return Error::success();
 }
 
@@ -64,6 +66,8 @@ Error SubprocessMemory::addMemoryDefinition(
     SharedMemoryNames.push_back(SharedMemoryName);
     int SharedMemoryFD =
         shm_open(SharedMemoryName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
+    auto SharedMemoryFDClose =
+        make_scope_exit([SharedMemoryFD]() { close(SharedMemoryFD); });
     if (ftruncate(SharedMemoryFD, MemVal.SizeBytes) != 0) {
       return make_error<Failure>("Truncating a memory definiton failed: " +
                                  Twine(strerror(errno)));
@@ -88,8 +92,6 @@ Error SubprocessMemory::addMemoryDefinition(
           "Unmapping a memory definition in the parent failed: " +
           Twine(strerror(errno)));
     }
-
-    close(SharedMemoryFD);
   }
   return Error::success();
 }



More information about the llvm-commits mailing list