1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/ipc_fuzzer/replay/replay_process.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/posix/global_descriptors.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "ipc/ipc_descriptors.h"
16 #include "ipc/ipc_switches.h"
18 namespace ipc_fuzzer
{
20 ReplayProcess::ReplayProcess()
21 : io_thread_("Chrome_ChildIOThread"),
22 shutdown_event_(true, false),
26 ReplayProcess::~ReplayProcess() {
29 // Signal this event before shutting down the service process. That way all
30 // background threads can cleanup.
31 shutdown_event_
.Signal();
35 bool ReplayProcess::Initialize(int argc
, const char** argv
) {
36 base::CommandLine::Init(argc
, argv
);
38 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
39 switches::kIpcFuzzerTestcase
)) {
40 LOG(ERROR
) << "This binary shouldn't be executed directly, "
41 << "please use tools/ipc_fuzzer/play_testcase.py";
45 // Log to both stderr and file destinations.
46 logging::SetMinLogLevel(logging::LOG_ERROR
);
47 logging::LoggingSettings settings
;
48 settings
.logging_dest
= logging::LOG_TO_ALL
;
49 settings
.log_file
= FILE_PATH_LITERAL("ipc_replay.log");
50 logging::InitLogging(settings
);
52 io_thread_
.StartWithOptions(
53 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0));
56 base::GlobalDescriptors
* g_fds
= base::GlobalDescriptors::GetInstance();
57 g_fds
->Set(kPrimaryIPCChannel
,
58 kPrimaryIPCChannel
+ base::GlobalDescriptors::kBaseDescriptor
);
64 void ReplayProcess::OpenChannel() {
65 std::string channel_name
=
66 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
67 switches::kProcessChannelID
);
69 channel_
= IPC::ChannelProxy::Create(channel_name
,
70 IPC::Channel::MODE_CLIENT
,
72 io_thread_
.message_loop_proxy());
75 bool ReplayProcess::OpenTestcase() {
77 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
78 switches::kIpcFuzzerTestcase
);
79 return MessageFile::Read(path
, &messages_
);
82 void ReplayProcess::SendNextMessage() {
83 if (message_index_
>= messages_
.size()) {
84 base::MessageLoop::current()->Quit();
88 // Take next message and release it from vector.
89 IPC::Message
* message
= messages_
[message_index_
];
90 messages_
[message_index_
++] = NULL
;
92 if (!channel_
->Send(message
)) {
93 LOG(ERROR
) << "ChannelProxy::Send() failed after "
94 << message_index_
<< " messages";
95 base::MessageLoop::current()->Quit();
99 void ReplayProcess::Run() {
100 timer_
.reset(new base::Timer(false, true));
101 timer_
->Start(FROM_HERE
,
102 base::TimeDelta::FromMilliseconds(1),
103 base::Bind(&ReplayProcess::SendNextMessage
,
104 base::Unretained(this)));
105 base::MessageLoop::current()->Run();
108 bool ReplayProcess::OnMessageReceived(const IPC::Message
& msg
) {
112 void ReplayProcess::OnChannelError() {
113 LOG(ERROR
) << "Channel error, quitting after "
114 << message_index_
<< " messages";
115 base::MessageLoop::current()->Quit();
118 } // namespace ipc_fuzzer