[Lldb-commits] [lldb] ddd576a - [lldb] Prevent race condition when fetching /proc/cpuinfo
Walter Erquinigo via lldb-commits
lldb-commits at lists.llvm.org
Mon Aug 8 12:31:49 PDT 2022
Author: Walter Erquinigo
Date: 2022-08-08T12:31:42-07:00
New Revision: ddd576ab87a16eb7fb06531da8040ac2ece0c9a3
URL: https://github.com/llvm/llvm-project/commit/ddd576ab87a16eb7fb06531da8040ac2ece0c9a3
DIFF: https://github.com/llvm/llvm-project/commit/ddd576ab87a16eb7fb06531da8040ac2ece0c9a3.diff
LOG: [lldb] Prevent race condition when fetching /proc/cpuinfo
@clayborg found a potential race condition when setting a static
variable. The fix seems simply to use call_once.
All relevant tests pass.
Differential Revision: https://reviews.llvm.org/D131081
Added:
Modified:
lldb/source/Plugins/Process/Linux/Procfs.cpp
Removed:
################################################################################
diff --git a/lldb/source/Plugins/Process/Linux/Procfs.cpp b/lldb/source/Plugins/Process/Linux/Procfs.cpp
index 7c0c55a4f208..f59883d1caff 100644
--- a/lldb/source/Plugins/Process/Linux/Procfs.cpp
+++ b/lldb/source/Plugins/Process/Linux/Procfs.cpp
@@ -7,9 +7,9 @@
//===----------------------------------------------------------------------===//
#include "Procfs.h"
-
#include "lldb/Host/linux/Support.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Threading.h"
using namespace lldb;
using namespace lldb_private;
@@ -17,17 +17,14 @@ using namespace process_linux;
using namespace llvm;
Expected<ArrayRef<uint8_t>> lldb_private::process_linux::GetProcfsCpuInfo() {
- static Optional<std::vector<uint8_t>> cpu_info;
- if (!cpu_info) {
- auto buffer_or_error = errorOrToExpected(getProcFile("cpuinfo"));
- if (!buffer_or_error)
- return buffer_or_error.takeError();
- MemoryBuffer &buffer = **buffer_or_error;
- cpu_info = std::vector<uint8_t>(
- reinterpret_cast<const uint8_t *>(buffer.getBufferStart()),
- reinterpret_cast<const uint8_t *>(buffer.getBufferEnd()));
- }
- return *cpu_info;
+ static ErrorOr<std::unique_ptr<MemoryBuffer>> cpu_info_or_err =
+ getProcFile("cpuinfo");
+
+ if (!*cpu_info_or_err)
+ cpu_info_or_err.getError();
+
+ MemoryBuffer &buffer = **cpu_info_or_err;
+ return arrayRefFromStringRef(buffer.getBuffer());
}
Expected<std::vector<cpu_id_t>>
More information about the lldb-commits
mailing list