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 "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(nullptr, channel_name
), this,
97 io_thread_
.message_loop_proxy());
100 IPC::ChannelProxy::Create(channel_name
, IPC::Channel::MODE_CLIENT
, this,
101 io_thread_
.message_loop_proxy());
105 bool ReplayProcess::OpenTestcase() {
106 base::FilePath path
=
107 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
108 switches::kIpcFuzzerTestcase
);
109 return MessageFile::Read(path
, &messages_
);
112 void ReplayProcess::SendNextMessage() {
113 if (message_index_
>= messages_
.size()) {
114 base::MessageLoop::current()->Quit();
118 // Take next message and release it from vector.
119 IPC::Message
* message
= messages_
[message_index_
];
120 messages_
[message_index_
++] = NULL
;
122 if (!channel_
->Send(message
)) {
123 LOG(ERROR
) << "ChannelProxy::Send() failed after "
124 << message_index_
<< " messages";
125 base::MessageLoop::current()->Quit();
129 void ReplayProcess::Run() {
130 timer_
.reset(new base::Timer(false, true));
131 timer_
->Start(FROM_HERE
,
132 base::TimeDelta::FromMilliseconds(1),
133 base::Bind(&ReplayProcess::SendNextMessage
,
134 base::Unretained(this)));
135 base::MessageLoop::current()->Run();
138 bool ReplayProcess::OnMessageReceived(const IPC::Message
& msg
) {
142 void ReplayProcess::OnChannelError() {
143 LOG(ERROR
) << "Channel error, quitting after "
144 << message_index_
<< " messages";
145 base::MessageLoop::current()->Quit();
148 } // namespace ipc_fuzzer