[Lldb-commits] [PATCH] D131081: [lldb] Prevent race condition when fetching /proc/cpuinfo

walter erquinigo via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed Aug 3 10:01:29 PDT 2022


wallace created this revision.
wallace added reviewers: clayborg, jj10306.
Herald added a project: All.
wallace requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

@clayborg found a potential race condition when setting a static
variable. The fix seems simply to use call_once.

All relevant tests pass.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131081

Files:
  lldb/source/Plugins/Process/Linux/Procfs.cpp


Index: lldb/source/Plugins/Process/Linux/Procfs.cpp
===================================================================
--- lldb/source/Plugins/Process/Linux/Procfs.cpp
+++ lldb/source/Plugins/Process/Linux/Procfs.cpp
@@ -9,7 +9,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;
@@ -18,16 +20,23 @@
 
 Expected<ArrayRef<uint8_t>> lldb_private::process_linux::GetProcfsCpuInfo() {
   static Optional<std::vector<uint8_t>> cpu_info;
-  if (!cpu_info) {
+  static Optional<std::string> error;
+  static llvm::once_flag g_once_flag;
+
+  llvm::call_once(g_once_flag, [] {
     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;
+    if (!buffer_or_error) {
+      error = toString(buffer_or_error.takeError());
+    } else {
+      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()));
+    }
+  });
+  if (cpu_info)
+    return *cpu_info;
+  return createStringError(inconvertibleErrorCode(), error->c_str());
 }
 
 Expected<std::vector<cpu_id_t>>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131081.449695.patch
Type: text/x-patch
Size: 1598 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220803/df39f3d0/attachment-0001.bin>


More information about the lldb-commits mailing list