Skip to content

Commit 74a5e77

Browse files
[llvm-exegesis] Close file descriptors after use (llvm#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 llvm#86583.
1 parent 23b6709 commit 74a5e77

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "PerfHelper.h"
1919
#include "SubprocessMemory.h"
2020
#include "Target.h"
21+
#include "llvm/ADT/ScopeExit.h"
2122
#include "llvm/ADT/StringExtras.h"
2223
#include "llvm/ADT/StringRef.h"
2324
#include "llvm/ADT/Twine.h"
@@ -283,6 +284,7 @@ class SubProcessFunctionExecutorImpl
283284
SmallVectorImpl<int64_t> &CounterValues,
284285
ArrayRef<const char *> ValidationCounters,
285286
SmallVectorImpl<int64_t> &ValidationCounterValues) const {
287+
auto WriteFDClose = make_scope_exit([WriteFD]() { close(WriteFD); });
286288
const ExegesisTarget &ET = State.getExegesisTarget();
287289
auto CounterOrError =
288290
ET.createCounter(CounterName, State, ValidationCounters, ChildPID);

llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "SubprocessMemory.h"
1010
#include "Error.h"
11+
#include "llvm/ADT/ScopeExit.h"
1112
#include "llvm/Support/Error.h"
1213
#include "llvm/Support/FormatVariadic.h"
1314
#include <cerrno>
@@ -56,6 +57,8 @@ Error SubprocessMemory::initializeSubprocessMemory(pid_t ProcessID) {
5657
return make_error<Failure>(
5758
"Failed to create shared memory object for auxiliary memory: " +
5859
Twine(strerror(errno)));
60+
auto AuxiliaryMemoryFDClose =
61+
make_scope_exit([AuxiliaryMemoryFD]() { close(AuxiliaryMemoryFD); });
5962
if (ftruncate(AuxiliaryMemoryFD, AuxiliaryMemorySize) != 0) {
6063
return make_error<Failure>("Truncating the auxiliary memory failed: " +
6164
Twine(strerror(errno)));
@@ -78,6 +81,8 @@ Error SubprocessMemory::addMemoryDefinition(
7881
return make_error<Failure>(
7982
"Failed to create shared memory object for memory definition: " +
8083
Twine(strerror(errno)));
84+
auto SharedMemoryFDClose =
85+
make_scope_exit([SharedMemoryFD]() { close(SharedMemoryFD); });
8186
if (ftruncate(SharedMemoryFD, MemVal.SizeBytes) != 0) {
8287
return make_error<Failure>("Truncating a memory definiton failed: " +
8388
Twine(strerror(errno)));

0 commit comments

Comments
 (0)