kernel/svc: Handle thread handles within GetProcessId

If a thread handle is passed to svcGetProcessId, the kernel attempts to
access the process ID via the thread's instance's owning process.

Technically, this function should also be handling the kernel debug
objects as well, however we currently don't handle those kernel objects
yet, so I've left a note via a comment about it to remind myself when
implementing it in the future.
This commit is contained in:
Lioncash 2018-12-18 23:09:08 -05:00
parent 62d4377053
commit b74eb88c68

View file

@ -364,22 +364,35 @@ static ResultCode GetThreadId(u64* thread_id, Handle thread_handle) {
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
/// Get the ID of the specified process /// Gets the ID of the specified process or a specified thread's owning process.
static ResultCode GetProcessId(u64* process_id, Handle process_handle) { static ResultCode GetProcessId(u64* process_id, Handle handle) {
LOG_TRACE(Kernel_SVC, "called process=0x{:08X}", process_handle); LOG_DEBUG(Kernel_SVC, "called handle=0x{:08X}", handle);
const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
const SharedPtr<Process> process = handle_table.Get<Process>(process_handle); const SharedPtr<Process> process = handle_table.Get<Process>(handle);
if (!process) { if (process) {
LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", *process_id = process->GetProcessID();
process_handle); return RESULT_SUCCESS;
}
const SharedPtr<Thread> thread = handle_table.Get<Thread>(handle);
if (thread) {
const Process* const owner_process = thread->GetOwnerProcess();
if (!owner_process) {
LOG_ERROR(Kernel_SVC, "Non-existent owning process encountered.");
return ERR_INVALID_HANDLE; return ERR_INVALID_HANDLE;
} }
*process_id = process->GetProcessID(); *process_id = owner_process->GetProcessID();
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
// NOTE: This should also handle debug objects before returning.
LOG_ERROR(Kernel_SVC, "Handle does not exist, handle=0x{:08X}", handle);
return ERR_INVALID_HANDLE;
}
/// Default thread wakeup callback for WaitSynchronization /// Default thread wakeup callback for WaitSynchronization
static bool DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thread> thread, static bool DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thread> thread,
SharedPtr<WaitObject> object, std::size_t index) { SharedPtr<WaitObject> object, std::size_t index) {