#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "mojo/core/channel.h"
#include <stddef.h>
#include <string.h>
#include <cstdint>
#include <limits>
#include <utility>
#include "base/check_op.h"
#include "base/functional/overloaded.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/safe_math.h"
#include "base/process/current_process.h"
#include "base/process/process_handle.h"
#include "base/ranges/algorithm.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/typed_macros.h"
#include "build/build_config.h"
#include "mojo/core/configuration.h"
#include "mojo/core/embedder/features.h"
#if BUILDFLAG(MOJO_USE_APPLE_CHANNEL)
#include "base/apple/mach_logging.h"
#elif BUILDFLAG(IS_WIN)
#include "base/win/win_util.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "mojo/core/channel_binder.h"
#endif
namespace mojo::core {
namespace {
std::atomic_bool g_use_trivial_messages{ … };
constexpr int kGrowthFactor = …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
const size_t kReadBufferSize = …;
const size_t kMaxUnusedReadBufferCapacity = …;
const size_t kMaxAttachedHandles = …;
static_assert …;
Channel::AlignedBuffer MakeAlignedBuffer(size_t size) { … }
struct TrivialMessage;
struct IpczMessage : public Channel::Message { … };
struct ComplexMessage : public Channel::Message { … };
struct TrivialMessage : public Channel::Message { … };
static_assert …;
}
Channel::MessagePtr Channel::Message::CreateMessage(size_t payload_size,
size_t max_handles) { … }
Channel::MessagePtr Channel::Message::CreateMessage(size_t payload_size,
size_t max_handles,
MessageType message_type) { … }
Channel::MessagePtr Channel::Message::CreateMessage(size_t capacity,
size_t payload_size,
size_t max_handles) { … }
Channel::MessagePtr Channel::Message::CreateMessage(size_t capacity,
size_t payload_size,
size_t max_handles,
MessageType message_type) { … }
Channel::MessagePtr Channel::Message::CreateIpczMessage(
base::span<const uint8_t> data,
std::vector<PlatformHandle> handles) { … }
void Channel::set_use_trivial_messages(bool use_trivial_messages) { … }
Channel::MessagePtr Channel::Message::CreateRawForFuzzing(
base::span<const unsigned char> data) { … }
Channel::MessagePtr Channel::Message::Deserialize(
const void* data,
size_t data_num_bytes,
HandlePolicy handle_policy,
base::ProcessHandle from_process) { … }
void Channel::Message::ExtendPayload(MessagePtr& message,
size_t new_payload_size) { … }
const void* Channel::Message::extra_header() const { … }
void* Channel::Message::mutable_extra_header() { … }
size_t Channel::Message::extra_header_size() const { … }
void* Channel::Message::mutable_payload() { … }
const void* Channel::Message::payload() const { … }
size_t Channel::Message::payload_size() const { … }
size_t Channel::Message::num_handles() const { … }
bool Channel::Message::has_handles() const { … }
bool Channel::Message::is_legacy_message() const { … }
Channel::Message::LegacyHeader* Channel::Message::legacy_header() const { … }
Channel::Message::Header* Channel::Message::header() const { … }
ComplexMessage::ComplexMessage(size_t capacity,
size_t payload_size,
size_t max_handles,
MessageType message_type)
: … { … }
size_t ComplexMessage::capacity() const { … }
bool ComplexMessage::ExtendPayload(size_t new_payload_size) { … }
void ComplexMessage::SetHandles(std::vector<PlatformHandle> new_handles) { … }
void ComplexMessage::SetHandles(
std::vector<PlatformHandleInTransit> new_handles) { … }
std::vector<PlatformHandleInTransit> ComplexMessage::TakeHandles() { … }
size_t ComplexMessage::NumHandlesForTransit() const { … }
Channel::MessagePtr TrivialMessage::TryConstruct(size_t payload_size,
MessageType message_type) { … }
size_t TrivialMessage::capacity() const { … }
bool TrivialMessage::ExtendPayload(size_t new_payload_size) { … }
void TrivialMessage::SetHandles(std::vector<PlatformHandle> new_handles) { … }
void TrivialMessage::SetHandles(
std::vector<PlatformHandleInTransit> new_handles) { … }
std::vector<PlatformHandleInTransit> TrivialMessage::TakeHandles() { … }
size_t TrivialMessage::NumHandlesForTransit() const { … }
class Channel::ReadBuffer { … };
bool Channel::Delegate::IsIpczTransport() const { … }
void Channel::Delegate::OnChannelDestroyed() { … }
Channel::Channel(Delegate* delegate,
HandlePolicy handle_policy,
DispatchBufferPolicy buffer_policy)
: … { … }
Channel::~Channel() { … }
scoped_refptr<Channel> Channel::CreateForIpczDriver(
Delegate* delegate,
PlatformChannelEndpoint endpoint,
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) { … }
void Channel::ShutDown() { … }
char* Channel::GetReadBuffer(size_t* buffer_capacity) { … }
bool Channel::OnReadComplete(size_t bytes_read, size_t* next_read_size_hint) { … }
Channel::DispatchResult Channel::TryDispatchMessage(
base::span<const char> buffer,
size_t* size_hint) { … }
Channel::DispatchResult Channel::TryDispatchMessage(
base::span<const char> buffer,
std::optional<std::vector<PlatformHandle>> received_handles,
size_t* size_hint) { … }
void Channel::OnError(Error error) { … }
bool Channel::OnControlMessage(Message::MessageType message_type,
const void* payload,
size_t payload_size,
std::vector<PlatformHandle> handles) { … }
void Channel::LogHistogramForIPCMetrics(MessageType type) { … }
#if BUILDFLAG(IS_NACL) || (!(BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX) || \
BUILDFLAG(IS_ANDROID)))
MOJO_SYSTEM_IMPL_EXPORT bool Channel::SupportsChannelUpgrade() {
return false;
}
MOJO_SYSTEM_IMPL_EXPORT void Channel::OfferChannelUpgrade() {
NOTREACHED();
}
#endif
bool Channel::ShouldRecordSubsampledHistograms() { … }
}