From b2971b48ed8f86857878f0fc0e1a744c4bc9d766 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 9 Jun 2021 21:37:11 -0700 Subject: hle: kernel: KServerSession: Fix client disconnected. - Prevents a cloned session's handler from being overwritten by another disconnected session. - Fixes session handler nullptr asserts with Pokemon Sword & Shield. --- src/core/hle/kernel/hle_ipc.cpp | 4 ++-- src/core/hle/kernel/k_server_session.h | 11 +++++------ src/core/hle/service/service.h | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) (limited to 'src/core') diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 45aced99f..28ed6265a 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -57,11 +57,11 @@ bool SessionRequestManager::HasSessionRequestHandler(const HLERequestContext& co } void SessionRequestHandler::ClientConnected(KServerSession* session) { - session->SetSessionHandler(shared_from_this()); + session->ClientConnected(shared_from_this()); } void SessionRequestHandler::ClientDisconnected(KServerSession* session) { - session->SetSessionHandler(nullptr); + session->ClientDisconnected(); } HLERequestContext::HLERequestContext(KernelCore& kernel_, Core::Memory::Memory& memory_, diff --git a/src/core/hle/kernel/k_server_session.h b/src/core/hle/kernel/k_server_session.h index 9efd400bc..d44bc9d4f 100644 --- a/src/core/hle/kernel/k_server_session.h +++ b/src/core/hle/kernel/k_server_session.h @@ -62,15 +62,14 @@ public: void OnClientClosed(); - /** - * Sets the HLE handler for the session. This handler will be called to service IPC requests - * instead of the regular IPC machinery. (The regular IPC machinery is currently not - * implemented.) - */ - void SetSessionHandler(SessionRequestHandlerPtr handler) { + void ClientConnected(SessionRequestHandlerPtr handler) { manager->SetSessionHandler(std::move(handler)); } + void ClientDisconnected() { + manager = nullptr; + } + /** * Handle a sync request from the emulated application. * diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h index 7f133a7cb..78afd7c73 100644 --- a/src/core/hle/service/service.h +++ b/src/core/hle/service/service.h @@ -42,7 +42,7 @@ class ServiceManager; static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters) /// Arbitrary default number of maximum connections to an HLE service. -static const u32 DefaultMaxSessions = 0x10000; +static const u32 DefaultMaxSessions = 0x100; /** * This is an non-templated base of ServiceFramework to reduce code bloat and compilation times, it -- cgit v1.2.3 From 2aa6a8d889e91cf999a9e95d18d06f455c337288 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 9 Jun 2021 22:04:36 -0700 Subject: hle: service: Use correct size for ServerSessionCountMax. --- src/core/hle/service/service.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/core') diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h index 78afd7c73..e078ac176 100644 --- a/src/core/hle/service/service.h +++ b/src/core/hle/service/service.h @@ -40,9 +40,11 @@ namespace SM { class ServiceManager; } -static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters) -/// Arbitrary default number of maximum connections to an HLE service. -static const u32 DefaultMaxSessions = 0x100; +/// Default number of maximum connections to a server session. +static constexpr u32 ServerSessionCountMax = 0x40; +static_assert(ServerSessionCountMax == 0x40, + "ServerSessionCountMax isn't 0x40 somehow, this assert is a reminder that this will " + "break lots of things"); /** * This is an non-templated base of ServiceFramework to reduce code bloat and compilation times, it @@ -178,7 +180,7 @@ protected: * connected to this service at the same time. */ explicit ServiceFramework(Core::System& system_, const char* service_name_, - u32 max_sessions_ = DefaultMaxSessions) + u32 max_sessions_ = ServerSessionCountMax) : ServiceFrameworkBase(system_, service_name_, max_sessions_, Invoker) {} /// Registers handlers in the service. -- cgit v1.2.3 From ec5674a6ad89ec705e835d2b711dca18f4ef1616 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 9 Jun 2021 22:29:18 -0700 Subject: hle: service: sm: Fix GetService setup of session & port. --- src/core/hle/kernel/k_client_port.cpp | 2 +- src/core/hle/service/sm/sm.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/core') diff --git a/src/core/hle/kernel/k_client_port.cpp b/src/core/hle/kernel/k_client_port.cpp index 50606bd91..bcb884e8a 100644 --- a/src/core/hle/kernel/k_client_port.cpp +++ b/src/core/hle/kernel/k_client_port.cpp @@ -66,7 +66,7 @@ ResultCode KClientPort::CreateSession(KClientSession** out, // Update the session counts. { // Atomically increment the number of sessions. - s32 new_sessions; + s32 new_sessions{}; { const auto max = max_sessions; auto cur_sessions = num_sessions.load(std::memory_order_acquire); diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index bffa9ffcb..a1e1a7d76 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -164,18 +164,18 @@ ResultVal SM::GetServiceImpl(Kernel::HLERequestContext& R_UNLESS(session_reservation.Succeeded(), Kernel::ResultLimitReached); // Create a new session. - auto* session = Kernel::KSession::Create(kernel); - session->Initialize(&port->GetClientPort(), std::move(name)); + Kernel::KClientSession* session{}; + port->GetClientPort().CreateSession(std::addressof(session)); // Commit the session reservation. session_reservation.Commit(); // Enqueue the session with the named port. - port->EnqueueSession(&session->GetServerSession()); + port->EnqueueSession(&session->GetParent()->GetServerSession()); LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId()); - return MakeResult(&session->GetClientSession()); + return MakeResult(session); } void SM::RegisterService(Kernel::HLERequestContext& ctx) { -- cgit v1.2.3 From b259e95c09f231571c6a1c66046ff859019b1733 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 9 Jun 2021 22:36:42 -0700 Subject: hle: kernel: KClientPort: Add an assert for session count. - Prevents us from over decrementing num_sessions. --- src/core/hle/kernel/k_client_port.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/core') diff --git a/src/core/hle/kernel/k_client_port.cpp b/src/core/hle/kernel/k_client_port.cpp index bcb884e8a..ef168fe87 100644 --- a/src/core/hle/kernel/k_client_port.cpp +++ b/src/core/hle/kernel/k_client_port.cpp @@ -28,6 +28,9 @@ void KClientPort::Initialize(KPort* parent_port_, s32 max_sessions_, std::string void KClientPort::OnSessionFinalized() { KScopedSchedulerLock sl{kernel}; + // This might happen if a session was improperly used with this port. + ASSERT_MSG(num_sessions > 0, "num_sessions is invalid"); + const auto prev = num_sessions--; if (prev == max_sessions) { this->NotifyAvailable(); -- cgit v1.2.3