From 5feda37688cafee8054910cd05916742c8263f89 Mon Sep 17 00:00:00 2001
From: Liam <byteslice@airmail.cc>
Date: Sat, 9 Dec 2023 11:25:21 -0500
Subject: [PATCH] service: populate pid and handle table from client

---
 src/core/hle/kernel/k_server_session.cpp |  3 +--
 src/core/hle/service/hle_ipc.cpp         | 15 +++++++++------
 src/core/hle/service/hle_ipc.h           | 14 +++++++++-----
 3 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/src/core/hle/kernel/k_server_session.cpp b/src/core/hle/kernel/k_server_session.cpp
index 3ea653163b..598ec78780 100644
--- a/src/core/hle/kernel/k_server_session.cpp
+++ b/src/core/hle/kernel/k_server_session.cpp
@@ -462,8 +462,7 @@ Result KServerSession::ReceiveRequest(std::shared_ptr<Service::HLERequestContext
             std::make_shared<Service::HLERequestContext>(m_kernel, memory, this, client_thread);
         (*out_context)->SetSessionRequestManager(manager);
         (*out_context)
-            ->PopulateFromIncomingCommandBuffer(client_thread->GetOwnerProcess()->GetHandleTable(),
-                                                cmd_buf);
+            ->PopulateFromIncomingCommandBuffer(*client_thread->GetOwnerProcess(), cmd_buf);
     } else {
         KThread* server_thread = GetCurrentThreadPointer(m_kernel);
         KProcess& src_process = *client_thread->GetOwnerProcess();
diff --git a/src/core/hle/service/hle_ipc.cpp b/src/core/hle/service/hle_ipc.cpp
index ff374ae395..38955932cd 100644
--- a/src/core/hle/service/hle_ipc.cpp
+++ b/src/core/hle/service/hle_ipc.cpp
@@ -146,8 +146,10 @@ HLERequestContext::HLERequestContext(Kernel::KernelCore& kernel_, Core::Memory::
 
 HLERequestContext::~HLERequestContext() = default;
 
-void HLERequestContext::ParseCommandBuffer(const Kernel::KHandleTable& handle_table,
-                                           u32_le* src_cmdbuf, bool incoming) {
+void HLERequestContext::ParseCommandBuffer(Kernel::KProcess& process, u32_le* src_cmdbuf,
+                                           bool incoming) {
+    client_handle_table = &process.GetHandleTable();
+
     IPC::RequestParser rp(src_cmdbuf);
     command_header = rp.PopRaw<IPC::CommandHeader>();
 
@@ -160,7 +162,8 @@ void HLERequestContext::ParseCommandBuffer(const Kernel::KHandleTable& handle_ta
     if (command_header->enable_handle_descriptor) {
         handle_descriptor_header = rp.PopRaw<IPC::HandleDescriptorHeader>();
         if (handle_descriptor_header->send_current_pid) {
-            pid = rp.Pop<u64>();
+            pid = process.GetProcessId();
+            rp.Skip(2, false);
         }
         if (incoming) {
             // Populate the object lists with the data in the IPC request.
@@ -267,9 +270,9 @@ void HLERequestContext::ParseCommandBuffer(const Kernel::KHandleTable& handle_ta
     rp.Skip(1, false); // The command is actually an u64, but we don't use the high part.
 }
 
-Result HLERequestContext::PopulateFromIncomingCommandBuffer(
-    const Kernel::KHandleTable& handle_table, u32_le* src_cmdbuf) {
-    ParseCommandBuffer(handle_table, src_cmdbuf, true);
+Result HLERequestContext::PopulateFromIncomingCommandBuffer(Kernel::KProcess& process,
+                                                            u32_le* src_cmdbuf) {
+    ParseCommandBuffer(process, src_cmdbuf, true);
 
     if (command_header->IsCloseCommand()) {
         // Close does not populate the rest of the IPC header
diff --git a/src/core/hle/service/hle_ipc.h b/src/core/hle/service/hle_ipc.h
index 4436f4f83f..18d464c63b 100644
--- a/src/core/hle/service/hle_ipc.h
+++ b/src/core/hle/service/hle_ipc.h
@@ -38,6 +38,7 @@ namespace Kernel {
 class KAutoObject;
 class KernelCore;
 class KHandleTable;
+class KProcess;
 class KServerSession;
 class KThread;
 } // namespace Kernel
@@ -195,8 +196,7 @@ public:
     }
 
     /// Populates this context with data from the requesting process/thread.
-    Result PopulateFromIncomingCommandBuffer(const Kernel::KHandleTable& handle_table,
-                                             u32_le* src_cmdbuf);
+    Result PopulateFromIncomingCommandBuffer(Kernel::KProcess& process, u32_le* src_cmdbuf);
 
     /// Writes data from this context back to the requesting process/thread.
     Result WriteToOutgoingCommandBuffer(Kernel::KThread& requesting_thread);
@@ -359,6 +359,10 @@ public:
         return *thread;
     }
 
+    Kernel::KHandleTable& GetClientHandleTable() {
+        return *client_handle_table;
+    }
+
     [[nodiscard]] std::shared_ptr<SessionRequestManager> GetManager() const {
         return manager.lock();
     }
@@ -374,12 +378,12 @@ public:
 private:
     friend class IPC::ResponseBuilder;
 
-    void ParseCommandBuffer(const Kernel::KHandleTable& handle_table, u32_le* src_cmdbuf,
-                            bool incoming);
+    void ParseCommandBuffer(Kernel::KProcess& process, u32_le* src_cmdbuf, bool incoming);
 
     std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
     Kernel::KServerSession* server_session{};
-    Kernel::KThread* thread;
+    Kernel::KHandleTable* client_handle_table{};
+    Kernel::KThread* thread{};
 
     std::vector<Handle> incoming_move_handles;
     std::vector<Handle> incoming_copy_handles;