| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "tools/ipc_fuzzer/replay/replay_process.h" | |
| 6 | |
| 7 #include <limits.h> | |
| 8 #include <string> | |
| 9 #include "base/bind.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" | |
| 23 | |
| 24 namespace ipc_fuzzer { | |
| 25 | |
| 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 = | |
| 30 64 * 1024 * 1024; | |
| 31 mojo::embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>( | |
| 32 new mojo::embedder::SimplePlatformSupport())); | |
| 33 } | |
| 34 | |
| 35 ReplayProcess::ReplayProcess() | |
| 36 : io_thread_("Chrome_ChildIOThread"), | |
| 37 shutdown_event_(true, false), | |
| 38 message_index_(0) { | |
| 39 } | |
| 40 | |
| 41 ReplayProcess::~ReplayProcess() { | |
| 42 channel_.reset(); | |
| 43 | |
| 44 // Signal this event before shutting down the service process. That way all | |
| 45 // background threads can cleanup. | |
| 46 shutdown_event_.Signal(); | |
| 47 io_thread_.Stop(); | |
| 48 } | |
| 49 | |
| 50 bool ReplayProcess::Initialize(int argc, const char** argv) { | |
| 51 base::CommandLine::Init(argc, argv); | |
| 52 | |
| 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"; | |
| 57 return false; | |
| 58 } | |
| 59 | |
| 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); | |
| 66 | |
| 67 // Make sure to initialize Mojo before starting the IO thread. | |
| 68 InitializeMojo(); | |
| 69 | |
| 70 io_thread_.StartWithOptions( | |
| 71 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | |
| 72 | |
| 73 #if defined(OS_POSIX) | |
| 74 base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); | |
| 75 g_fds->Set(kPrimaryIPCChannel, | |
| 76 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); | |
| 77 #endif | |
| 78 | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 void ReplayProcess::OpenChannel() { | |
| 83 std::string channel_name = | |
| 84 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 85 switches::kProcessChannelID); | |
| 86 | |
| 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()); | |
| 98 } else { | |
| 99 channel_ = | |
| 100 IPC::ChannelProxy::Create(channel_name, IPC::Channel::MODE_CLIENT, this, | |
| 101 io_thread_.message_loop_proxy()); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 bool ReplayProcess::OpenTestcase() { | |
| 106 base::FilePath path = | |
| 107 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( | |
| 108 switches::kIpcFuzzerTestcase); | |
| 109 return MessageFile::Read(path, &messages_); | |
| 110 } | |
| 111 | |
| 112 void ReplayProcess::SendNextMessage() { | |
| 113 if (message_index_ >= messages_.size()) { | |
| 114 base::MessageLoop::current()->Quit(); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 // Take next message and release it from vector. | |
| 119 IPC::Message* message = messages_[message_index_]; | |
| 120 messages_[message_index_++] = NULL; | |
| 121 | |
| 122 if (!channel_->Send(message)) { | |
| 123 LOG(ERROR) << "ChannelProxy::Send() failed after " | |
| 124 << message_index_ << " messages"; | |
| 125 base::MessageLoop::current()->Quit(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 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(); | |
| 136 } | |
| 137 | |
| 138 bool ReplayProcess::OnMessageReceived(const IPC::Message& msg) { | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 142 void ReplayProcess::OnChannelError() { | |
| 143 LOG(ERROR) << "Channel error, quitting after " | |
| 144 << message_index_ << " messages"; | |
| 145 base::MessageLoop::current()->Quit(); | |
| 146 } | |
| 147 | |
| 148 } // namespace ipc_fuzzer | |
| OLD | NEW |