Chromium Code Reviews| Index: tools/ipc_fuzzer/ipc_fuzzer_main.cc |
| diff --git a/tools/ipc_fuzzer/ipc_fuzzer_main.cc b/tools/ipc_fuzzer/ipc_fuzzer_main.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..671d5c4c43f2340dde7d6a7438532d086e5d7229 |
| --- /dev/null |
| +++ b/tools/ipc_fuzzer/ipc_fuzzer_main.cc |
| @@ -0,0 +1,209 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <limits.h> |
| +#include <stdio.h> |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/command_line.h" |
| +#include "base/files/memory_mapped_file.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/posix/global_descriptors.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "base/threading/thread.h" |
| +#include "base/timer/timer.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "ipc/ipc_channel_proxy.h" |
| +#include "ipc/ipc_descriptors.h" |
| +#include "ipc/ipc_listener.h" |
| +#include "ipc/ipc_message.h" |
| +#include "ipc/ipc_platform_file.h" |
| +#include "ipc/ipc_switches.h" |
| + |
| +namespace { |
| + |
| +class IpcFuzzerProcess : public IPC::Listener { |
| + public: |
| + IpcFuzzerProcess(); |
| + virtual ~IpcFuzzerProcess(); |
| + |
| + // Set up command line, logging, IO thread. |
| + void Initialize(int argc, const char **argv); |
| + |
| + // Open a channel to the browser process. It will think we are a renderer. |
| + void OpenChannel(); |
| + |
| + // Extract messages from a file specified by --ipc-fuzzer-testcase=. |
| + bool OpenTestcase(); |
| + |
| + // Trigger the sending of messages to the browser. |
| + void StartSendingMessages(); |
| + |
| + // IPC::Listener implementation. |
| + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| + virtual void OnChannelError() OVERRIDE; |
| + |
| + private: |
| + bool ExtractMessages(const char *data, size_t len); |
| + IPC::Message* GetNextMessage(); |
| + void SendNextMessage(); |
| + |
| + scoped_ptr<IPC::ChannelProxy> channel_; |
| + base::MessageLoop main_loop_; |
| + base::Thread io_thread_; |
| + base::WaitableEvent shutdown_event_; |
| + scoped_ptr<base::Timer> timer_; |
| + scoped_ptr<base::MemoryMappedFile> testcase_map_; |
| + std::vector<scoped_ptr<IPC::Message>> messages_; |
|
Tom Sepez
2013/11/26 21:30:44
nit: > >
aedla
2013/11/27 19:24:51
Moved to std::list<IPC::Message*>.
|
| + size_t current_message_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(IpcFuzzerProcess); |
| +}; |
| + |
| +IpcFuzzerProcess::IpcFuzzerProcess() |
| + : main_loop_(base::MessageLoop::TYPE_DEFAULT), |
| + io_thread_("Chrome_ChildIOThread"), |
| + shutdown_event_(true, false), |
| + current_message_(0) { |
| +} |
| + |
| +IpcFuzzerProcess::~IpcFuzzerProcess() { |
| + channel_.reset(); |
| +} |
| + |
| +void IpcFuzzerProcess::Initialize(int argc, const char **argv) { |
| + CommandLine::Init(argc, argv); |
| + |
| + // Log to default destination. |
| + logging::SetMinLogLevel(logging::LOG_ERROR); |
| + logging::InitLogging(logging::LoggingSettings()); |
| + |
| + io_thread_.StartWithOptions( |
| + base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| + |
| +#if defined(OS_POSIX) |
| + base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); |
| + g_fds->Set(kPrimaryIPCChannel, |
| + kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); |
| +#endif |
| +} |
| + |
| +void IpcFuzzerProcess::OpenChannel() { |
| + std::string channel_name = |
| + CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| + switches::kProcessChannelID); |
| + |
| + channel_.reset( |
| + new IPC::ChannelProxy(channel_name, |
| + IPC::Channel::MODE_CLIENT, |
| + this, |
| + io_thread_.message_loop_proxy())); |
| +} |
| + |
| +bool IpcFuzzerProcess::ExtractMessages(const char *data, size_t len) { |
| + const char* end = data + len; |
| + |
| + while (data < end) { |
| + const char* message_tail = IPC::Message::FindNext(data, end); |
| + if (!message_tail) |
| + break; |
| + |
| + size_t len = message_tail - data; |
| + if (len > INT_MAX) |
| + break; |
|
Tom Sepez
2013/11/26 18:25:05
nit: do you want to do something more extreme then
aedla
2013/11/27 19:24:51
Yeah, added an error message here. It previously f
|
| + |
| + IPC::Message* message = new IPC::Message(data, len); |
| + data = message_tail; |
| + |
| + messages_.resize(messages_.size() + 1); |
| + messages_.back().reset(message); |
| + } |
| + |
| + if (data < end) { |
| + unsigned long left = end - data; |
| + LOG(ERROR) << left << " bytes left while extracting messages"; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool IpcFuzzerProcess::OpenTestcase() { |
| + const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| + |
| + if (!command_line.HasSwitch(switches::kIpcFuzzerTestcase)) { |
| + LOG(ERROR) << "No IPC fuzzer testcase specified"; |
| + return false; |
| + } |
| + |
| + base::FilePath path = |
| + command_line.GetSwitchValuePath(switches::kIpcFuzzerTestcase); |
| + testcase_map_.reset(new base::MemoryMappedFile()); |
| + if (!testcase_map_->Initialize(path)) { |
| + LOG(ERROR) << "Failed to map testcase: " << path.value(); |
| + return false; |
| + } |
| + |
| + const char* data = reinterpret_cast<const char *>(testcase_map_->data()); |
| + size_t len = testcase_map_->length(); |
| + |
| + return ExtractMessages(data, len); |
| +} |
| + |
| +IPC::Message* IpcFuzzerProcess::GetNextMessage() { |
| + if (current_message_ == messages_.size()) |
| + return NULL; |
| + |
| + IPC::Message* const_message = messages_[current_message_].get(); |
| + IPC::Message* message = new IPC::Message(*const_message); |
|
Tom Sepez
2013/11/26 18:25:05
nit: might be nice if we didn't have to duplicate
aedla
2013/11/27 19:24:51
Done.
|
| + ++current_message_; |
| + |
| + return message; |
| +} |
| + |
| +void IpcFuzzerProcess::SendNextMessage() { |
| + IPC::Message* message = GetNextMessage(); |
| + if (!message) { |
| + base::MessageLoop::current()->Quit(); |
| + return; |
| + } |
| + |
| + channel_->Send(message); |
| +} |
| + |
| +void IpcFuzzerProcess::StartSendingMessages() { |
| + timer_.reset(new base::Timer(false, true)); |
| + timer_->Start(FROM_HERE, |
| + base::TimeDelta::FromMilliseconds(1), |
| + base::Bind(&IpcFuzzerProcess::SendNextMessage, |
| + base::Unretained(this))); |
| +} |
| + |
| +bool IpcFuzzerProcess::OnMessageReceived(const IPC::Message& msg) { |
| + return true; |
| +} |
| + |
| +void IpcFuzzerProcess::OnChannelError() { |
| + LOG(ERROR) << "Channel error, quitting"; |
| + base::MessageLoop::current()->Quit(); |
| +} |
| + |
| +} // namespace |
| + |
| +int main(int argc, const char **argv) { |
| + IpcFuzzerProcess fuzzer; |
| + fuzzer.Initialize(argc, argv); |
| + fuzzer.OpenChannel(); |
| + |
| + if (!fuzzer.OpenTestcase()) |
| + return 0; |
| + |
| + fuzzer.StartSendingMessages(); |
| + |
| + base::MessageLoop::current()->Run(); |
| + return 0; |
| +} |