| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/ipc_fuzzer/replay/replay_process.h" | 5 #include "tools/ipc_fuzzer/replay/replay_process.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/posix/global_descriptors.h" | 13 #include "base/posix/global_descriptors.h" |
| 14 #include "base/stl_util.h" | |
| 15 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 16 #include "ipc/ipc_descriptors.h" | 15 #include "ipc/ipc_descriptors.h" |
| 17 #include "ipc/ipc_switches.h" | 16 #include "ipc/ipc_switches.h" |
| 18 | 17 |
| 19 namespace ipc_fuzzer { | 18 namespace ipc_fuzzer { |
| 20 | 19 |
| 21 ReplayProcess::ReplayProcess() | 20 ReplayProcess::ReplayProcess() |
| 22 : main_loop_(base::MessageLoop::TYPE_DEFAULT), | 21 : main_loop_(base::MessageLoop::TYPE_DEFAULT), |
| 23 io_thread_("Chrome_ChildIOThread"), | 22 io_thread_("Chrome_ChildIOThread"), |
| 24 shutdown_event_(true, false) { | 23 shutdown_event_(true, false), |
| 24 message_index_(0) { |
| 25 } | 25 } |
| 26 | 26 |
| 27 ReplayProcess::~ReplayProcess() { | 27 ReplayProcess::~ReplayProcess() { |
| 28 channel_.reset(); | 28 channel_.reset(); |
| 29 STLDeleteElements(&messages_); | |
| 30 } | 29 } |
| 31 | 30 |
| 32 bool ReplayProcess::Initialize(int argc, const char** argv) { | 31 bool ReplayProcess::Initialize(int argc, const char** argv) { |
| 33 CommandLine::Init(argc, argv); | 32 CommandLine::Init(argc, argv); |
| 34 | 33 |
| 35 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 34 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 36 switches::kIpcFuzzerTestcase)) { | 35 switches::kIpcFuzzerTestcase)) { |
| 37 LOG(ERROR) << "This binary shouldn't be executed directly, " | 36 LOG(ERROR) << "This binary shouldn't be executed directly, " |
| 38 << "please use tools/ipc_fuzzer/play_testcase.py"; | 37 << "please use tools/ipc_fuzzer/play_testcase.py"; |
| 39 return false; | 38 return false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 57 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 56 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 58 switches::kProcessChannelID); | 57 switches::kProcessChannelID); |
| 59 | 58 |
| 60 channel_.reset( | 59 channel_.reset( |
| 61 new IPC::ChannelProxy(channel_name, | 60 new IPC::ChannelProxy(channel_name, |
| 62 IPC::Channel::MODE_CLIENT, | 61 IPC::Channel::MODE_CLIENT, |
| 63 this, | 62 this, |
| 64 io_thread_.message_loop_proxy())); | 63 io_thread_.message_loop_proxy())); |
| 65 } | 64 } |
| 66 | 65 |
| 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() { | 66 bool ReplayProcess::OpenTestcase() { |
| 92 base::FilePath path = CommandLine::ForCurrentProcess()->GetSwitchValuePath( | 67 base::FilePath path = CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 93 switches::kIpcFuzzerTestcase); | 68 switches::kIpcFuzzerTestcase); |
| 94 mapped_testcase_.reset(new base::MemoryMappedFile()); | 69 return MessageFile::Read(path, &messages_); |
| 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 } | 70 } |
| 105 | 71 |
| 106 void ReplayProcess::SendNextMessage() { | 72 void ReplayProcess::SendNextMessage() { |
| 107 if (messages_.empty()) { | 73 if (message_index_ >= messages_.size()) { |
| 108 base::MessageLoop::current()->Quit(); | 74 base::MessageLoop::current()->Quit(); |
| 109 return; | 75 return; |
| 110 } | 76 } |
| 111 | 77 |
| 112 IPC::Message* message = messages_.front(); | 78 // Take next message and release it from vector. |
| 113 messages_.pop_front(); | 79 IPC::Message* message = messages_[message_index_]; |
| 80 messages_[message_index_++] = NULL; |
| 114 | 81 |
| 115 if (!channel_->Send(message)) { | 82 if (!channel_->Send(message)) { |
| 116 LOG(ERROR) << "ChannelProxy::Send() failed"; | 83 LOG(ERROR) << "ChannelProxy::Send() failed"; |
| 117 base::MessageLoop::current()->Quit(); | 84 base::MessageLoop::current()->Quit(); |
| 118 } | 85 } |
| 119 } | 86 } |
| 120 | 87 |
| 121 void ReplayProcess::Run() { | 88 void ReplayProcess::Run() { |
| 122 timer_.reset(new base::Timer(false, true)); | 89 timer_.reset(new base::Timer(false, true)); |
| 123 timer_->Start(FROM_HERE, | 90 timer_->Start(FROM_HERE, |
| 124 base::TimeDelta::FromMilliseconds(1), | 91 base::TimeDelta::FromMilliseconds(1), |
| 125 base::Bind(&ReplayProcess::SendNextMessage, | 92 base::Bind(&ReplayProcess::SendNextMessage, |
| 126 base::Unretained(this))); | 93 base::Unretained(this))); |
| 127 base::MessageLoop::current()->Run(); | 94 base::MessageLoop::current()->Run(); |
| 128 } | 95 } |
| 129 | 96 |
| 130 bool ReplayProcess::OnMessageReceived(const IPC::Message& msg) { | 97 bool ReplayProcess::OnMessageReceived(const IPC::Message& msg) { |
| 131 return true; | 98 return true; |
| 132 } | 99 } |
| 133 | 100 |
| 134 void ReplayProcess::OnChannelError() { | 101 void ReplayProcess::OnChannelError() { |
| 135 LOG(ERROR) << "Channel error, quitting"; | 102 LOG(ERROR) << "Channel error, quitting"; |
| 136 base::MessageLoop::current()->Quit(); | 103 base::MessageLoop::current()->Quit(); |
| 137 } | 104 } |
| 138 | 105 |
| 139 } // namespace ipc_fuzzer | 106 } // namespace ipc_fuzzer |
| OLD | NEW |