From 2c5ef434f078c3cc21fcd1c2410f59e82f75e107 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 20 Nov 2019 22:03:22 -0800 Subject: [PATCH] sf: fixes (basic mitm service functionality now confirmed working) --- .../atmosphere/util/util_typed_storage.hpp | 2 +- .../sf/hipc/sf_hipc_server_manager.hpp | 19 +++++++++---------- .../stratosphere/sf/sf_service_object.hpp | 3 +++ .../source/sf/hipc/sf_hipc_mitm_query_api.cpp | 15 +++++++++++---- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/stratosphere/libstratosphere/include/atmosphere/util/util_typed_storage.hpp b/stratosphere/libstratosphere/include/atmosphere/util/util_typed_storage.hpp index ffcd564a7..62d65b2de 100644 --- a/stratosphere/libstratosphere/include/atmosphere/util/util_typed_storage.hpp +++ b/stratosphere/libstratosphere/include/atmosphere/util/util_typed_storage.hpp @@ -46,4 +46,4 @@ namespace ams::util { return *GetPointer(ts); } -} \ No newline at end of file +} diff --git a/stratosphere/libstratosphere/include/stratosphere/sf/hipc/sf_hipc_server_manager.hpp b/stratosphere/libstratosphere/include/stratosphere/sf/hipc/sf_hipc_server_manager.hpp index 9b83a01fe..6bdfd672d 100644 --- a/stratosphere/libstratosphere/include/stratosphere/sf/hipc/sf_hipc_server_manager.hpp +++ b/stratosphere/libstratosphere/include/stratosphere/sf/hipc/sf_hipc_server_manager.hpp @@ -18,12 +18,6 @@ #include "sf_hipc_server_domain_session_manager.hpp" #include "../../sm.hpp" -namespace ams::ncm { - - struct ProgramId; - -} - namespace ams::sf::hipc { struct DefaultServerManagerOptions { @@ -116,7 +110,7 @@ namespace ams::sf::hipc { ncm::ProgramId client_program_id; R_ASSERT(sm::mitm::AcknowledgeSession(forward_service.get(), &client_process_id, &client_program_id, this->service_name)); - *out_obj = std::move(cmif::ServiceObjectHolder(std::move(MakeShared(forward_service)))); + *out_obj = std::move(cmif::ServiceObjectHolder(std::move(MakeShared(std::shared_ptr<::Service>(forward_service), client_process_id, client_program_id)))); *out_fsrv = std::move(forward_service); } else { *out_obj = std::move(cmif::ServiceObjectHolder(std::move(MakeShared()))); @@ -171,6 +165,11 @@ namespace ams::sf::hipc { this->waitable_manager.LinkWaitableHolder(server); } + template + static constexpr inline std::shared_ptr MakeSharedMitm(std::shared_ptr<::Service> &&s, os::ProcessId p, ncm::ProgramId r) { + return std::make_shared(std::forward>(s), p, r); + } + Result InstallMitmServerImpl(Handle *out_port_handle, sm::ServiceName service_name, MitmQueryFunction query_func); protected: virtual ServerBase *AllocateServer() = 0; @@ -214,13 +213,13 @@ namespace ams::sf::hipc { return ResultSuccess(); } - template> - Result RegisterMitmServer(sm::ServiceName service_name, size_t max_sessions) { + template> + Result RegisterMitmServer(sm::ServiceName service_name) { static_assert(ServiceObjectTraits::IsMitmServiceObject, "RegisterMitmServer requires mitm object. Use RegisterServer instead."); /* Install mitm service. */ Handle port_handle; - R_TRY(this->InstallMitmServerImpl(&port_handle, service_name, max_sessions, &ServiceImpl::ShouldMitm)); + R_TRY(this->InstallMitmServerImpl(&port_handle, service_name, &ServiceImpl::ShouldMitm)); this->RegisterServerImpl(port_handle, service_name, true, cmif::ServiceObjectHolder()); return ResultSuccess(); diff --git a/stratosphere/libstratosphere/include/stratosphere/sf/sf_service_object.hpp b/stratosphere/libstratosphere/include/stratosphere/sf/sf_service_object.hpp index 38c730eff..8cf45caa5 100644 --- a/stratosphere/libstratosphere/include/stratosphere/sf/sf_service_object.hpp +++ b/stratosphere/libstratosphere/include/stratosphere/sf/sf_service_object.hpp @@ -33,6 +33,9 @@ namespace ams::sf { static bool ShouldMitm(os::ProcessId process_id, ncm::ProgramId program_id); }; + /* Utility. */ + #define SF_MITM_SERVICE_OBJECT_CTOR(cls) cls(std::shared_ptr<::Service> &&s, os::ProcessId p, ncm::ProgramId r) : ::ams::sf::IMitmServiceObject(std::forward>(s), p, r) + template struct ServiceObjectTraits { static_assert(std::is_base_of::value, "ServiceObjectTraits requires ServiceObject"); diff --git a/stratosphere/libstratosphere/source/sf/hipc/sf_hipc_mitm_query_api.cpp b/stratosphere/libstratosphere/source/sf/hipc/sf_hipc_mitm_query_api.cpp index f3c148901..abf747175 100644 --- a/stratosphere/libstratosphere/source/sf/hipc/sf_hipc_mitm_query_api.cpp +++ b/stratosphere/libstratosphere/source/sf/hipc/sf_hipc_mitm_query_api.cpp @@ -41,6 +41,7 @@ namespace ams::sf::hipc::impl { /* Globals. */ os::Mutex g_query_server_lock; + bool g_constructed_server = false; bool g_registered_any = false; void QueryServerProcessThreadMain(void *query_server) { @@ -51,18 +52,24 @@ namespace ams::sf::hipc::impl { constexpr int QueryServerProcessThreadPriority = 27; os::StaticThread g_query_server_process_thread; + constexpr size_t MaxServers = 0; + TYPED_STORAGE(sf::hipc::ServerManager) g_query_server_storage; + } void RegisterMitmQueryHandle(Handle query_handle, ServerManagerBase::MitmQueryFunction query_func) { std::scoped_lock lk(g_query_server_lock); - constexpr size_t MaxServers = 0; - sf::hipc::ServerManager s_query_server; - R_ASSERT(s_query_server.RegisterSession(query_handle, cmif::ServiceObjectHolder(std::make_shared(query_func)))); + if (!g_constructed_server) { + new (GetPointer(g_query_server_storage)) sf::hipc::ServerManager(); + g_constructed_server = true; + } + + R_ASSERT(GetPointer(g_query_server_storage)->RegisterSession(query_handle, cmif::ServiceObjectHolder(std::make_shared(query_func)))); if (!g_registered_any) { - R_ASSERT(g_query_server_process_thread.Initialize(&QueryServerProcessThreadMain, &s_query_server, QueryServerProcessThreadPriority)); + R_ASSERT(g_query_server_process_thread.Initialize(&QueryServerProcessThreadMain, GetPointer(g_query_server_storage), QueryServerProcessThreadPriority)); R_ASSERT(g_query_server_process_thread.Start()); g_registered_any = true; }