From f680830b53e1dee0cf86d50c1c177a7531431469 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Thu, 4 Jun 2026 14:36:43 +0200 Subject: [PATCH] Implement resource cleanup for failed allocations during idle thread setup --- xtoskrnl/ke/kprocess.cc | 3 ++- xtoskrnl/ke/kthread.cc | 7 +++++-- xtoskrnl/ps/thread.cc | 11 ++++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/xtoskrnl/ke/kprocess.cc b/xtoskrnl/ke/kprocess.cc index 7b35e11..d958c43 100644 --- a/xtoskrnl/ke/kprocess.cc +++ b/xtoskrnl/ke/kprocess.cc @@ -82,7 +82,8 @@ KE::KProcess::InitializeIdleProcess(IN OUT PKPROCESS Process, Status = MM::Allocator::AllocatePool(NonPagedPool, MapSize, (PVOID*)&Process->ActiveProcessors); if(Status != STATUS_SUCCESS) { - /* Memory allocation failed, return the status code */ + /* Memory allocation failed, free previously allocated memory and return the status code */ + MM::Allocator::FreePool((PVOID)Process->Affinity); return Status; } diff --git a/xtoskrnl/ke/kthread.cc b/xtoskrnl/ke/kthread.cc index 5a4d5a2..5842694 100644 --- a/xtoskrnl/ke/kthread.cc +++ b/xtoskrnl/ke/kthread.cc @@ -75,7 +75,8 @@ KE::KThread::InitializeIdleThread(IN PKPROCESS IdleProcess, Status = MM::Allocator::AllocatePool(NonPagedPool, MapSize, (PVOID*)&IdleThread->UserAffinity); if(Status != STATUS_SUCCESS) { - /* Memory allocation failed, return the status code */ + /* Memory allocation failed, free previously allocated memory and return the status code */ + MM::Allocator::FreePool((PVOID)IdleThread->Affinity); return Status; } @@ -88,7 +89,9 @@ KE::KThread::InitializeIdleThread(IN PKPROCESS IdleProcess, NULLPTR, NULLPTR, Stack, TRUE); if(Status != STATUS_SUCCESS) { - /* Failed to initialize IDLE thread, return status code */ + /* Failed to initialize IDLE thread, free both affinity maps and return the status code */ + MM::Allocator::FreePool((PVOID)IdleThread->Affinity); + MM::Allocator::FreePool((PVOID)IdleThread->UserAffinity); return Status; } diff --git a/xtoskrnl/ps/thread.cc b/xtoskrnl/ps/thread.cc index f35a905..5507dc4 100644 --- a/xtoskrnl/ps/thread.cc +++ b/xtoskrnl/ps/thread.cc @@ -50,5 +50,14 @@ PS::Thread::CreateIdleThread(IN PKPROCESSOR_CONTROL_BLOCK Prcb, Prcb->IdleThread = &IdleThread->ThreadControlBlock; /* Initialize the IDLE thread */ - return KE::KThread::InitializeIdleThread(IdleProcess, &IdleThread->ThreadControlBlock, Prcb, Stack); + Status = KE::KThread::InitializeIdleThread(IdleProcess, &IdleThread->ThreadControlBlock, Prcb, Stack); + if(Status != STATUS_SUCCESS) + { + /* Failed to initialize the IDLE thread state, free the ETHREAD object and return the status code */ + MM::Allocator::FreePool((PVOID)IdleThread); + return Status; + } + + /* Return success */ + return STATUS_SUCCESS; }