1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
* Copyright (C) 2010-2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "ArgumentCoders.h"
#include "Logging.h"
#include "MessageArgumentDescriptions.h"
#include "MessageNames.h"
#include "StreamServerConnection.h"
#include <wtf/CompletionHandler.h>
#include <wtf/ProcessID.h>
#include <wtf/RuntimeApplicationChecks.h>
#include <wtf/StdLibExtras.h>
namespace IPC {
class Connection;
// IPC message logging. Only enabled in DEBUG builds.
//
// Message argument values appear as "..." if no operator<<(TextStream&) is
// implemented for them.
constexpr unsigned loggingContainerSizeLimit = 200;
#if !LOG_DISABLED
enum class ForReply : bool { No, Yes };
template<typename C>
inline TextStream textStreamForLogging(const C& connection, MessageName messageName, void* object, ForReply forReply)
{
TextStream stream(TextStream::LineMode::SingleLine, { }, loggingContainerSizeLimit);
stream << '[';
if constexpr(requires { connection.remoteProcessID(); }) {
if (auto pid = connection.remoteProcessID())
stream << pid << ' ';
}
switch (forReply) {
case ForReply::No:
stream << "-> "_s << processTypeDescription(processType()) << ' ' << getCurrentProcessID() << " receiver "_s << object << "] "_s << description(messageName);
break;
case ForReply::Yes:
stream << "<- "_s << processTypeDescription(processType()) << ' ' << getCurrentProcessID() << "] "_s << description(messageName) << " Reply"_s;
break;
}
return stream;
}
#endif
template<typename C, typename ArgsTuple, size_t... ArgsIndex>
void logMessageImpl(const C& connection, MessageName messageName, void* object, const ArgsTuple& args, std::index_sequence<ArgsIndex...>)
{
#if !LOG_DISABLED
if (LOG_CHANNEL(IPCMessages).state != WTFLogChannelState::On)
return;
auto stream = textStreamForLogging(connection, messageName, object, ForReply::No);
if (auto argumentDescriptions = messageArgumentDescriptions(messageName))
(stream.dumpProperty((*argumentDescriptions)[ArgsIndex].name, ValueOrEllipsis(std::get<ArgsIndex>(args))), ...);
LOG(IPCMessages, "%s", stream.release().utf8().data());
#else
UNUSED_PARAM(connection);
UNUSED_PARAM(messageName);
UNUSED_PARAM(args);
#endif
}
template<typename C, typename ArgsTuple, typename ArgsIndices = std::make_index_sequence<std::tuple_size<ArgsTuple>::value>>
void logMessage(const C& connection, MessageName messageName, void* object, const ArgsTuple& args)
{
logMessageImpl(connection, messageName, object, args, ArgsIndices());
}
template<typename C, typename... T>
void logReply(const C& connection, MessageName messageName, const T&... args)
{
#if !LOG_DISABLED
if (!sizeof...(T))
return;
if (LOG_CHANNEL(IPCMessages).state != WTFLogChannelState::On)
return;
auto stream = textStreamForLogging(connection, messageName, nullptr, ForReply::Yes);
unsigned argIndex = 0;
if (auto argumentDescriptions = messageReplyArgumentDescriptions(messageName))
(stream.dumpProperty((*argumentDescriptions)[argIndex++].name, ValueOrEllipsis(args)), ...);
LOG(IPCMessages, "%s", stream.release().utf8().data());
#else
UNUSED_PARAM(connection);
UNUSED_PARAM(messageName);
(UNUSED_PARAM(args), ...);
#endif
}
// Dispatch functions with no reply arguments.
template<typename T, typename U, typename MF, typename ArgsTuple>
void callMemberFunction(T* object, MF U::* function, ArgsTuple&& tuple)
{
std::apply(
[&](auto&&... args) {
(object->*function)(std::forward<decltype(args)>(args)...);
}, std::forward<ArgsTuple>(tuple));
}
// Dispatch functions with synchronous reply arguments.
template<typename T, typename U, typename MF, typename ArgsTuple, typename CH>
void callMemberFunction(T* object, MF U::* function, ArgsTuple&& tuple, CompletionHandler<CH>&& completionHandler)
{
std::apply(
[&](auto&&... args) {
(object->*function)(std::forward<decltype(args)>(args)..., WTFMove(completionHandler));
}, std::forward<ArgsTuple>(tuple));
}
// Dispatch functions with connection parameter with synchronous reply arguments.
template<typename T, typename U, typename MF, typename ArgsTuple, typename CH>
void callMemberFunction(T* object, MF U::* function, Connection& connection, ArgsTuple&& tuple, CompletionHandler<CH>&& completionHandler)
{
std::apply(
[&](auto&&... args) {
(object->*function)(connection, std::forward<decltype(args)>(args)..., WTFMove(completionHandler));
}, std::forward<ArgsTuple>(tuple));
}
// Dispatch functions with connection parameter with no reply arguments.
template<typename T, typename U, typename MF, typename ArgsTuple>
void callMemberFunction(T* object, MF U::* function, Connection& connection, ArgsTuple&& tuple)
{
std::apply(
[&](auto&&... args) {
(object->*function)(connection, std::forward<decltype(args)>(args)...);
}, std::forward<ArgsTuple>(tuple));
}
// MethodSignatureValidation template works on function types of message-handling methods,
// deducing the expected list of argument types that a given method is expecting along with
// properly handling the possible initial Connection& argument and the possible final
// CompletionHandler<>&& argument.
// Once the template instantiations traverse across the method's arguments, the MessageArguments
// type alias will present a tuple of method's expected argument types that the handleMessage()
// variants can use for validation against the argument types specified by the message.
// In case a CompletionHandler argument is present, the CompletionHandlerArguments type alias
// will hold a list of the handler's expected argument types that again can be used for validation
// against the message's specified reply types, and the CompletionHandlerType type alias will
// provide that exact CompletionHandler type to enable proper construction of the object.
template<typename MessageArgumentTypesTuple, typename MethodArgumentTypesTuple> struct MethodSignatureValidationImpl { };
template<typename... MessageArgumentTypes, typename MethodArgumentType, typename... MethodArgumentTypes>
struct MethodSignatureValidationImpl<std::tuple<MessageArgumentTypes...>, std::tuple<MethodArgumentType, MethodArgumentTypes...>>
: MethodSignatureValidationImpl<std::tuple<MessageArgumentTypes..., MethodArgumentType>, std::tuple<MethodArgumentTypes...>> { };
template<typename... MessageArgumentTypes>
struct MethodSignatureValidationImpl<std::tuple<Connection&, MessageArgumentTypes...>, std::tuple<>>
: MethodSignatureValidationImpl<std::tuple<MessageArgumentTypes...>, std::tuple<>> {
static constexpr bool expectsConnectionArgument = true;
};
template<typename... MessageArgumentTypes>
struct MethodSignatureValidationImpl<std::tuple<MessageArgumentTypes...>, std::tuple<>> {
static constexpr bool expectsConnectionArgument = false;
using MessageArguments = std::tuple<std::remove_cvref_t<MessageArgumentTypes>...>;
};
template<typename... MessageArgumentTypes, typename... CompletionHandlerArgumentTypes>
struct MethodSignatureValidationImpl<std::tuple<MessageArgumentTypes...>, std::tuple<CompletionHandler<void(CompletionHandlerArgumentTypes...)>&&>>
: MethodSignatureValidationImpl<std::tuple<MessageArgumentTypes...>, std::tuple<>> {
using CompletionHandlerArguments = std::tuple<std::remove_cvref_t<CompletionHandlerArgumentTypes>...>;
using CompletionHandlerType = CompletionHandler<void(CompletionHandlerArgumentTypes...)>;
};
template<typename FunctionType> struct MethodSignatureValidation { };
template<typename R, typename... MethodArgumentTypes>
struct MethodSignatureValidation<R(MethodArgumentTypes...)>
: MethodSignatureValidationImpl<std::tuple<>, std::tuple<MethodArgumentTypes...>> { };
template<typename R, typename... MethodArgumentTypes>
struct MethodSignatureValidation<R(MethodArgumentTypes...) const>
: MethodSignatureValidation<R(MethodArgumentTypes...)> { };
// Main dispatch functions
template<typename MessageType, typename C, typename T, typename U, typename MF>
void handleMessage(C& connection, Decoder& decoder, T* object, MF U::* function)
{
using ValidationType = MethodSignatureValidation<MF>;
static_assert(std::is_same_v<typename ValidationType::MessageArguments, typename MessageType::Arguments>);
auto arguments = decoder.decode<typename MessageType::Arguments>();
if (UNLIKELY(!arguments))
return;
logMessage(connection, MessageType::name(), object, *arguments);
if constexpr (ValidationType::expectsConnectionArgument)
callMemberFunction(object, function, connection, WTFMove(*arguments));
else
callMemberFunction(object, function, WTFMove(*arguments));
}
template<typename MessageType, typename T, typename U, typename MF>
void handleMessageWithoutUsingIPCConnection(Decoder& decoder, T* object, MF U::* function)
{
using ValidationType = MethodSignatureValidation<MF>;
static_assert(std::is_same_v<typename ValidationType::MessageArguments, typename MessageType::Arguments>);
auto arguments = decoder.decode<typename MessageType::Arguments>();
if (UNLIKELY(!arguments))
return;
callMemberFunction(object, function, WTFMove(*arguments));
}
template<typename MessageType, typename T, typename U, typename MF>
bool handleMessageSynchronous(Connection& connection, Decoder& decoder, UniqueRef<Encoder>& replyEncoder, T* object, MF U::* function)
{
using ValidationType = MethodSignatureValidation<MF>;
static_assert(std::is_same_v<typename ValidationType::MessageArguments, typename MessageType::Arguments>);
auto arguments = decoder.decode<typename MessageType::Arguments>();
if (UNLIKELY(!arguments))
return true; // Message handler found, but decode failed.
static_assert(std::is_same_v<typename ValidationType::CompletionHandlerArguments, typename MessageType::ReplyArguments>);
using CompletionHandlerType = typename ValidationType::CompletionHandlerType;
CompletionHandlerType completionHandler(
[replyEncoder = WTFMove(replyEncoder), connection = Ref { connection }] (auto&&... args) mutable {
logReply(connection, MessageType::name(), args...);
(replyEncoder.get() << ... << std::forward<decltype(args)>(args));
connection->sendSyncReply(WTFMove(replyEncoder));
});
logMessage(connection, MessageType::name(), object, *arguments);
if constexpr (ValidationType::expectsConnectionArgument)
callMemberFunction(object, function, connection, WTFMove(*arguments), WTFMove(completionHandler));
else
callMemberFunction(object, function, WTFMove(*arguments), WTFMove(completionHandler));
return true;
}
template<typename MessageType, typename T, typename U, typename MF>
void handleMessageSynchronous(StreamServerConnection& connection, Decoder& decoder, T* object, MF U::* function)
{
using ValidationType = MethodSignatureValidation<MF>;
static_assert(std::is_same_v<typename ValidationType::MessageArguments, typename MessageType::Arguments>);
auto arguments = decoder.decode<typename MessageType::Arguments>();
if (UNLIKELY(!arguments))
return;
static_assert(std::is_same_v<typename ValidationType::CompletionHandlerArguments, typename MessageType::ReplyArguments>);
using CompletionHandlerType = typename ValidationType::CompletionHandlerType;
logMessage(connection, MessageType::name(), object, *arguments);
callMemberFunction(object, function, WTFMove(*arguments),
CompletionHandlerType([syncRequestID = decoder.syncRequestID(), connection = Ref { connection }] (auto&&... args) mutable {
logReply(connection, MessageType::name(), args...);
connection->sendSyncReply<MessageType>(syncRequestID, std::forward<decltype(args)>(args)...);
}));
}
template<typename MessageType, typename C, typename T, typename U, typename MF>
void handleMessageAsync(C& connection, Decoder& decoder, T* object, MF U::* function)
{
using ValidationType = MethodSignatureValidation<MF>;
static_assert(std::is_same_v<typename ValidationType::MessageArguments, typename MessageType::Arguments>);
auto arguments = decoder.decode<typename MessageType::Arguments>();
if (UNLIKELY(!arguments))
return;
auto replyID = decoder.decode<IPC::AsyncReplyID>();
if (UNLIKELY(!replyID))
return;
static_assert(std::is_same_v<typename ValidationType::CompletionHandlerArguments, typename MessageType::ReplyArguments>);
using CompletionHandlerType = typename ValidationType::CompletionHandlerType;
CompletionHandlerType completionHandler {
[replyID = *replyID, connection = Ref { connection }] (auto&&... args) mutable {
connection->template sendAsyncReply<MessageType>(replyID, std::forward<decltype(args)>(args)...);
}, MessageType::callbackThread };
logMessage(connection, MessageType::name(), object, *arguments);
if constexpr (ValidationType::expectsConnectionArgument)
callMemberFunction(object, function, connection, WTFMove(*arguments), WTFMove(completionHandler));
else
callMemberFunction(object, function, WTFMove(*arguments), WTFMove(completionHandler));
}
template<typename MessageType, typename T, typename U, typename MF>
void handleMessageAsyncWithoutUsingIPCConnection(Decoder& decoder, Function<void(UniqueRef<Encoder>&&)>&& replyHandler, T* object, MF U::* function)
{
using ValidationType = MethodSignatureValidation<MF>;
static_assert(std::is_same_v<typename ValidationType::MessageArguments, typename MessageType::Arguments>);
auto arguments = decoder.decode<typename MessageType::Arguments>();
if (UNLIKELY(!arguments))
return;
static_assert(std::is_same_v<typename ValidationType::CompletionHandlerArguments, typename MessageType::ReplyArguments>);
using CompletionHandlerType = typename ValidationType::CompletionHandlerType;
CompletionHandlerType completionHandler {
[destinationID = decoder.destinationID(), replyHandler = WTFMove(replyHandler), object = Ref { *object }] (auto&&... args) mutable {
auto encoder = makeUniqueRef<Encoder>(MessageType::asyncMessageReplyName(), destinationID);
(encoder.get() << ... << std::forward<decltype(args)>(args));
replyHandler(WTFMove(encoder));
}, MessageType::callbackThread };
callMemberFunction(object, function, WTFMove(*arguments), WTFMove(completionHandler));
}
template<typename MessageType, typename T, typename U, typename MF>
void handleMessageAsyncWithReplyID(Connection& connection, Decoder& decoder, T* object, MF U::* function)
{
using ValidationType = MethodSignatureValidation<MF>;
static_assert(std::is_same_v<typename ValidationType::MessageArguments, std::tuple<IPC::AsyncReplyID>>);
auto replyID = decoder.decode<Connection::AsyncReplyID>();
if (UNLIKELY(!replyID))
return;
logMessage(connection, MessageType::name(), object, std::tuple<>());
static_assert(!ValidationType::expectsConnectionArgument);
callMemberFunction(object, function, std::tuple<IPC::AsyncReplyID>(*replyID));
}
} // namespace IPC
|