clean up and standardize types across the codebase, unify atomic operations, improve UTF-8 handling throughout the system, remove obsolete input and named queue interfaces, redesign process preparation and creation, tighten IPC and service boundaries, improve error handling and reliability, minor documentation cleanup, fix doom surfaces, fix panic in ini parsing.

This commit is contained in:
maksw committed 2026-10-12 18:17:43 +02:00
1 parent 899225f551
commit c0ad63c27f
226 files changed
+6562 -4147

No files matched your search

+27 -19
View File
@@ -1,5 +1,7 @@
// Kernel process model. This file owns process lifetime, thread membership, and address-space metadata.
#include <ntlite/atomic.h>
#include <stdbool.h>
#include <string.h>
#include <kernel/ke_idt.h>
#include <kernel/ke_hal.h>
@@ -145,8 +147,8 @@ size_t KeCopyProcessInfo(struct SYS_PROCESS_INFO* infoOut, size_t maxCount) {
KeReleaseSpinlock(&p->ThreadLock, threadFlags);
uint32_t handleCount = KeGetHandleCount(p);
int32_t exitCode = p->ExitCode;
long terminating = p->Terminating;
long exited = p->Exited;
int32_t terminating = p->Terminating;
int32_t exited = p->Exited;
info->ThreadCount = threadCount;
info->HandleCount = handleCount;
@@ -170,7 +172,7 @@ size_t KeCopyProcessInfo(struct SYS_PROCESS_INFO* infoOut, size_t maxCount) {
} else if (terminating) {
info->State = SYS_PROCESS_STATE_TERMINATING;
info->Flags |= SYS_PROCESS_FLAG_TERMINATING;
} else if (_InterlockedCompareExchange(&p->BuildState, 0, 0) != KE_PROCESS_BUILD_STARTED) {
} else if (NtlAtomicLoad32(&p->BuildState) != KE_PROCESS_BUILD_STARTED) {
info->State = SYS_PROCESS_STATE_CONSTRUCTING;
info->Flags |= SYS_PROCESS_FLAG_CONSTRUCTING;
} else {
@@ -298,7 +300,7 @@ KSTATUS KeCopyProcessThreadInfo(uint32_t pid, struct SYS_THREAD_INFO* infoOut, s
// both observe a process whose threads are already gone.
static void KiProcessRundownResources(PROCESS* proc) {
if (!proc) return;
if (_InterlockedExchange(&proc->ResourcesReleased, 1) != 0)
if (NtlAtomicExchange32(&proc->ResourcesReleased, 1) != 0)
return;
IoReleaseFileLocksForProcess(proc);
@@ -377,6 +379,9 @@ KSTATUS KeCreateProcess(const char* name, SECURITY_CONTEXT* securityContext, PRO
KiProcessInit();
if (!name || !securityContext || !out) return NTLSTATUS_INVALID_PARAMETER;
size_t nameLength = strlen(name);
if (!SysSvIsUtf8Text(SysSvFromParts(name, nameLength)))
return NTLSTATUS_INVALID_PARAMETER;
uint32_t processId = 0;
KSTATUS idStatus = KeAllocateId32(&gProcessIds, &processId);
@@ -391,8 +396,14 @@ KSTATUS KeCreateProcess(const char* name, SECURITY_CONTEXT* securityContext, PRO
proc->Flags = 0;
proc->SecurityContext = securityContext;
ObReferenceObject(&securityContext->Header);
strncpy(proc->Name, name, PROCESS_NAME_MAX - 1);
proc->Name[PROCESS_NAME_MAX - 1] = '\0';
// Truncate the diagnostic name on a scalar boundary, never inside UTF-8.
size_t nameCopy = nameLength < PROCESS_NAME_MAX ? nameLength : PROCESS_NAME_MAX - 1u;
if (nameCopy < nameLength) {
while (nameCopy && (((uint8_t)name[nameCopy] & 0xC0u) == 0x80u))
--nameCopy;
}
memcpy(proc->Name, name, nameCopy);
proc->Name[nameCopy] = '\0';
LIST_INIT(&proc->ThreadList);
proc->ThreadCount = 0;
@@ -463,8 +474,7 @@ static KSTATUS KiCopyEnvironmentStringView(PROCESS* proc,
*baseOut = 0;
*sizeOut = 0;
if (!SysSvIsValid(src)) return NTLSTATUS_INVALID_PARAMETER;
if (SysSvContainsByte(src, '\0')) return NTLSTATUS_INVALID_PARAMETER;
if (!SysSvIsUtf8Text(src)) return NTLSTATUS_INVALID_PARAMETER;
if (src.Length == (size_t)-1) return NTLSTATUS_BUFFER_TOO_SMALL;
size_t len = src.Length + 1;
@@ -509,9 +519,7 @@ KSTATUS KeSetupUserProcessEnvironment(PROCESS* proc,
SYS_STRING_VIEW imagePath,
SYS_STRING_VIEW commandLine) {
if (!proc || imagePath.Length == 0) return NTLSTATUS_INVALID_PARAMETER;
if (!SysSvIsValid(imagePath) || !SysSvIsValid(commandLine))
return NTLSTATUS_INVALID_PARAMETER;
if (SysSvContainsByte(imagePath, '\0') || SysSvContainsByte(commandLine, '\0'))
if (!SysSvIsUtf8Text(imagePath) || !SysSvIsUtf8Text(commandLine))
return NTLSTATUS_INVALID_PARAMETER;
SYS_STRING_VIEW finalCommandLine = commandLine.Length ? commandLine : imagePath;
@@ -594,19 +602,19 @@ KSTATUS KeSetProcessLoaderData(PROCESS* proc, const void* loaderData) {
// Stops new process-construction operations. Active builders defer final teardown until they leave.
void KeStopProcessConstruction(PROCESS* process) {
if (!process) return;
_InterlockedExchange(&process->BuildState, KE_PROCESS_BUILD_DEAD);
NtlAtomicExchange32(&process->BuildState, KE_PROCESS_BUILD_DEAD);
}
// Process exit / termination.
void KeCompleteProcessExitIfLastThread(PROCESS* process) {
if (!process) return;
if (_InterlockedCompareExchange(&process->BuildOperations, 0, 0) != 0) return;
if (NtlAtomicLoad32(&process->BuildOperations) != 0) return;
uintptr_t flags = KeAcquireSpinlock(&process->ThreadLock);
uint32_t threadCount = process->ThreadCount;
KeReleaseSpinlock(&process->ThreadLock, flags);
if (threadCount == 0 && _InterlockedExchange(&process->Exited, 1) == 0) {
if (threadCount == 0 && NtlAtomicExchange32(&process->Exited, 1) == 0) {
KiProcessRundownResources(process);
if (process->ExitEvent) KeSignalEvent(process->ExitEvent);
}
@@ -624,9 +632,9 @@ KSTATUS KeTerminateProcess(PROCESS* process, int32_t exitCode) {
if (KeIsProcessTerminationProtected(process)) return NTLSTATUS_ACCESS_DENIED;
process->ExitCode = exitCode;
_InterlockedExchange(&process->Terminating, 1);
NtlAtomicExchange32(&process->Terminating, 1);
KeStopProcessConstruction(process);
if (_InterlockedCompareExchange(&process->BuildOperations, 0, 0) != 0)
if (NtlAtomicLoad32(&process->BuildOperations) != 0)
return NTLSTATUS_SUCCESS;
uintptr_t flags = KeAcquireSpinlock(&process->ThreadLock);
@@ -655,7 +663,7 @@ KSTATUS KeTerminateProcess(PROCESS* process, int32_t exitCode) {
if (t->RunningCpu != KE_INVALID_CPU && t->RunningCpu != selfIndex && t->RunningCpu < gCpuCount) {
KCPU* cpu = &gCpus[t->RunningCpu];
if (cpu->State == KeCpuOnline) {
_InterlockedExchange(&cpu->ReschedulePending, 1);
NtlAtomicExchange32(&cpu->ReschedulePending, 1);
KeSendProcessorInterrupt(cpu->HardwareId, KE_VECTOR_RESCHEDULE_IPI);
}
}
@@ -667,12 +675,12 @@ KSTATUS KeTerminateProcess(PROCESS* process, int32_t exitCode) {
}
// Checks whether process Terminating.
int KeIsProcessTerminating(PROCESS* process) {
bool KeIsProcessTerminating(PROCESS* process) {
return process && process->Terminating != 0;
}
// Returns whether process termination is forbidden.
int KeIsProcessTerminationProtected(PROCESS* process) {
bool KeIsProcessTerminationProtected(PROCESS* process) {
return process && (process->Flags & KE_PROCESS_FLAG_TERMINATION_PROTECTED) != 0;
}