Skip to content

Commit ddd576a

Browse files
author
Walter Erquinigo
committed
[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
1 parent 33af4b2 commit ddd576a

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

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

+9-12
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,24 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Procfs.h"
10-
1110
#include "lldb/Host/linux/Support.h"
1211
#include "llvm/Support/MemoryBuffer.h"
12+
#include "llvm/Support/Threading.h"
1313

1414
using namespace lldb;
1515
using namespace lldb_private;
1616
using namespace process_linux;
1717
using namespace llvm;
1818

1919
Expected<ArrayRef<uint8_t>> lldb_private::process_linux::GetProcfsCpuInfo() {
20-
static Optional<std::vector<uint8_t>> cpu_info;
21-
if (!cpu_info) {
22-
auto buffer_or_error = errorOrToExpected(getProcFile("cpuinfo"));
23-
if (!buffer_or_error)
24-
return buffer_or_error.takeError();
25-
MemoryBuffer &buffer = **buffer_or_error;
26-
cpu_info = std::vector<uint8_t>(
27-
reinterpret_cast<const uint8_t *>(buffer.getBufferStart()),
28-
reinterpret_cast<const uint8_t *>(buffer.getBufferEnd()));
29-
}
30-
return *cpu_info;
20+
static ErrorOr<std::unique_ptr<MemoryBuffer>> cpu_info_or_err =
21+
getProcFile("cpuinfo");
22+
23+
if (!*cpu_info_or_err)
24+
cpu_info_or_err.getError();
25+
26+
MemoryBuffer &buffer = **cpu_info_or_err;
27+
return arrayRefFromStringRef(buffer.getBuffer());
3128
}
3229

3330
Expected<std::vector<cpu_id_t>>

0 commit comments

Comments
 (0)