sf: implement service framework enough for ro to work.

This completely re-does the whole interface for ipc servers.
This commit is contained in:
Michael Scire
2019-10-10 23:49:28 -07:00
committed by SciresM
parent bd341d5c00
commit f4dcd1db9b
47 changed files with 3545 additions and 166 deletions

View File

@@ -0,0 +1,108 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace sts::sf::cmif {
Result impl::ServiceDispatchTableBase::ProcessMessageImpl(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data, const ServiceCommandMeta *entries, const size_t entry_count) const {
/* Get versioning info. */
const auto hos_version = hos::GetVersion();
const u32 max_cmif_version = hos_version >= hos::Version_500 ? 1 : 0;
/* Parse the CMIF in header. */
const CmifInHeader *in_header = reinterpret_cast<const CmifInHeader *>(in_raw_data.GetPointer());
R_UNLESS(in_raw_data.GetSize() >= sizeof(*in_header), ResultServiceFrameworkInvalidCmifHeaderSize);
R_UNLESS(in_header->magic == CMIF_IN_HEADER_MAGIC && in_header->version <= max_cmif_version, ResultServiceFrameworkInvalidCmifInHeader);
const u32 cmd_id = in_header->command_id;
/* Find a handler. */
decltype(ServiceCommandMeta::handler) cmd_handler = nullptr;
for (size_t i = 0; i < entry_count; i++) {
if (entries[i].Matches(cmd_id, hos_version)) {
cmd_handler = entries[i].GetHandler();
break;
}
}
R_UNLESS(cmd_handler != nullptr, ResultServiceFrameworkUnknownCmifCommandId);
/* Invoke handler. */
CmifOutHeader *out_header = nullptr;
Result command_result = cmd_handler(&out_header, ctx, cmif::PointerAndSize(in_raw_data.GetAddress() + sizeof(*out_header), in_raw_data.GetSize() - sizeof(*out_header)));
/* Forward forwardable results, otherwise ensure we can send result to user. */
R_TRY_CATCH(command_result) {
R_CATCH(ResultServiceFrameworkRequestDeferredByUser) { return ResultServiceFrameworkRequestDeferredByUser; }
R_CATCH_ALL() { STS_ASSERT(out_header != nullptr); }
} R_END_TRY_CATCH;
/* Write output header to raw data. */
if (out_header != nullptr) {
*out_header = CmifOutHeader{CMIF_OUT_HEADER_MAGIC, 0, command_result, 0};
}
return ResultSuccess;
}
Result impl::ServiceDispatchTableBase::ProcessMessageForMitmImpl(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data, const ServiceCommandMeta *entries, const size_t entry_count) const {
/* Get versioning info. */
const auto hos_version = hos::GetVersion();
const u32 max_cmif_version = hos_version >= hos::Version_500 ? 1 : 0;
/* Parse the CMIF in header. */
const CmifInHeader *in_header = reinterpret_cast<const CmifInHeader *>(in_raw_data.GetPointer());
R_UNLESS(in_raw_data.GetSize() >= sizeof(*in_header), ResultServiceFrameworkInvalidCmifHeaderSize);
R_UNLESS(in_header->magic == CMIF_IN_HEADER_MAGIC && in_header->version <= max_cmif_version, ResultServiceFrameworkInvalidCmifInHeader);
const u32 cmd_id = in_header->command_id;
/* Find a handler. */
decltype(ServiceCommandMeta::handler) cmd_handler = nullptr;
for (size_t i = 0; i < entry_count; i++) {
if (entries[i].Matches(cmd_id, hos_version)) {
cmd_handler = entries[i].GetHandler();
break;
}
}
/* If we didn't find a handler, forward the request. */
if (cmd_handler == nullptr) {
/* TODO: FORWARD REQUEST */
STS_ASSERT(false);
}
/* Invoke handler. */
CmifOutHeader *out_header = nullptr;
Result command_result = cmd_handler(&out_header, ctx, cmif::PointerAndSize(in_raw_data.GetAddress() + sizeof(*out_header), in_raw_data.GetSize() - sizeof(*out_header)));
/* Forward forwardable results, otherwise ensure we can send result to user. */
R_TRY_CATCH(command_result) {
R_CATCH(ResultServiceFrameworkRequestDeferredByUser) { return ResultServiceFrameworkRequestDeferredByUser; }
R_CATCH(ResultAtmosphereMitmShouldForwardToSession) {
/* TODO: Restore TLS. */
/* TODO: FORWARD REQUEST */
STS_ASSERT(false);
}
R_CATCH_ALL() { STS_ASSERT(out_header != nullptr); }
} R_END_TRY_CATCH;
/* Write output header to raw data. */
if (out_header != nullptr) {
*out_header = CmifOutHeader{CMIF_OUT_HEADER_MAGIC, 0, command_result, 0};
}
return ResultSuccess;
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <functional>
#include <stratosphere.hpp>
namespace sts::sf::cmif {
Result ServiceObjectHolder::ProcessMessage(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data) const {
const auto ProcessHandler = this->dispatch_meta->ProcessHandler;
const auto *DispatchTable = this->dispatch_meta->DispatchTable;
return (DispatchTable->*ProcessHandler)(ctx, in_raw_data);
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace sts::sf::hipc {
namespace {
NX_INLINE Result ReceiveImpl(Handle session_handle, void *message_buf, size_t message_buf_size) {
s32 unused_index;
if (message_buf == armGetTls()) {
/* Consider: STS_ASSERT(message_buf_size == TlsMessageBufferSize); */
return svcReplyAndReceive(&unused_index, &session_handle, 1, INVALID_HANDLE, U64_MAX);
} else {
return svcReplyAndReceiveWithUserBuffer(&unused_index, message_buf, message_buf_size, &session_handle, 1, INVALID_HANDLE, U64_MAX);
}
}
NX_INLINE Result ReplyImpl(Handle session_handle, void *message_buf, size_t message_buf_size) {
s32 unused_index;
if (message_buf == armGetTls()) {
/* Consider: STS_ASSERT(message_buf_size == TlsMessageBufferSize); */
return svcReplyAndReceive(&unused_index, &session_handle, 0, session_handle, 0);
} else {
return svcReplyAndReceiveWithUserBuffer(&unused_index, message_buf, message_buf_size, &session_handle, 0, session_handle, 0);
}
}
}
Result Receive(ReceiveResult *out_recv_result, Handle session_handle, const cmif::PointerAndSize &message_buffer) {
R_TRY_CATCH(ReceiveImpl(session_handle, message_buffer.GetPointer(), message_buffer.GetSize())) {
R_CATCH(ResultKernelConnectionClosed) {
*out_recv_result = ReceiveResult::Closed;
return ResultSuccess;
}
R_CATCH(ResultKernelReceiveListBroken) {
*out_recv_result = ReceiveResult::NeedsRetry;
return ResultSuccess;
}
} R_END_TRY_CATCH;
*out_recv_result = ReceiveResult::Success;
return ResultSuccess;
}
Result Receive(bool *out_closed, Handle session_handle, const cmif::PointerAndSize &message_buffer) {
R_TRY_CATCH(ReceiveImpl(session_handle, message_buffer.GetPointer(), message_buffer.GetSize())) {
R_CATCH(ResultKernelConnectionClosed) {
*out_closed = true;
return ResultSuccess;
}
} R_END_TRY_CATCH;
*out_closed = false;
return ResultSuccess;
}
Result Reply(Handle session_handle, const cmif::PointerAndSize &message_buffer) {
R_TRY_CATCH(ReplyImpl(session_handle, message_buffer.GetPointer(), message_buffer.GetSize())) {
R_CATCH(ResultKernelTimedOut) { return ResultSuccess; }
R_CATCH(ResultKernelConnectionClosed) { return ResultSuccess; }
} R_END_TRY_CATCH;
/* ReplyImpl should *always* return an error. */
STS_ASSERT(false);
}
Result CreateSession(Handle *out_server_handle, Handle *out_client_handle) {
R_TRY_CATCH(svcCreateSession(out_server_handle, out_client_handle, 0, 0)) {
R_CATCH(ResultKernelResourceExhausted) { return ResultHipcOutOfSessions; }
} R_END_TRY_CATCH;
return ResultSuccess;
}
}

View File

@@ -0,0 +1,165 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace sts::sf::hipc {
ServerManagerBase::ServerBase::~ServerBase() { /* Pure virtual destructor, to prevent linker errors. */ }
void ServerManagerBase::RegisterSessionToWaitList(ServerSession *session) {
session->has_received = false;
/* Set user data tag. */
session->SetUserData(static_cast<uintptr_t>(UserDataTag::Session));
this->RegisterToWaitList(session);
}
void ServerManagerBase::RegisterToWaitList(os::WaitableHolder *holder) {
std::scoped_lock lk(this->waitlist_mutex);
this->waitlist.LinkWaitableHolder(holder);
this->notify_event.Signal();
}
void ServerManagerBase::ProcessWaitList() {
std::scoped_lock lk(this->waitlist_mutex);
this->waitable_manager.MoveAllFrom(&this->waitlist);
}
os::WaitableHolder *ServerManagerBase::WaitSignaled() {
std::scoped_lock lk(this->waitable_selection_mutex);
while (true) {
this->ProcessWaitList();
auto selected = this->waitable_manager.WaitAny();
if (selected == &this->request_stop_event_holder) {
return nullptr;
} else if (selected == &this->notify_event_holder) {
this->notify_event.Reset();
} else {
selected->UnlinkFromWaitableManager();
return selected;
}
}
}
void ServerManagerBase::ResumeProcessing() {
this->request_stop_event.Reset();
}
void ServerManagerBase::RequestStopProcessing() {
this->request_stop_event.Signal();
}
void ServerManagerBase::AddUserWaitableHolder(os::WaitableHolder *waitable) {
const auto user_data_tag = static_cast<UserDataTag>(waitable->GetUserData());
STS_ASSERT(user_data_tag != UserDataTag::Server);
STS_ASSERT(user_data_tag != UserDataTag::MitmServer);
STS_ASSERT(user_data_tag != UserDataTag::Session);
this->RegisterToWaitList(waitable);
}
Result ServerManagerBase::ProcessForServer(os::WaitableHolder *holder) {
STS_ASSERT(static_cast<UserDataTag>(holder->GetUserData()) == UserDataTag::Server);
ServerBase *server = static_cast<ServerBase *>(holder);
ON_SCOPE_EXIT { this->RegisterToWaitList(server); };
/* Create resources for new session. */
cmif::ServiceObjectHolder obj;
std::shared_ptr<::Service> fsrv;
server->CreateSessionObjectHolder(&obj, &fsrv);
/* Not a mitm server, so we must have no forward service. */
STS_ASSERT(fsrv == nullptr);
/* Try to accept. */
return this->AcceptSession(server->port_handle, std::move(obj));
}
Result ServerManagerBase::ProcessForMitmServer(os::WaitableHolder *holder) {
STS_ASSERT(static_cast<UserDataTag>(holder->GetUserData()) == UserDataTag::MitmServer);
ServerBase *server = static_cast<ServerBase *>(holder);
ON_SCOPE_EXIT { this->RegisterToWaitList(server); };
/* Create resources for new session. */
cmif::ServiceObjectHolder obj;
std::shared_ptr<::Service> fsrv;
server->CreateSessionObjectHolder(&obj, &fsrv);
/* Mitm server, so we must have forward service. */
STS_ASSERT(fsrv != nullptr);
/* Try to accept. */
return this->AcceptMitmSession(server->port_handle, std::move(obj), std::move(fsrv));
}
Result ServerManagerBase::ProcessForSession(os::WaitableHolder *holder) {
STS_ASSERT(static_cast<UserDataTag>(holder->GetUserData()) == UserDataTag::Session);
ServerSession *session = static_cast<ServerSession *>(holder);
cmif::PointerAndSize tls_message(armGetTls(), hipc::TlsMessageBufferSize);
const cmif::PointerAndSize &saved_message = session->saved_message;
STS_ASSERT(tls_message.GetSize() == saved_message.GetSize());
if (!session->has_received) {
R_TRY(this->ReceiveRequest(session, tls_message));
session->has_received = true;
std::memcpy(saved_message.GetPointer(), tls_message.GetPointer(), tls_message.GetSize());
} else {
/* We were deferred and are re-receiving, so just memcpy. */
std::memcpy(tls_message.GetPointer(), saved_message.GetPointer(), tls_message.GetSize());
}
return this->ProcessRequest(session, tls_message);
}
Result ServerManagerBase::Process(os::WaitableHolder *holder) {
switch (static_cast<UserDataTag>(holder->GetUserData())) {
case UserDataTag::Server:
return this->ProcessForServer(holder);
break;
case UserDataTag::MitmServer:
return this->ProcessForMitmServer(holder);
break;
case UserDataTag::Session:
/* TODO: Process deferred. */
return this->ProcessForSession(holder);
break;
STS_UNREACHABLE_DEFAULT_CASE();
}
}
bool ServerManagerBase::WaitAndProcessImpl() {
auto waitable = this->WaitSignaled();
if (!waitable) {
return false;
}
R_ASSERT(this->Process(waitable));
return true;
}
void ServerManagerBase::WaitAndProcess() {
this->WaitAndProcessImpl();
}
void ServerManagerBase::LoopProcess() {
while (this->WaitAndProcessImpl()) {
/* ... */
}
}
}

View File

@@ -0,0 +1,246 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace sts::sf::hipc {
void ServerSessionManager::DestroySession(ServerSession *session) {
/* Destroy object. */
session->~ServerSession();
/* Free object memory. */
this->FreeSession(session);
}
void ServerSessionManager::CloseSessionImpl(ServerSession *session) {
const Handle session_handle = session->session_handle;
this->DestroySession(session);
R_ASSERT(svcCloseHandle(session_handle));
}
Result ServerSessionManager::RegisterSessionImpl(ServerSession *session_memory, Handle session_handle, cmif::ServiceObjectHolder &&obj) {
/* Create session object. */
new (session_memory) ServerSession(session_handle, std::forward<cmif::ServiceObjectHolder>(obj));
/* Assign session resources. */
session_memory->pointer_buffer = this->GetSessionPointerBuffer(session_memory);
session_memory->saved_message = this->GetSessionSavedMessageBuffer(session_memory);
/* Register to wait list. */
this->RegisterSessionToWaitList(session_memory);
return ResultSuccess;
}
Result ServerSessionManager::AcceptSessionImpl(ServerSession *session_memory, Handle port_handle, cmif::ServiceObjectHolder &&obj) {
/* Create session handle. */
Handle session_handle;
R_TRY(svcAcceptSession(&session_handle, port_handle));
bool succeeded = false;
ON_SCOPE_EXIT {
if (!succeeded) {
R_ASSERT(svcCloseHandle(session_handle));
}
};
/* Register session. */
R_TRY(this->RegisterSessionImpl(session_memory, session_handle, std::forward<cmif::ServiceObjectHolder>(obj)));
succeeded = true;
return ResultSuccess;
}
Result ServerSessionManager::RegisterMitmSessionImpl(ServerSession *session_memory, Handle mitm_session_handle, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) {
/* Create session object. */
new (session_memory) ServerSession(mitm_session_handle, std::forward<cmif::ServiceObjectHolder>(obj), std::forward<std::shared_ptr<::Service>>(fsrv));
/* Assign session resources. */
session_memory->pointer_buffer = this->GetSessionPointerBuffer(session_memory);
session_memory->saved_message = this->GetSessionSavedMessageBuffer(session_memory);
/* Register to wait list. */
this->RegisterSessionToWaitList(session_memory);
return ResultSuccess;
}
Result ServerSessionManager::AcceptMitmSessionImpl(ServerSession *session_memory, Handle mitm_port_handle, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) {
/* Create session handle. */
Handle mitm_session_handle;
R_TRY(svcAcceptSession(&mitm_session_handle, mitm_port_handle));
bool succeeded = false;
ON_SCOPE_EXIT {
if (!succeeded) {
R_ASSERT(svcCloseHandle(mitm_session_handle));
}
};
/* Register session. */
R_TRY(this->RegisterMitmSessionImpl(session_memory, mitm_session_handle, std::forward<cmif::ServiceObjectHolder>(obj), std::forward<std::shared_ptr<::Service>>(fsrv)));
succeeded = true;
return ResultSuccess;
}
Result ServerSessionManager::RegisterSession(Handle session_handle, cmif::ServiceObjectHolder &&obj) {
/* We don't actually care about what happens to the session. It'll get linked. */
ServerSession *session_ptr = nullptr;
return this->RegisterSession(&session_ptr, session_handle,std::forward<cmif::ServiceObjectHolder>(obj));
}
Result ServerSessionManager::AcceptSession(Handle port_handle, cmif::ServiceObjectHolder &&obj) {
/* We don't actually care about what happens to the session. It'll get linked. */
ServerSession *session_ptr = nullptr;
return this->AcceptSession(&session_ptr, port_handle, std::forward<cmif::ServiceObjectHolder>(obj));
}
Result ServerSessionManager::RegisterMitmSession(Handle mitm_session_handle, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) {
/* We don't actually care about what happens to the session. It'll get linked. */
ServerSession *session_ptr = nullptr;
return this->RegisterMitmSession(&session_ptr, mitm_session_handle, std::forward<cmif::ServiceObjectHolder>(obj), std::forward<std::shared_ptr<::Service>>(fsrv));
}
Result ServerSessionManager::AcceptMitmSession(Handle mitm_port_handle, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) {
/* We don't actually care about what happens to the session. It'll get linked. */
ServerSession *session_ptr = nullptr;
return this->AcceptMitmSession(&session_ptr, mitm_port_handle, std::forward<cmif::ServiceObjectHolder>(obj), std::forward<std::shared_ptr<::Service>>(fsrv));
}
Result ServerSessionManager::ReceiveRequestImpl(ServerSession *session, const cmif::PointerAndSize &message) {
const cmif::PointerAndSize &pointer_buffer = session->pointer_buffer;
/* If the receive list is odd, we may need to receive repeatedly. */
while (true) {
if (pointer_buffer.GetPointer()) {
hipcMakeRequestInline(message.GetPointer(),
.type = CmifCommandType_Invalid,
.num_recv_statics = HIPC_AUTO_RECV_STATIC,
).recv_list[0] = hipcMakeRecvStatic(pointer_buffer.GetPointer(), pointer_buffer.GetSize());
} else {
hipcMakeRequestInline(message.GetPointer(),
.type = CmifCommandType_Invalid,
);
}
hipc::ReceiveResult recv_result;
R_TRY(hipc::Receive(&recv_result, session->session_handle, message));
switch (recv_result) {
case hipc::ReceiveResult::Success:
session->is_closed = false;
return ResultSuccess;
case hipc::ReceiveResult::Closed:
session->is_closed = true;
return ResultSuccess;
case hipc::ReceiveResult::NeedsRetry:
continue;
STS_UNREACHABLE_DEFAULT_CASE();
}
}
}
namespace {
NX_CONSTEXPR u32 GetCmifCommandType(const cmif::PointerAndSize &message) {
HipcHeader hdr = {};
__builtin_memcpy(&hdr, message.GetPointer(), sizeof(hdr));
return hdr.type;
}
}
Result ServerSessionManager::ProcessRequest(ServerSession *session, const cmif::PointerAndSize &message) {
if (session->is_closed) {
this->CloseSessionImpl(session);
return ResultSuccess;
}
switch (GetCmifCommandType(message)) {
case CmifCommandType_Close:
{
this->CloseSessionImpl(session);
return ResultSuccess;
}
default:
{
R_TRY_CATCH(this->ProcessRequestImpl(session, message, message)) {
R_CATCH(ResultServiceFrameworkRequestDeferredByUser) { /* TODO: Properly include entire range Nintendo does */
return ResultServiceFrameworkRequestDeferredByUser;
}
R_CATCH_ALL() {
/* All other results indicate something went very wrong. */
this->CloseSessionImpl(session);
return ResultSuccess;
}
} R_END_TRY_CATCH;
/* We succeeded, so we can process future messages on this session. */
this->RegisterSessionToWaitList(session);
return ResultSuccess;
}
}
}
Result ServerSessionManager::ProcessRequestImpl(ServerSession *session, const cmif::PointerAndSize &in_message, const cmif::PointerAndSize &out_message) {
/* TODO: Inline context support, retrieve from raw data + 0xC. */
const auto cmif_command_type = GetCmifCommandType(in_message);
switch (cmif_command_type) {
case CmifCommandType_Request:
case CmifCommandType_RequestWithContext:
return this->DispatchRequest(session->srv_obj_holder.Clone(), session, in_message, out_message);
case CmifCommandType_Control:
case CmifCommandType_ControlWithContext:
return this->DispatchManagerRequest(session, in_message, out_message);
default:
return ResultHipcUnknownCommandType;
}
}
Result ServerSessionManager::DispatchManagerRequest(ServerSession *session, const cmif::PointerAndSize &in_message, const cmif::PointerAndSize &out_message) {
/* This will get overridden by ... WithDomain class. */
return ResultServiceFrameworkNotSupported;
}
Result ServerSessionManager::DispatchRequest(cmif::ServiceObjectHolder &&obj_holder, ServerSession *session, const cmif::PointerAndSize &in_message, const cmif::PointerAndSize &out_message) {
/* Create request context. */
cmif::HandlesToClose handles_to_close = {};
cmif::ServiceDispatchContext dispatch_ctx = {
.srv_obj = obj_holder.GetServiceObjectUnsafe(),
.manager = this,
.processor = nullptr, /* Filled in by template implementations. */
.handles_to_close = &handles_to_close,
.pointer_buffer = session->pointer_buffer,
.in_message_buffer = in_message,
.out_message_buffer = out_message,
.request = hipcParseRequest(in_message.GetPointer()),
};
/* Validate message sizes. */
const uintptr_t in_message_buffer_end = in_message.GetAddress() + in_message.GetSize();
const uintptr_t in_raw_addr = reinterpret_cast<uintptr_t>(dispatch_ctx.request.data.data_words);
const size_t in_raw_size = dispatch_ctx.request.meta.num_data_words * sizeof(u32);
/* Note: Nintendo does not validate this size before subtracting 0x10 from it. This is not exploitable. */
R_UNLESS(in_raw_size >= 0x10, ResultHipcInvalidRequestSize);
R_UNLESS(in_raw_addr + in_raw_size <= in_message_buffer_end, ResultHipcInvalidRequestSize);
const uintptr_t recv_list_end = reinterpret_cast<uintptr_t>(dispatch_ctx.request.data.recv_list + dispatch_ctx.request.meta.num_recv_statics);
R_UNLESS(recv_list_end <= in_message_buffer_end, ResultHipcInvalidRequestSize);
/* CMIF has 0x10 of padding in raw data, and requires 0x10 alignment. */
const cmif::PointerAndSize in_raw_data(util::AlignUp(in_raw_addr, 0x10), in_raw_size - 0x10);
/* Invoke command handler. */
R_TRY(obj_holder.ProcessMessage(dispatch_ctx, in_raw_data));
/* Reply. */
{
ON_SCOPE_EXIT {
for (size_t i = 0; i < handles_to_close.num_handles; i++) {
R_ASSERT(svcCloseHandle(handles_to_close.handles[i]));
}
};
R_TRY(hipc::Reply(session->session_handle, out_message));
}
return ResultSuccess;
}
}