#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "ipc/ipc_message_utils.h"
#include <stddef.h>
#include <stdint.h>
#include <string_view>
#include <type_traits>
#include "base/files/file_path.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "ipc/ipc_channel_handle.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_attachment.h"
#include "ipc/ipc_message_attachment_set.h"
#include "ipc/ipc_mojo_param_traits.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"
#if BUILDFLAG(IS_APPLE)
#include "ipc/mach_port_mac.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <tchar.h>
#include "ipc/handle_win.h"
#include "ipc/ipc_platform_file.h"
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include "base/file_descriptor_posix.h"
#include "ipc/ipc_platform_file_attachment_posix.h"
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include "base/fuchsia/fuchsia_logging.h"
#include "ipc/handle_attachment_fuchsia.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/scoped_hardware_buffer_handle.h"
#include "ipc/ipc_mojo_handle_attachment.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "mojo/public/cpp/system/scope_to_message_pipe.h"
#endif
namespace IPC {
namespace {
const int kMaxRecursionDepth = …;
template<typename CharType>
void LogBytes(const std::vector<CharType>& data, std::string* out) { … }
template <typename CharType>
void WriteCharVector(base::Pickle* m, const std::vector<CharType>& p) { … }
template <typename CharType>
bool ReadCharVector(const base::Pickle* m,
base::PickleIterator* iter,
std::vector<CharType>* r) { … }
void WriteValue(const base::Value& value, int recursion, base::Pickle* pickle);
void WriteDictValue(const base::Value::Dict& value,
int recursion,
base::Pickle* pickle) { … }
void WriteListValue(const base::Value::List& value,
int recursion,
base::Pickle* pickle) { … }
void WriteValue(const base::Value& value, int recursion, base::Pickle* pickle) { … }
bool ReadValue(const base::Pickle* pickle,
base::PickleIterator* iter,
int recursion,
base::Value* value);
bool ReadDictValue(const base::Pickle* pickle,
base::PickleIterator* iter,
int recursion,
base::Value::Dict* value) { … }
bool ReadListValue(const base::Pickle* pickle,
base::PickleIterator* iter,
int recursion,
base::Value::List* value) { … }
bool ReadValue(const base::Pickle* pickle,
base::PickleIterator* iter,
int recursion,
base::Value* value) { … }
}
LogData::LogData()
: … { … }
LogData::LogData(const LogData& other) = default;
LogData::~LogData() = default;
void ParamTraits<bool>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<signed char>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<signed char>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<signed char>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<unsigned char>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<unsigned char>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<unsigned short>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<unsigned short>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<int>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) { … }
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_FUCHSIA) || \
(BUILDFLAG(IS_ANDROID) && defined(ARCH_CPU_64_BITS))
void ParamTraits<long>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) { … }
#endif
void ParamTraits<long long>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<float>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<double>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<double>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<double>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<std::string>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<std::u16string>::Log(const param_type& p, std::string* l) { … }
#if BUILDFLAG(IS_WIN)
bool ParamTraits<std::wstring>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) {
std::u16string_view piece16;
if (!iter->ReadStringPiece16(&piece16))
return false;
*r = base::AsWString(piece16);
return true;
}
void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
l->append(base::WideToUTF8(p));
}
#endif
void ParamTraits<std::vector<char>>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<std::vector<char>>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<std::vector<char>>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<std::vector<unsigned char>>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<std::vector<unsigned char>>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
std::string* l) { … }
void ParamTraits<std::vector<bool>>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<std::vector<bool>>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) { … }
void ParamTraits<base::Value::Dict>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<base::Value::Dict>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::Value::Dict>::Log(const param_type& p, std::string* l) { … }
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
void ParamTraits<base::FileDescriptor>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<base::FileDescriptor>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
std::string* l) { … }
void ParamTraits<base::ScopedFD>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<base::ScopedFD>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::ScopedFD>::Log(const param_type& p, std::string* l) { … }
#endif
#if BUILDFLAG(IS_WIN)
void ParamTraits<base::win::ScopedHandle>::Write(base::Pickle* m,
const param_type& p) {
const bool valid = p.IsValid();
WriteParam(m, valid);
if (!valid)
return;
HandleWin handle(p.Get());
WriteParam(m, handle);
}
bool ParamTraits<base::win::ScopedHandle>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) {
r->Close();
bool valid;
if (!ReadParam(m, iter, &valid))
return false;
if (!valid)
return true;
HandleWin handle;
if (!ReadParam(m, iter, &handle))
return false;
r->Set(handle.get_handle());
return true;
}
void ParamTraits<base::win::ScopedHandle>::Log(const param_type& p,
std::string* l) {
l->append(base::StringPrintf("ScopedHandle(%p)", p.Get()));
}
#endif
#if BUILDFLAG(IS_FUCHSIA)
void ParamTraits<zx::vmo>::Write(base::Pickle* m, const param_type& p) {
const bool valid = p.is_valid();
WriteParam(m, valid);
if (!valid)
return;
if (!m->WriteAttachment(new internal::HandleAttachmentFuchsia(
std::move(const_cast<param_type&>(p))))) {
NOTREACHED_IN_MIGRATION();
}
}
bool ParamTraits<zx::vmo>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) {
r->reset();
bool valid;
if (!ReadParam(m, iter, &valid))
return false;
if (!valid)
return true;
scoped_refptr<base::Pickle::Attachment> attachment;
if (!m->ReadAttachment(iter, &attachment))
return false;
if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
MessageAttachment::Type::FUCHSIA_HANDLE) {
return false;
}
*r = zx::vmo(static_cast<internal::HandleAttachmentFuchsia*>(attachment.get())
->Take());
return true;
}
void ParamTraits<zx::vmo>::Log(const param_type& p, std::string* l) {
l->append("ZirconVMO");
}
void ParamTraits<zx::channel>::Write(base::Pickle* m, const param_type& p) {
const bool valid = p.is_valid();
WriteParam(m, valid);
if (!valid)
return;
if (!m->WriteAttachment(new internal::HandleAttachmentFuchsia(
std::move(const_cast<param_type&>(p))))) {
NOTREACHED_IN_MIGRATION();
}
}
bool ParamTraits<zx::channel>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) {
r->reset();
bool valid;
if (!ReadParam(m, iter, &valid))
return false;
if (!valid)
return true;
scoped_refptr<base::Pickle::Attachment> attachment;
if (!m->ReadAttachment(iter, &attachment))
return false;
if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
MessageAttachment::Type::FUCHSIA_HANDLE) {
return false;
}
*r = zx::channel(
static_cast<internal::HandleAttachmentFuchsia*>(attachment.get())
->Take());
return true;
}
void ParamTraits<zx::channel>::Log(const param_type& p, std::string* l) {
l->append("ZirconChannel");
}
#endif
#if BUILDFLAG(IS_ANDROID)
void ParamTraits<base::android::ScopedHardwareBufferHandle>::Write(
base::Pickle* m,
const param_type& p) {
const bool is_valid = p.is_valid();
WriteParam(m, is_valid);
if (!is_valid)
return;
mojo::MessagePipe tracking_pipe;
m->WriteAttachment(new internal::MojoHandleAttachment(
mojo::ScopedHandle::From(std::move(tracking_pipe.handle0))));
WriteParam(m, base::FileDescriptor(p.SerializeAsFileDescriptor().release(),
true ));
mojo::ScopeToMessagePipe(std::move(const_cast<param_type&>(p)),
std::move(tracking_pipe.handle1));
}
bool ParamTraits<base::android::ScopedHardwareBufferHandle>::Read(
const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) {
*r = base::android::ScopedHardwareBufferHandle();
bool is_valid;
if (!ReadParam(m, iter, &is_valid))
return false;
if (!is_valid)
return true;
scoped_refptr<base::Pickle::Attachment> tracking_pipe_attachment;
if (!m->ReadAttachment(iter, &tracking_pipe_attachment))
return false;
mojo::ScopedHandle tracking_pipe =
static_cast<MessageAttachment*>(tracking_pipe_attachment.get())
->TakeMojoHandle();
base::FileDescriptor descriptor;
if (!ReadParam(m, iter, &descriptor))
return false;
base::ScopedFD scoped_fd(descriptor.fd);
if (!scoped_fd.is_valid())
return false;
*r = base::android::ScopedHardwareBufferHandle::DeserializeFromFileDescriptor(
std::move(scoped_fd));
return true;
}
void ParamTraits<base::android::ScopedHardwareBufferHandle>::Log(
const param_type& p,
std::string* l) {
l->append(base::StringPrintf("base::android::ScopedHardwareBufferHandle(%p)",
p.get()));
}
#endif
void ParamTraits<base::ReadOnlySharedMemoryRegion>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<base::ReadOnlySharedMemoryRegion>::Read(
const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::ReadOnlySharedMemoryRegion>::Log(const param_type& p,
std::string* l) { … }
void ParamTraits<base::WritableSharedMemoryRegion>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<base::WritableSharedMemoryRegion>::Read(
const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::WritableSharedMemoryRegion>::Log(const param_type& p,
std::string* l) { … }
void ParamTraits<base::UnsafeSharedMemoryRegion>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<base::UnsafeSharedMemoryRegion>::Read(
const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::UnsafeSharedMemoryRegion>::Log(const param_type& p,
std::string* l) { … }
void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Write(
base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Read(
const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Log(
const param_type& p,
std::string* l) { … }
void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Write(
base::Pickle* m,
const param_type& value) { … }
bool ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Read(
const base::Pickle* m,
base::PickleIterator* iter,
param_type* p) { … }
void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Log(
const param_type& p,
std::string* l) { … }
#if BUILDFLAG(IS_WIN)
void ParamTraits<PlatformFileForTransit>::Write(base::Pickle* m,
const param_type& p) {
m->WriteBool(p.IsValid());
if (p.IsValid()) {
HandleWin handle_win(p.GetHandle());
ParamTraits<HandleWin>::Write(m, handle_win);
::CloseHandle(p.GetHandle());
}
}
bool ParamTraits<PlatformFileForTransit>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) {
bool is_valid;
if (!iter->ReadBool(&is_valid))
return false;
if (!is_valid) {
*r = PlatformFileForTransit();
return true;
}
HandleWin handle_win;
if (!ParamTraits<HandleWin>::Read(m, iter, &handle_win))
return false;
*r = PlatformFileForTransit(handle_win.get_handle());
return true;
}
void ParamTraits<PlatformFileForTransit>::Log(const param_type& p,
std::string* l) {
LogParam(p.GetHandle(), l);
}
#endif
void ParamTraits<base::FilePath>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<base::FilePath>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<base::Value::List>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<base::Value::List>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::Value::List>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<base::Value>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<base::Value>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::Value>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<base::File::Info>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<base::File::Info>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* p) { … }
void ParamTraits<base::File::Info>::Log(const param_type& p,
std::string* l) { … }
void ParamTraits<base::Time>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<base::Time>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<base::TimeDelta>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<base::TimeDelta>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<base::TimeTicks>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<base::TimeTicks>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) { … }
static_assert …;
void ParamTraits<base::UnguessableToken>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<base::UnguessableToken>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<base::UnguessableToken>::Log(const param_type& p,
std::string* l) { … }
void ParamTraits<IPC::ChannelHandle>::Write(base::Pickle* m,
const param_type& p) { … }
bool ParamTraits<IPC::ChannelHandle>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
std::string* l) { … }
void ParamTraits<LogData>::Write(base::Pickle* m, const param_type& p) { … }
bool ParamTraits<LogData>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) { … }
void ParamTraits<LogData>::Log(const param_type& p, std::string* l) { … }
void ParamTraits<Message>::Write(base::Pickle* m, const Message& p) { … }
bool ParamTraits<Message>::Read(const base::Pickle* m,
base::PickleIterator* iter,
Message* r) { … }
void ParamTraits<Message>::Log(const Message& p, std::string* l) { … }
#if BUILDFLAG(IS_WIN)
void ParamTraits<HANDLE>::Write(base::Pickle* m, const param_type& p) {
m->WriteInt(HandleToLong(p));
}
bool ParamTraits<HANDLE>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) {
int32_t temp;
if (!iter->ReadInt(&temp))
return false;
*r = LongToHandle(temp);
return true;
}
void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
l->append(base::StringPrintf("0x%p", p));
}
void ParamTraits<MSG>::Write(base::Pickle* m, const param_type& p) {
m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
}
bool ParamTraits<MSG>::Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) {
const char *data;
size_t data_size = 0;
bool result = iter->ReadData(&data, &data_size);
if (result && data_size == sizeof(MSG)) {
memcpy(r, data, sizeof(MSG));
} else {
result = false;
NOTREACHED_IN_MIGRATION();
}
return result;
}
void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
l->append("<MSG>");
}
#endif
}