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 "base/stl_util.h" |
| 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "ipc/ipc_descriptors.h" |
| 17 #include "ipc/ipc_switches.h" |
| 18 |
| 19 namespace ipc_fuzzer { |
| 20 |
| 21 ReplayProcess::ReplayProcess() |
| 22 : main_loop_(base::MessageLoop::TYPE_DEFAULT), |
| 23 io_thread_("Chrome_ChildIOThread"), |
| 24 shutdown_event_(true, false) { |
| 25 } |
| 26 |
| 27 ReplayProcess::~ReplayProcess() { |
| 28 channel_.reset(); |
| 29 STLDeleteElements(&messages_); |
| 30 } |
| 31 |
| 32 bool ReplayProcess::Initialize(int argc, const char** argv) { |
| 33 CommandLine::Init(argc, argv); |
| 34 |
| 35 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 36 switches::kIpcFuzzerTestcase)) { |
| 37 LOG(ERROR) << "This binary shouldn't be executed directly, " |
| 38 << "please use tools/ipc_fuzzer/play_testcase.py"; |
| 39 return false; |
| 40 } |
| 41 |
| 42 // Log to default destination. |
| 43 logging::SetMinLogLevel(logging::LOG_ERROR); |
| 44 logging::InitLogging(logging::LoggingSettings()); |
| 45 |
| 46 io_thread_.StartWithOptions( |
| 47 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 48 |
| 49 base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); |
| 50 g_fds->Set(kPrimaryIPCChannel, |
| 51 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); |
| 52 return true; |
| 53 } |
| 54 |
| 55 void ReplayProcess::OpenChannel() { |
| 56 std::string channel_name = |
| 57 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 58 switches::kProcessChannelID); |
| 59 |
| 60 channel_.reset( |
| 61 new IPC::ChannelProxy(channel_name, |
| 62 IPC::Channel::MODE_CLIENT, |
| 63 this, |
| 64 io_thread_.message_loop_proxy())); |
| 65 } |
| 66 |
| 67 bool ReplayProcess::ExtractMessages(const char *data, size_t len) { |
| 68 const char* end = data + len; |
| 69 |
| 70 while (data < end) { |
| 71 const char* message_tail = IPC::Message::FindNext(data, end); |
| 72 if (!message_tail) { |
| 73 LOG(ERROR) << "Failed to extract message"; |
| 74 return false; |
| 75 } |
| 76 |
| 77 size_t len = message_tail - data; |
| 78 if (len > INT_MAX) { |
| 79 LOG(ERROR) << "Message too large"; |
| 80 return false; |
| 81 } |
| 82 |
| 83 IPC::Message* message = new IPC::Message(data, len); |
| 84 messages_.push_back(message); |
| 85 data = message_tail; |
| 86 } |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 bool ReplayProcess::OpenTestcase() { |
| 92 base::FilePath path = CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 93 switches::kIpcFuzzerTestcase); |
| 94 mapped_testcase_.reset(new base::MemoryMappedFile()); |
| 95 if (!mapped_testcase_->Initialize(path)) { |
| 96 LOG(ERROR) << "Failed to map testcase: " << path.value(); |
| 97 return false; |
| 98 } |
| 99 |
| 100 const char* data = reinterpret_cast<const char *>(mapped_testcase_->data()); |
| 101 size_t len = mapped_testcase_->length(); |
| 102 |
| 103 return ExtractMessages(data, len); |
| 104 } |
| 105 |
| 106 void ReplayProcess::SendNextMessage() { |
| 107 if (messages_.empty()) { |
| 108 base::MessageLoop::current()->Quit(); |
| 109 return; |
| 110 } |
| 111 |
| 112 IPC::Message* message = messages_.front(); |
| 113 messages_.pop_front(); |
| 114 |
| 115 if (!channel_->Send(message)) { |
| 116 LOG(ERROR) << "ChannelProxy::Send() failed"; |
| 117 base::MessageLoop::current()->Quit(); |
| 118 } |
| 119 } |
| 120 |
| 121 void ReplayProcess::Run() { |
| 122 timer_.reset(new base::Timer(false, true)); |
| 123 timer_->Start(FROM_HERE, |
| 124 base::TimeDelta::FromMilliseconds(1), |
| 125 base::Bind(&ReplayProcess::SendNextMessage, |
| 126 base::Unretained(this))); |
| 127 base::MessageLoop::current()->Run(); |
| 128 } |
| 129 |
| 130 bool ReplayProcess::OnMessageReceived(const IPC::Message& msg) { |
| 131 return true; |
| 132 } |
| 133 |
| 134 void ReplayProcess::OnChannelError() { |
| 135 LOG(ERROR) << "Channel error, quitting"; |
| 136 base::MessageLoop::current()->Quit(); |
| 137 } |
| 138 |
| 139 } // namespace ipc_fuzzer |
OLD | NEW |