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

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 25 14:32:48 PDT 2024


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

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.

>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] [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();
 }



More information about the llvm-commits mailing list