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/message_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 "content/public/common/content_switches.h"
16 #include "content/public/common/mojo_channel_switches.h"
17 #include "ipc/ipc_descriptors.h"
18 #include "ipc/ipc_switches.h"
19 #include "ipc/mojo/ipc_channel_mojo.h"
20 #include "third_party/mojo/src/mojo/edk/embedder/configuration.h"
21 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h"
22 #include "third_party/mojo/src/mojo/edk/embedder/simple_platform_support.h"
24 namespace ipc_fuzzer
{
26 // TODO(morrita): content::InitializeMojo() should be used once it becomes
27 // a public API. See src/content/app/mojo/mojo_init.cc
28 void InitializeMojo() {
29 mojo::embedder::GetConfiguration()->max_message_num_bytes
=
31 mojo::embedder::Init(scoped_ptr
<mojo::embedder::PlatformSupport
>(
32 new mojo::embedder::SimplePlatformSupport()));
35 ReplayProcess::ReplayProcess()
36 : io_thread_("Chrome_ChildIOThread"),
37 shutdown_event_(true, false),
41 ReplayProcess::~ReplayProcess() {
44 // Signal this event before shutting down the service process. That way all
45 // background threads can cleanup.
46 shutdown_event_
.Signal();
50 bool ReplayProcess::Initialize(int argc
, const char** argv
) {
51 base::CommandLine::Init(argc
, argv
);
53 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
54 switches::kIpcFuzzerTestcase
)) {
55 LOG(ERROR
) << "This binary shouldn't be executed directly, "
56 << "please use tools/ipc_fuzzer/play_testcase.py";
60 // Log to both stderr and file destinations.
61 logging::SetMinLogLevel(logging::LOG_ERROR
);
62 logging::LoggingSettings settings
;
63 settings
.logging_dest
= logging::LOG_TO_ALL
;
64 settings
.log_file
= FILE_PATH_LITERAL("ipc_replay.log");
65 logging::InitLogging(settings
);
67 // Make sure to initialize Mojo before starting the IO thread.
70 io_thread_
.StartWithOptions(
71 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0));
74 base::GlobalDescriptors
* g_fds
= base::GlobalDescriptors::GetInstance();
75 g_fds
->Set(kPrimaryIPCChannel
,
76 kPrimaryIPCChannel
+ base::GlobalDescriptors::kBaseDescriptor
);
82 void ReplayProcess::OpenChannel() {
83 std::string channel_name
=
84 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
85 switches::kProcessChannelID
);
87 // TODO(morrita): As the adoption of ChannelMojo spreads, this
88 // criteria has to be updated.
89 std::string process_type
=
90 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
91 switches::kProcessType
);
92 bool should_use_mojo
= process_type
== switches::kRendererProcess
&&
93 content::ShouldUseMojoChannel();
94 if (should_use_mojo
) {
95 channel_
= IPC::ChannelProxy::Create(
96 IPC::ChannelMojo::CreateClientFactory(
97 io_thread_
.task_runner(), channel_name
), this,
98 io_thread_
.task_runner());
101 IPC::ChannelProxy::Create(channel_name
, IPC::Channel::MODE_CLIENT
, this,
102 io_thread_
.task_runner());
106 bool ReplayProcess::OpenTestcase() {
107 base::FilePath path
=
108 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
109 switches::kIpcFuzzerTestcase
);
110 return MessageFile::Read(path
, &messages_
);
113 void ReplayProcess::SendNextMessage() {
114 if (message_index_
>= messages_
.size()) {
115 base::MessageLoop::current()->Quit();
119 // Take next message and release it from vector.
120 IPC::Message
* message
= messages_
[message_index_
];
121 messages_
[message_index_
++] = NULL
;
123 if (!channel_
->Send(message
)) {
124 LOG(ERROR
) << "ChannelProxy::Send() failed after "
125 << message_index_
<< " messages";
126 base::MessageLoop::current()->Quit();
130 void ReplayProcess::Run() {
131 timer_
.reset(new base::Timer(false, true));
132 timer_
->Start(FROM_HERE
,
133 base::TimeDelta::FromMilliseconds(1),
134 base::Bind(&ReplayProcess::SendNextMessage
,
135 base::Unretained(this)));
136 base::MessageLoop::current()->Run();
139 bool ReplayProcess::OnMessageReceived(const IPC::Message
& msg
) {
143 void ReplayProcess::OnChannelError() {
144 LOG(ERROR
) << "Channel error, quitting after "
145 << message_index_
<< " messages";
146 base::MessageLoop::current()->Quit();
149 } // namespace ipc_fuzzer