LLDB  mainline
PipeWindows.cpp
Go to the documentation of this file.
1 //===-- PipeWindows.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Process.h"
13 #include "llvm/Support/raw_ostream.h"
14 
15 #include <fcntl.h>
16 #include <io.h>
17 #include <rpc.h>
18 
19 #include <atomic>
20 #include <string>
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 static std::atomic<uint32_t> g_pipe_serial(0);
26 static constexpr llvm::StringLiteral g_pipe_name_prefix = "\\\\.\\Pipe\\";
27 
28 PipeWindows::PipeWindows()
29  : m_read(INVALID_HANDLE_VALUE), m_write(INVALID_HANDLE_VALUE),
30  m_read_fd(PipeWindows::kInvalidDescriptor),
31  m_write_fd(PipeWindows::kInvalidDescriptor) {
32  ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
33  ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
34 }
35 
37  : m_read((HANDLE)read), m_write((HANDLE)write),
38  m_read_fd(PipeWindows::kInvalidDescriptor),
39  m_write_fd(PipeWindows::kInvalidDescriptor) {
40  assert(read != LLDB_INVALID_PIPE || write != LLDB_INVALID_PIPE);
41 
42  // Don't risk in passing file descriptors and getting handles from them by
43  // _get_osfhandle since the retrieved handles are highly likely unrecognized
44  // in the current process and usually crashes the program. Pass handles
45  // instead since the handle can be inherited.
46 
47  if (read != LLDB_INVALID_PIPE) {
48  m_read_fd = _open_osfhandle((intptr_t)read, _O_RDONLY);
49  // Make sure the fd and native handle are consistent.
50  if (m_read_fd < 0)
51  m_read = INVALID_HANDLE_VALUE;
52  }
53 
54  if (write != LLDB_INVALID_PIPE) {
55  m_write_fd = _open_osfhandle((intptr_t)write, _O_WRONLY);
56  if (m_write_fd < 0)
57  m_write = INVALID_HANDLE_VALUE;
58  }
59 
60  ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
61  ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
62 }
63 
65 
66 Status PipeWindows::CreateNew(bool child_process_inherit) {
67  // Create an anonymous pipe with the specified inheritance.
68  SECURITY_ATTRIBUTES sa{sizeof(SECURITY_ATTRIBUTES), 0,
69  child_process_inherit ? TRUE : FALSE};
70  BOOL result = ::CreatePipe(&m_read, &m_write, &sa, 1024);
71  if (result == FALSE)
72  return Status(::GetLastError(), eErrorTypeWin32);
73 
74  m_read_fd = _open_osfhandle((intptr_t)m_read, _O_RDONLY);
75  ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
76  m_read_overlapped.hEvent = ::CreateEventA(nullptr, TRUE, FALSE, nullptr);
77 
78  m_write_fd = _open_osfhandle((intptr_t)m_write, _O_WRONLY);
79  ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
80 
81  return Status();
82 }
83 
84 Status PipeWindows::CreateNewNamed(bool child_process_inherit) {
85  // Even for anonymous pipes, we open a named pipe. This is because you
86  // cannot get overlapped i/o on Windows without using a named pipe. So we
87  // synthesize a unique name.
88  uint32_t serial = g_pipe_serial.fetch_add(1);
89  std::string pipe_name;
90  llvm::raw_string_ostream pipe_name_stream(pipe_name);
91  pipe_name_stream << "lldb.pipe." << ::GetCurrentProcessId() << "." << serial;
92  pipe_name_stream.flush();
93 
94  return CreateNew(pipe_name.c_str(), child_process_inherit);
95 }
96 
97 Status PipeWindows::CreateNew(llvm::StringRef name,
98  bool child_process_inherit) {
99  if (name.empty())
100  return Status(ERROR_INVALID_PARAMETER, eErrorTypeWin32);
101 
102  if (CanRead() || CanWrite())
103  return Status(ERROR_ALREADY_EXISTS, eErrorTypeWin32);
104 
105  std::string pipe_path = g_pipe_name_prefix.str();
106  pipe_path.append(name.str());
107 
108  // Always open for overlapped i/o. We implement blocking manually in Read
109  // and Write.
110  DWORD read_mode = FILE_FLAG_OVERLAPPED;
111  m_read = ::CreateNamedPipeA(
112  pipe_path.c_str(), PIPE_ACCESS_INBOUND | read_mode,
113  PIPE_TYPE_BYTE | PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL);
114  if (INVALID_HANDLE_VALUE == m_read)
115  return Status(::GetLastError(), eErrorTypeWin32);
116  m_read_fd = _open_osfhandle((intptr_t)m_read, _O_RDONLY);
117  ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
118  m_read_overlapped.hEvent = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
119 
120  // Open the write end of the pipe. Note that closing either the read or
121  // write end of the pipe could directly close the pipe itself.
122  Status result = OpenNamedPipe(name, child_process_inherit, false);
123  if (!result.Success()) {
125  return result;
126  }
127 
128  return result;
129 }
130 
132  bool child_process_inherit,
134  llvm::SmallString<128> pipe_name;
135  Status error;
136  ::UUID unique_id;
137  RPC_CSTR unique_string;
138  RPC_STATUS status = ::UuidCreate(&unique_id);
139  if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
140  status = ::UuidToStringA(&unique_id, &unique_string);
141  if (status == RPC_S_OK) {
142  pipe_name = prefix;
143  pipe_name += "-";
144  pipe_name += reinterpret_cast<char *>(unique_string);
145  ::RpcStringFreeA(&unique_string);
146  error = CreateNew(pipe_name, child_process_inherit);
147  } else {
148  error.SetError(status, eErrorTypeWin32);
149  }
150  if (error.Success())
151  name = pipe_name;
152  return error;
153 }
154 
155 Status PipeWindows::OpenAsReader(llvm::StringRef name,
156  bool child_process_inherit) {
157  if (CanRead())
158  return Status(ERROR_ALREADY_EXISTS, eErrorTypeWin32);
159 
160  return OpenNamedPipe(name, child_process_inherit, true);
161 }
162 
163 Status
165  bool child_process_inherit,
166  const std::chrono::microseconds &timeout) {
167  if (CanWrite())
168  return Status(ERROR_ALREADY_EXISTS, eErrorTypeWin32);
169 
170  return OpenNamedPipe(name, child_process_inherit, false);
171 }
172 
173 Status PipeWindows::OpenNamedPipe(llvm::StringRef name,
174  bool child_process_inherit, bool is_read) {
175  if (name.empty())
176  return Status(ERROR_INVALID_PARAMETER, eErrorTypeWin32);
177 
178  assert(is_read ? !CanRead() : !CanWrite());
179 
180  SECURITY_ATTRIBUTES attributes = {};
181  attributes.bInheritHandle = child_process_inherit;
182 
183  std::string pipe_path = g_pipe_name_prefix.str();
184  pipe_path.append(name.str());
185 
186  if (is_read) {
187  m_read = ::CreateFileA(pipe_path.c_str(), GENERIC_READ, 0, &attributes,
188  OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
189  if (INVALID_HANDLE_VALUE == m_read)
190  return Status(::GetLastError(), eErrorTypeWin32);
191 
192  m_read_fd = _open_osfhandle((intptr_t)m_read, _O_RDONLY);
193 
194  ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
195  m_read_overlapped.hEvent = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
196  } else {
197  m_write = ::CreateFileA(pipe_path.c_str(), GENERIC_WRITE, 0, &attributes,
198  OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
199  if (INVALID_HANDLE_VALUE == m_write)
200  return Status(::GetLastError(), eErrorTypeWin32);
201 
202  m_write_fd = _open_osfhandle((intptr_t)m_write, _O_WRONLY);
203 
204  ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
205  }
206 
207  return Status();
208 }
209 
211 
213 
215  if (!CanRead())
217  int result = m_read_fd;
219  if (m_read_overlapped.hEvent)
220  ::CloseHandle(m_read_overlapped.hEvent);
221  m_read = INVALID_HANDLE_VALUE;
222  ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
223  return result;
224 }
225 
227  if (!CanWrite())
229  int result = m_write_fd;
231  m_write = INVALID_HANDLE_VALUE;
232  ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
233  return result;
234 }
235 
237  if (!CanRead())
238  return;
239 
240  if (m_read_overlapped.hEvent)
241  ::CloseHandle(m_read_overlapped.hEvent);
242 
243  _close(m_read_fd);
244  m_read = INVALID_HANDLE_VALUE;
246  ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
247 }
248 
250  if (!CanWrite())
251  return;
252 
253  _close(m_write_fd);
254  m_write = INVALID_HANDLE_VALUE;
256  ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
257 }
258 
262 }
263 
264 Status PipeWindows::Delete(llvm::StringRef name) { return Status(); }
265 
266 bool PipeWindows::CanRead() const { return (m_read != INVALID_HANDLE_VALUE); }
267 
268 bool PipeWindows::CanWrite() const { return (m_write != INVALID_HANDLE_VALUE); }
269 
270 HANDLE
272 
273 HANDLE
275 
276 Status PipeWindows::ReadWithTimeout(void *buf, size_t size,
277  const std::chrono::microseconds &duration,
278  size_t &bytes_read) {
279  if (!CanRead())
280  return Status(ERROR_INVALID_HANDLE, eErrorTypeWin32);
281 
282  bytes_read = 0;
283  DWORD sys_bytes_read = size;
284  BOOL result = ::ReadFile(m_read, buf, sys_bytes_read, &sys_bytes_read,
286  if (!result && GetLastError() != ERROR_IO_PENDING)
287  return Status(::GetLastError(), eErrorTypeWin32);
288 
289  DWORD timeout = (duration == std::chrono::microseconds::zero())
290  ? INFINITE
291  : duration.count() * 1000;
292  DWORD wait_result = ::WaitForSingleObject(m_read_overlapped.hEvent, timeout);
293  if (wait_result != WAIT_OBJECT_0) {
294  // The operation probably failed. However, if it timed out, we need to
295  // cancel the I/O. Between the time we returned from WaitForSingleObject
296  // and the time we call CancelIoEx, the operation may complete. If that
297  // hapens, CancelIoEx will fail and return ERROR_NOT_FOUND. If that
298  // happens, the original operation should be considered to have been
299  // successful.
300  bool failed = true;
301  DWORD failure_error = ::GetLastError();
302  if (wait_result == WAIT_TIMEOUT) {
303  BOOL cancel_result = CancelIoEx(m_read, &m_read_overlapped);
304  if (!cancel_result && GetLastError() == ERROR_NOT_FOUND)
305  failed = false;
306  }
307  if (failed)
308  return Status(failure_error, eErrorTypeWin32);
309  }
310 
311  // Now we call GetOverlappedResult setting bWait to false, since we've
312  // already waited as long as we're willing to.
313  if (!GetOverlappedResult(m_read, &m_read_overlapped, &sys_bytes_read, FALSE))
314  return Status(::GetLastError(), eErrorTypeWin32);
315 
316  bytes_read = sys_bytes_read;
317  return Status();
318 }
319 
320 Status PipeWindows::Write(const void *buf, size_t num_bytes,
321  size_t &bytes_written) {
322  if (!CanWrite())
323  return Status(ERROR_INVALID_HANDLE, eErrorTypeWin32);
324 
325  DWORD sys_bytes_written = 0;
326  BOOL write_result = ::WriteFile(m_write, buf, num_bytes, &sys_bytes_written,
328  if (!write_result && GetLastError() != ERROR_IO_PENDING)
329  return Status(::GetLastError(), eErrorTypeWin32);
330 
331  BOOL result = GetOverlappedResult(m_write, &m_write_overlapped,
332  &sys_bytes_written, TRUE);
333  if (!result)
334  return Status(::GetLastError(), eErrorTypeWin32);
335  return Status();
336 }
lldb_private::UUID
Definition: UUID.h:23
lldb_private::PipeWindows
Definition: PipeWindows.h:22
lldb_private::PipeWindows::~PipeWindows
~PipeWindows() override
Definition: PipeWindows.cpp:64
lldb_private::PipeWindows::m_read_overlapped
OVERLAPPED m_read_overlapped
Definition: PipeWindows.h:83
lldb_private::PipeWindows::Write
Status Write(const void *buf, size_t size, size_t &bytes_written) override
Definition: PipeWindows.cpp:320
lldb_private::PipeWindows::ReadWithTimeout
Status ReadWithTimeout(void *buf, size_t size, const std::chrono::microseconds &timeout, size_t &bytes_read) override
Definition: PipeWindows.cpp:276
LLDB_INVALID_PIPE
#define LLDB_INVALID_PIPE
Definition: lldb-types.h:80
lldb_private::PipeWindows::GetWriteFileDescriptor
int GetWriteFileDescriptor() const override
Definition: PipeWindows.cpp:212
lldb_private::PipeWindows::m_read_fd
int m_read_fd
Definition: PipeWindows.h:80
lldb_private::PipeWindows::ReleaseWriteFileDescriptor
int ReleaseWriteFileDescriptor() override
Definition: PipeWindows.cpp:226
lldb::pipe_t
int pipe_t
Definition: lldb-types.h:64
PipeWindows.h
lldb_private::PipeWindows::CanWrite
bool CanWrite() const override
Definition: PipeWindows.cpp:268
lldb_private::PipeWindows::kInvalidDescriptor
static const int kInvalidDescriptor
Definition: PipeWindows.h:24
lldb::eErrorTypeWin32
@ eErrorTypeWin32
Standard Win32 error codes.
Definition: lldb-enumerations.h:312
lldb_private::PipeWindows::OpenAsWriterWithTimeout
Status OpenAsWriterWithTimeout(llvm::StringRef name, bool child_process_inherit, const std::chrono::microseconds &timeout) override
Definition: PipeWindows.cpp:164
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::PipeWindows::CreateWithUniqueName
Status CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit, llvm::SmallVectorImpl< char > &name) override
Definition: PipeWindows.cpp:131
lldb_private::Status::Success
bool Success() const
Test for success condition.
Definition: Status.cpp:288
g_pipe_serial
static std::atomic< uint32_t > g_pipe_serial(0)
lldb_private::PipeWindows::CloseWriteFileDescriptor
void CloseWriteFileDescriptor() override
Definition: PipeWindows.cpp:249
lldb_private::PipeWindows::OpenNamedPipe
Status OpenNamedPipe(llvm::StringRef name, bool child_process_inherit, bool is_read)
Definition: PipeWindows.cpp:173
lldb_private::PipeWindows::m_write
HANDLE m_write
Definition: PipeWindows.h:78
lldb_private::PipeWindows::CloseReadFileDescriptor
void CloseReadFileDescriptor() override
Definition: PipeWindows.cpp:236
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::PipeWindows::Close
void Close() override
Definition: PipeWindows.cpp:259
lldb_private::PipeWindows::GetWriteNativeHandle
HANDLE GetWriteNativeHandle()
Definition: PipeWindows.cpp:274
lldb_private::PipeWindows::CreateNew
Status CreateNew(bool child_process_inherit) override
Definition: PipeWindows.cpp:66
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::PipeWindows::PipeWindows
PipeWindows()
Definition: PipeWindows.cpp:28
lldb_private::PipeWindows::OpenAsReader
Status OpenAsReader(llvm::StringRef name, bool child_process_inherit) override
Definition: PipeWindows.cpp:155
lldb_private::PipeWindows::GetReadFileDescriptor
int GetReadFileDescriptor() const override
Definition: PipeWindows.cpp:210
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::PipeWindows::Delete
Status Delete(llvm::StringRef name) override
Definition: PipeWindows.cpp:264
lldb_private::PipeWindows::m_read
HANDLE m_read
Definition: PipeWindows.h:77
g_pipe_name_prefix
static constexpr llvm::StringLiteral g_pipe_name_prefix
Definition: PipeWindows.cpp:26
lldb_private::PipeWindows::CreateNewNamed
Status CreateNewNamed(bool child_process_inherit)
Definition: PipeWindows.cpp:84
llvm::SmallVectorImpl
Definition: Disassembler.h:42
lldb_private::PipeWindows::CanRead
bool CanRead() const override
Definition: PipeWindows.cpp:266
lldb_private::PipeWindows::ReleaseReadFileDescriptor
int ReleaseReadFileDescriptor() override
Definition: PipeWindows.cpp:214
lldb_private::PipeWindows::m_write_fd
int m_write_fd
Definition: PipeWindows.h:81
lldb_private::PipeWindows::GetReadNativeHandle
HANDLE GetReadNativeHandle()
Definition: PipeWindows.cpp:271
lldb
Definition: SBAddress.h:15
lldb_private::PipeWindows::m_write_overlapped
OVERLAPPED m_write_overlapped
Definition: PipeWindows.h:84