#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "mojo/core/node_channel.h"
#include <stdint.h>
#include <algorithm>
#include "base/functional/callback_helpers.h"
#include "base/message_loop/message_pump_type.h"
#include "base/no_destructor.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_executor.h"
#include "build/build_config.h"
#include "mojo/core/channel.h"
#include "mojo/core/connection_params.h"
#include "mojo/core/entrypoints.h"
#include "mojo/core/ipcz_driver/envelope.h"
#include "mojo/core/test/mock_node_channel_delegate.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif
using mojo::core::Channel;
using mojo::core::ConnectionParams;
using mojo::core::ports::NodeName;
class FakeChannelDelegate : public Channel::Delegate {
public:
FakeChannelDelegate() = default;
~FakeChannelDelegate() override = default;
void OnChannelMessage(
const void* payload,
size_t payload_size,
std::vector<mojo::PlatformHandle> handles,
scoped_refptr<mojo::core::ipcz_driver::Envelope> envelope) override {}
void OnChannelError(Channel::Error error) override {}
};
struct Environment {
Environment() : main_thread_task_executor(base::MessagePumpType::IO) {
mojo::core::InitializeCore();
}
base::SingleThreadTaskExecutor main_thread_task_executor;
};
extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, size_t size) {
static base::NoDestructor<Environment> environment;
mojo::PlatformChannel channel;
mojo::core::MockNodeChannelDelegate receiver_delegate;
auto receiver = mojo::core::NodeChannel::Create(
&receiver_delegate, ConnectionParams(channel.TakeLocalEndpoint()),
Channel::HandlePolicy::kRejectHandles,
environment->main_thread_task_executor.task_runner(), base::DoNothing());
#if BUILDFLAG(IS_WIN)
receiver->SetRemoteProcessHandle(base::Process::Current());
#endif
receiver->Start();
FakeChannelDelegate sender_delegate;
auto sender = Channel::Create(
&sender_delegate, ConnectionParams(channel.TakeRemoteEndpoint()),
Channel::HandlePolicy::kRejectHandles,
environment->main_thread_task_executor.task_runner());
sender->Start();
auto message = Channel::Message::CreateMessage(size, 0 );
std::copy(data, data + size,
static_cast<unsigned char*>(message->mutable_payload()));
sender->Write(std::move(message));
base::RunLoop().RunUntilIdle();
sender->ShutDown();
sender.reset();
receiver->ShutDown();
receiver.reset();
base::RunLoop().RunUntilIdle();
return 0;
}