From 78b1bc3b619dd441d10f1131bca7ccf260ed4e80 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 17 Sep 2020 10:43:54 -0400 Subject: [PATCH 1/2] service/sm: Eliminate dependency on the global system instance --- src/core/core.cpp | 2 +- src/core/hle/service/sm/sm.cpp | 7 +++---- src/core/hle/service/sm/sm.h | 7 +++++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 44aaba2424..81e8cc338a 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -178,7 +178,7 @@ struct System::Impl { arp_manager.ResetAll(); telemetry_session = std::make_unique(); - service_manager = std::make_shared(); + service_manager = std::make_shared(kernel); Service::Init(service_manager, system); GDBStub::DeferStart(); diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 586b3d8ebb..2aedc93eab 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -19,7 +19,7 @@ constexpr ResultCode ERR_ALREADY_REGISTERED(ErrorModule::SM, 4); constexpr ResultCode ERR_INVALID_NAME(ErrorModule::SM, 6); constexpr ResultCode ERR_SERVICE_NOT_REGISTERED(ErrorModule::SM, 7); -ServiceManager::ServiceManager() = default; +ServiceManager::ServiceManager(Kernel::KernelCore& kernel_) : kernel{kernel_} {} ServiceManager::~ServiceManager() = default; void ServiceManager::InvokeControlRequest(Kernel::HLERequestContext& context) { @@ -48,8 +48,8 @@ void ServiceManager::InstallInterfaces(std::shared_ptr self, self->controller_interface = std::make_unique(); } -ResultVal> ServiceManager::RegisterService( - std::string name, unsigned int max_sessions) { +ResultVal> ServiceManager::RegisterService(std::string name, + u32 max_sessions) { CASCADE_CODE(ValidateServiceName(name)); @@ -58,7 +58,6 @@ ResultVal> ServiceManager::RegisterService( return ERR_ALREADY_REGISTERED; } - auto& kernel = Core::System::GetInstance().Kernel(); auto [server_port, client_port] = Kernel::ServerPort::CreatePortPair(kernel, max_sessions, name); diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index aabf166b74..6790c86f07 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h @@ -48,11 +48,11 @@ class ServiceManager { public: static void InstallInterfaces(std::shared_ptr self, Kernel::KernelCore& kernel); - ServiceManager(); + explicit ServiceManager(Kernel::KernelCore& kernel_); ~ServiceManager(); ResultVal> RegisterService(std::string name, - unsigned int max_sessions); + u32 max_sessions); ResultCode UnregisterService(const std::string& name); ResultVal> GetServicePort(const std::string& name); ResultVal> ConnectToService(const std::string& name); @@ -79,6 +79,9 @@ private: /// Map of registered services, retrieved using GetServicePort or ConnectToService. std::unordered_map> registered_services; + + /// Kernel context + Kernel::KernelCore& kernel; }; } // namespace Service::SM From 057aa6275d4bfcdf043d0181d5284ca2023faaf2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 17 Sep 2020 10:54:09 -0400 Subject: [PATCH 2/2] service/sm: Slightly more efficient string name validation We can check the end of the string first for null-termination, rather than the beginning of the string. --- src/core/hle/service/sm/sm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 2aedc93eab..9c1da361ba 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -27,11 +27,11 @@ void ServiceManager::InvokeControlRequest(Kernel::HLERequestContext& context) { } static ResultCode ValidateServiceName(const std::string& name) { - if (name.size() <= 0 || name.size() > 8) { + if (name.empty() || name.size() > 8) { LOG_ERROR(Service_SM, "Invalid service name! service={}", name); return ERR_INVALID_NAME; } - if (name.find('\0') != std::string::npos) { + if (name.rfind('\0') != std::string::npos) { LOG_ERROR(Service_SM, "A non null terminated service was passed"); return ERR_INVALID_NAME; }