From 21436f5ef7b833fd29871a815aa4e69197f4f730 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Fri, 9 Jun 2017 00:51:18 -0700 Subject: [PATCH] IPC: Add Pop/PushObjects methods to RequestParser/Builder These use the context functions to create and look-up handles for the user. --- src/core/hle/ipc_helpers.h | 113 +++++++++++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 10 deletions(-) diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index a3abc102e..5f370bc3b 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -4,6 +4,10 @@ #pragma once +#include +#include +#include +#include #include "core/hle/ipc.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/hle_ipc.h" @@ -105,6 +109,9 @@ public: template void PushMoveHandles(H... handles); + template + void PushObjects(Kernel::SharedPtr... pointers); + void PushCurrentPIDHandle(); void PushStaticBuffer(VAddr buffer_vaddr, u32 size, u8 buffer_id); @@ -170,6 +177,11 @@ inline void RequestBuilder::PushMoveHandles(H... handles) { Push(static_cast(handles)...); } +template +inline void RequestBuilder::PushObjects(Kernel::SharedPtr... pointers) { + PushMoveHandles(context->AddOutgoingHandle(std::move(pointers))...); +} + inline void RequestBuilder::PushCurrentPIDHandle() { Push(CallingPidDesc()); Push(u32(0)); @@ -229,10 +241,52 @@ public: template void Pop(First& first_value, Other&... other_values); + /// Equivalent to calling `PopHandles<1>()[0]`. Kernel::Handle PopHandle(); + /** + * Pops a descriptor containing `N` handles. The handles are returned as an array. The + * descriptor must contain exactly `N` handles, it is not permitted to, for example, call + * PopHandles<1>() twice to read a multi-handle descriptor with 2 handles, or to make a single + * PopHandles<2>() call to read 2 single-handle descriptors. + */ + template + std::array PopHandles(); + + /// Convenience wrapper around PopHandles() which assigns the handles to the passed references. template - void PopHandles(H&... handles); + void PopHandles(H&... handles) { + std::tie(handles...) = PopHandles(); + } + + /// Equivalent to calling `PopGenericObjects<1>()[0]`. + Kernel::SharedPtr PopGenericObject(); + + /// Equivalent to calling `std::get<0>(PopObjects())`. + template + Kernel::SharedPtr PopObject(); + + /** + * Pop a descriptor containing `N` handles and resolves them to Kernel::Object pointers. If a + * handle is invalid, null is returned for that object instead. The same caveats from + * PopHandles() apply regarding `N` matching the number of handles in the descriptor. + */ + template + std::array, N> PopGenericObjects(); + + /** + * Resolves handles to Kernel::Objects as in PopGenericsObjects(), but then also casts them to + * the passed `T` types, while verifying that the cast is valid. If the type of an object does + * not match, null is returned instead. + */ + template + std::tuple...> PopObjects(); + + /// Convenience wrapper around PopObjects() which assigns the handles to the passed references. + template + void PopObjects(Kernel::SharedPtr&... pointers) { + std::tie(pointers...) = PopObjects(); + } /** * @brief Pops the static buffer vaddr @@ -344,15 +398,54 @@ inline Kernel::Handle RequestParser::PopHandle() { return Pop(); } -template -void RequestParser::PopHandles(H&... handles) { - const u32 handle_descriptor = Pop(); - const int handles_number = sizeof...(H); - DEBUG_ASSERT_MSG(IsHandleDescriptor(handle_descriptor), - "Tried to pop handle(s) but the descriptor is not a handle descriptor"); - DEBUG_ASSERT_MSG(handles_number == HandleNumberFromDesc(handle_descriptor), - "Number of handles doesn't match the descriptor"); - Pop(static_cast(handles)...); +template +std::array RequestParser::PopHandles() { + u32 handle_descriptor = Pop(); + ASSERT_MSG(IsHandleDescriptor(handle_descriptor), + "Tried to pop handle(s) but the descriptor is not a handle descriptor"); + ASSERT_MSG(N == HandleNumberFromDesc(handle_descriptor), + "Number of handles doesn't match the descriptor"); + + std::array handles{}; + for (Kernel::Handle& handle : handles) { + handle = Pop(); + } + return handles; +} + +inline Kernel::SharedPtr RequestParser::PopGenericObject() { + Kernel::Handle handle = PopHandle(); + return context->GetIncomingHandle(handle); +} + +template +Kernel::SharedPtr RequestParser::PopObject() { + return Kernel::DynamicObjectCast(PopGenericObject()); +} + +template +inline std::array, N> RequestParser::PopGenericObjects() { + std::array handles = PopHandles(); + std::array, N> pointers; + for (int i = 0; i < N; ++i) { + pointers[i] = context->GetIncomingHandle(handles[i]); + } + return pointers; +} + +namespace detail { +template +std::tuple...> PopObjectsHelper( + std::array, sizeof...(T)>&& pointers, + std::index_sequence) { + return std::make_tuple(Kernel::DynamicObjectCast(std::move(pointers[I]))...); +} +} // namespace detail + +template +inline std::tuple...> RequestParser::PopObjects() { + return detail::PopObjectsHelper(PopGenericObjects(), + std::index_sequence_for{}); } inline VAddr RequestParser::PopStaticBuffer(size_t* data_size, bool useStaticBuffersToGetVaddr) {