1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
content / browser / devtools / devtools_pipe_handler.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/342213636): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif
#include "content/browser/devtools/devtools_pipe_handler.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include <io.h>
#include <stdlib.h>
#else
#include <sys/socket.h>
#endif
#include <stdio.h>
#include <cstdlib>
#include <memory>
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_pump_type.h"
#include "base/strings/string_util.h"
#include "base/synchronization/atomic_flag.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/common/content_switches.h"
#include "net/server/http_connection.h"
#include "third_party/inspector_protocol/crdtp/cbor.h"
const size_t kReceiveBufferSizeForDevTools = 100 * 1024 * 1024; // 100Mb
const size_t kWritePacketSize = 1 << 16;
// Our CBOR (RFC 7049) based format starts with a tag 24 indicating
// an envelope, that is, a byte string which as payload carries the
// entire remaining message. Thereby, the length of the byte string
// also tells us the message size on the wire.
// The details of the encoding are implemented in
// third_party/inspector_protocol/crdtp/cbor.h.
namespace content {
namespace {
class PipeIOBase {
public:
explicit PipeIOBase(const char* thread_name)
: thread_(new base::Thread(thread_name)) {}
virtual ~PipeIOBase() = default;
bool Start() {
base::Thread::Options options;
options.message_pump_type = base::MessagePumpType::IO;
if (!thread_->StartWithOptions(std::move(options)))
return false;
StartMainLoop();
return true;
}
static void Shutdown(std::unique_ptr<PipeIOBase> pipe_io) {
if (!pipe_io)
return;
auto thread = std::move(pipe_io->thread_);
pipe_io->shutting_down_.Set();
pipe_io->ClosePipe();
// Post self destruction on the custom thread if it's running.
if (thread->task_runner()) {
thread->task_runner()->DeleteSoon(FROM_HERE, std::move(pipe_io));
} else {
pipe_io.reset();
}
// Post background task that would join and destroy the thread.
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN,
base::WithBaseSyncPrimitives(), base::TaskPriority::BEST_EFFORT})
->DeleteSoon(FROM_HERE, std::move(thread));
}
protected:
virtual void StartMainLoop() {}
virtual void ClosePipe() = 0;
std::unique_ptr<base::Thread> thread_;
base::AtomicFlag shutting_down_;
};
#if BUILDFLAG(IS_WIN)
// Temporary CRT parameter validation error handler override that allows
// _get_osfhandle() to return INVALID_HANDLE_VALUE instead of crashing.
class ScopedInvalidParameterHandlerOverride {
public:
ScopedInvalidParameterHandlerOverride()
: prev_invalid_parameter_handler_(
_set_thread_local_invalid_parameter_handler(
InvalidParameterHandler)) {}
ScopedInvalidParameterHandlerOverride(
const ScopedInvalidParameterHandlerOverride&) = delete;
ScopedInvalidParameterHandlerOverride& operator=(
const ScopedInvalidParameterHandlerOverride&) = delete;
~ScopedInvalidParameterHandlerOverride() {
_set_thread_local_invalid_parameter_handler(
prev_invalid_parameter_handler_);
}
private:
// A do nothing invalid parameter handler that causes CRT routine to return
// error to the caller.
static void InvalidParameterHandler(const wchar_t* expression,
const wchar_t* function,
const wchar_t* file,
unsigned int line,
uintptr_t reserved) {}
const _invalid_parameter_handler prev_invalid_parameter_handler_;
};
#endif // BUILDFLAG(IS_WIN)
} // namespace
class PipeReaderBase : public PipeIOBase {
public:
PipeReaderBase(base::WeakPtr<DevToolsPipeHandler> devtools_handler,
int read_fd)
: PipeIOBase("DevToolsPipeHandlerReadThread"),
devtools_handler_(std::move(devtools_handler)),
read_fd_(read_fd) {
#if BUILDFLAG(IS_WIN)
ScopedInvalidParameterHandlerOverride invalid_parameter_handler_override;
read_handle_ = reinterpret_cast<HANDLE>(_get_osfhandle(read_fd));
#endif
}
protected:
void StartMainLoop() override {
thread_->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&PipeReaderBase::ReadLoop, base::Unretained(this)));
}
void ClosePipe() override {
// Concurrently discard the pipe handles to successfully join threads.
#if BUILDFLAG(IS_WIN)
// Cancel pending synchronous read.
CancelIoEx(read_handle_, nullptr);
ScopedInvalidParameterHandlerOverride invalid_parameter_handler_override;
_close(read_fd_);
read_handle_ = INVALID_HANDLE_VALUE;
#else
shutdown(read_fd_, SHUT_RDWR);
#endif
}
virtual void ReadLoopInternal() = 0;
size_t ReadBytes(void* buffer, size_t size, bool exact_size) {
size_t bytes_read = 0;
while (bytes_read < size) {
#if BUILDFLAG(IS_WIN)
DWORD size_read = 0;
bool had_error =
!ReadFile(read_handle_, static_cast<char*>(buffer) + bytes_read,
size - bytes_read, &size_read, nullptr);
#else
int size_read = read(read_fd_, static_cast<char*>(buffer) + bytes_read,
size - bytes_read);
if (size_read < 0 && errno == EINTR)
continue;
bool had_error = size_read <= 0;
#endif
if (had_error) {
if (!shutting_down_.IsSet()) {
LOG(ERROR) << "Connection terminated while reading from pipe";
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&DevToolsPipeHandler::OnDisconnect,
devtools_handler_));
}
return 0;
}
bytes_read += size_read;
if (!exact_size)
break;
}
return bytes_read;
}
void HandleMessage(std::vector<uint8_t> message) {
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&DevToolsPipeHandler::HandleMessage,
devtools_handler_, std::move(message)));
}
private:
void ReadLoop() {
ReadLoopInternal();
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&DevToolsPipeHandler::Shutdown, devtools_handler_));
}
base::WeakPtr<DevToolsPipeHandler> devtools_handler_;
int read_fd_;
#if BUILDFLAG(IS_WIN)
HANDLE read_handle_;
#endif
};
class PipeWriterBase : public PipeIOBase {
public:
explicit PipeWriterBase(int write_fd)
: PipeIOBase("DevToolsPipeHandlerWriteThread"), write_fd_(write_fd) {
#if BUILDFLAG(IS_WIN)
ScopedInvalidParameterHandlerOverride invalid_parameter_handler_override;
write_handle_ = reinterpret_cast<HANDLE>(_get_osfhandle(write_fd));
#endif
}
void Write(base::span<const uint8_t> message) {
base::TaskRunner* task_runner = thread_->task_runner().get();
task_runner->PostTask(
FROM_HERE,
base::BindOnce(&PipeWriterBase::WriteIntoPipe, base::Unretained(this),
std::string(message.begin(), message.end())));
}
protected:
void ClosePipe() override {
#if BUILDFLAG(IS_WIN)
ScopedInvalidParameterHandlerOverride invalid_parameter_handler_override;
_close(write_fd_);
write_handle_ = INVALID_HANDLE_VALUE;
#else
shutdown(write_fd_, SHUT_RDWR);
#endif
}
virtual void WriteIntoPipe(std::string message) = 0;
void WriteBytes(const char* bytes, size_t size) {
size_t total_written = 0;
while (total_written < size) {
size_t length = size - total_written;
if (length > kWritePacketSize)
length = kWritePacketSize;
#if BUILDFLAG(IS_WIN)
DWORD bytes_written = 0;
bool had_error =
!WriteFile(write_handle_, bytes + total_written,
static_cast<DWORD>(length), &bytes_written, nullptr);
#else
int bytes_written = write(write_fd_, bytes + total_written, length);
if (bytes_written < 0 && errno == EINTR)
continue;
bool had_error = bytes_written <= 0;
#endif
if (had_error) {
if (!shutting_down_.IsSet())
LOG(ERROR) << "Could not write into pipe";
return;
}
total_written += bytes_written;
}
}
private:
int write_fd_;
#if BUILDFLAG(IS_WIN)
HANDLE write_handle_;
#endif
};
namespace {
class PipeWriterASCIIZ : public PipeWriterBase {
public:
explicit PipeWriterASCIIZ(int write_fd) : PipeWriterBase(write_fd) {}
void WriteIntoPipe(std::string message) override {
WriteBytes(message.data(), message.size());
WriteBytes("\0", 1);
}
};
class PipeWriterCBOR : public PipeWriterBase {
public:
explicit PipeWriterCBOR(int write_fd) : PipeWriterBase(write_fd) {}
void WriteIntoPipe(std::string message) override {
DCHECK(crdtp::cbor::IsCBORMessage(crdtp::SpanFrom(message)));
WriteBytes(message.data(), message.size());
}
};
class PipeReaderASCIIZ : public PipeReaderBase {
public:
PipeReaderASCIIZ(base::WeakPtr<DevToolsPipeHandler> devtools_handler,
int read_fd)
: PipeReaderBase(std::move(devtools_handler), read_fd) {
read_buffer_ = new net::HttpConnection::ReadIOBuffer();
read_buffer_->set_max_buffer_size(kReceiveBufferSizeForDevTools);
}
private:
void ReadLoopInternal() override {
while (true) {
if (read_buffer_->RemainingCapacity() == 0 &&
!read_buffer_->IncreaseCapacity()) {
LOG(ERROR) << "Connection closed, not enough capacity";
break;
}
size_t bytes_read = ReadBytes(read_buffer_->data(),
read_buffer_->RemainingCapacity(), false);
if (!bytes_read)
break;
read_buffer_->DidRead(bytes_read);
// Go over the last read chunk, look for \0, extract messages.
int offset = 0;
for (int i = read_buffer_->GetSize() - bytes_read;
i < read_buffer_->GetSize(); ++i) {
if (read_buffer_->StartOfBuffer()[i] == '\0') {
HandleMessage(
std::vector<uint8_t>(read_buffer_->StartOfBuffer() + offset,
read_buffer_->StartOfBuffer() + i));
offset = i + 1;
}
}
if (offset)
read_buffer_->DidConsume(offset);
}
}
scoped_refptr<net::HttpConnection::ReadIOBuffer> read_buffer_;
};
class PipeReaderCBOR : public PipeReaderBase {
public:
PipeReaderCBOR(base::WeakPtr<DevToolsPipeHandler> devtools_handler,
int read_fd)
: PipeReaderBase(std::move(devtools_handler), read_fd) {}
private:
static uint32_t UInt32FromCBOR(const uint8_t* buf) {
return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
}
void ReadLoopInternal() override {
while (true) {
const size_t kPeekSize =
8; // tag tag_type? byte_string length*4 map_start
std::vector<uint8_t> buffer(kPeekSize);
if (!ReadBytes(&buffer.front(), kPeekSize, true))
break;
auto status_or_header = crdtp::cbor::EnvelopeHeader::ParseFromFragment(
crdtp::SpanFrom(buffer));
if (!status_or_header.ok()) {
LOG(ERROR) << "Error parsing CBOR envelope: "
<< status_or_header.status().ToASCIIString();
return;
}
const size_t msg_size = (*status_or_header).outer_size();
CHECK_GT(msg_size, kPeekSize);
buffer.resize(msg_size);
if (!ReadBytes(&buffer.front() + kPeekSize, msg_size - kPeekSize, true))
return;
HandleMessage(std::move(buffer));
}
}
};
} // namespace
// DevToolsPipeHandler ---------------------------------------------------
DevToolsPipeHandler::DevToolsPipeHandler(int read_fd,
int write_fd,
base::OnceClosure on_disconnect)
: on_disconnect_(std::move(on_disconnect)),
read_fd_(read_fd),
write_fd_(write_fd) {
browser_target_ = DevToolsAgentHost::CreateForBrowser(
nullptr, DevToolsAgentHost::CreateServerSocketCallback());
browser_target_->AttachClient(this);
std::string str_mode = base::ToLowerASCII(
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kRemoteDebuggingPipe));
mode_ = str_mode == "cbor" ? DevToolsPipeHandler::ProtocolMode::kCBOR
: DevToolsPipeHandler::ProtocolMode::kASCIIZ;
switch (mode_) {
case ProtocolMode::kASCIIZ:
pipe_reader_ = std::make_unique<PipeReaderASCIIZ>(
weak_factory_.GetWeakPtr(), read_fd_);
pipe_writer_ = std::make_unique<PipeWriterASCIIZ>(write_fd_);
break;
case ProtocolMode::kCBOR:
pipe_reader_ = std::make_unique<PipeReaderCBOR>(
weak_factory_.GetWeakPtr(), read_fd_);
pipe_writer_ = std::make_unique<PipeWriterCBOR>(write_fd_);
break;
}
if (!pipe_reader_->Start() || !pipe_writer_->Start())
Shutdown();
}
void DevToolsPipeHandler::Shutdown() {
if (shutting_down_)
return;
shutting_down_ = true;
// Disconnect from the target.
DCHECK(browser_target_);
browser_target_->DetachClient(this);
browser_target_ = nullptr;
PipeIOBase::Shutdown(std::move(pipe_reader_));
PipeIOBase::Shutdown(std::move(pipe_writer_));
}
DevToolsPipeHandler::~DevToolsPipeHandler() {
Shutdown();
}
void DevToolsPipeHandler::HandleMessage(std::vector<uint8_t> message) {
if (browser_target_)
browser_target_->DispatchProtocolMessage(this, message);
}
void DevToolsPipeHandler::OnDisconnect() {
if (on_disconnect_)
std::move(on_disconnect_).Run();
}
void DevToolsPipeHandler::DispatchProtocolMessage(
DevToolsAgentHost* agent_host,
base::span<const uint8_t> message) {
if (pipe_writer_)
pipe_writer_->Write(message);
}
void DevToolsPipeHandler::AgentHostClosed(DevToolsAgentHost* agent_host) {}
bool DevToolsPipeHandler::UsesBinaryProtocol() {
return mode_ == ProtocolMode::kCBOR;
}
bool DevToolsPipeHandler::AllowUnsafeOperations() {
return true;
}
std::string DevToolsPipeHandler::GetTypeForMetrics() {
return "RemoteDebugger";
}
} // namespace content