From b134689f7bf7aecc197730e2da09d061624ea973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 8 Jan 2024 18:17:32 +0100 Subject: [PATCH] Minor code style fixes. --- src/analyzers/DiagnosticDescriptors.cs | 10 +++++++-- src/analyzers/Hosting/EntryPointGenerator.cs | 2 +- src/common/Diagnostics/Check.cs | 4 ++-- src/hosting/InjectedProgramContext.cs | 5 +++-- src/injection/AssemblyInjector.cs | 17 +++++++------- src/injection/IO/ProcessMemoryStream.cs | 4 ++-- src/injection/TargetProcess.cs | 22 +++++++++---------- src/memory/Code/CodePlacement.cs | 2 +- src/memory/Diagnostics/CallTrace.cs | 7 +++--- .../ManagedCallFrameSymbolicator.cs | 3 ++- src/samples/attach/InjectedProgram.cs | 2 +- src/samples/fdd/InjectedProgram.cs | 2 +- src/samples/hooking/InjectedProgram.cs | 2 +- src/samples/suspension/InjectedProgram.cs | 4 ++-- src/samples/trace/Program.cs | 4 ++-- src/system/KernelObject.cs | 2 +- src/system/ProcessObject.cs | 10 ++++----- src/system/SynchronizationObject.cs | 4 ++-- src/system/ThreadObject.cs | 4 ++-- 19 files changed, 59 insertions(+), 51 deletions(-) diff --git a/src/analyzers/DiagnosticDescriptors.cs b/src/analyzers/DiagnosticDescriptors.cs index c24f689..cc3612f 100644 --- a/src/analyzers/DiagnosticDescriptors.cs +++ b/src/analyzers/DiagnosticDescriptors.cs @@ -43,8 +43,14 @@ static DiagnosticDescriptors() var attr = p.GetCustomAttribute(); p.SetValue( - null, - new DiagnosticDescriptor($"RUPT{id}", attr.Title, attr.Message, "Vezel.Ruptura", attr.Severity, true)); + obj: null, + new DiagnosticDescriptor( + $"RUPT{id}", + attr.Title, + attr.Message, + "Vezel.Ruptura", + attr.Severity, + isEnabledByDefault: true)); id++; } diff --git a/src/analyzers/Hosting/EntryPointGenerator.cs b/src/analyzers/Hosting/EntryPointGenerator.cs index 910dd90..4b32d65 100644 --- a/src/analyzers/Hosting/EntryPointGenerator.cs +++ b/src/analyzers/Hosting/EntryPointGenerator.cs @@ -34,7 +34,7 @@ tup is (not null, not null) && { var syms = tup.Right; - // Is the project using the terminal hosting APIs? + // Is the project using the hosting APIs? if (syms.IsEmpty) return; diff --git a/src/common/Diagnostics/Check.cs b/src/common/Diagnostics/Check.cs index 9fbed00..b17868c 100644 --- a/src/common/Diagnostics/Check.cs +++ b/src/common/Diagnostics/Check.cs @@ -115,7 +115,7 @@ public static void Assert( public static void Argument([DoesNotReturnIf(false)] bool condition) { if (!condition) - throw new ArgumentException(null); + throw new ArgumentException(message: null); } public static void Argument( @@ -198,6 +198,6 @@ public static void All( { foreach (var item in value) if (!predicate(item)) - throw new ArgumentException(null, name); + throw new ArgumentException(message: null, name); } } diff --git a/src/hosting/InjectedProgramContext.cs b/src/hosting/InjectedProgramContext.cs index f1f4112..f9fc4a4 100644 --- a/src/hosting/InjectedProgramContext.cs +++ b/src/hosting/InjectedProgramContext.cs @@ -16,7 +16,8 @@ private struct RupturaState public nint ModuleHandle; } - public static InjectedProgramContext Instance { get; private set; } = new(null, 0, 0); + public static InjectedProgramContext Instance { get; private set; } = + new(injectorProcessId: null, mainThreadId: 0, moduleHandle: 0); public int? InjectorProcessId { get; } @@ -64,7 +65,7 @@ public void WakeUp() { Check.Operation(_mainThreadId != 0, $"This process was not created suspended."); - using var thread = ThreadObject.OpenId((int)_mainThreadId, null); + using var thread = ThreadObject.OpenId((int)_mainThreadId, access: null); Check.Operation(thread.Resume() != 0, $"The process appears to have been resumed already."); } diff --git a/src/injection/AssemblyInjector.cs b/src/injection/AssemblyInjector.cs index 72c11d0..c018b42 100644 --- a/src/injection/AssemblyInjector.cs +++ b/src/injection/AssemblyInjector.cs @@ -77,7 +77,7 @@ private string GetModulePath() private void PopulateMemoryArea(nuint area, nint length, Action action) { using var stream = new ProcessMemoryStream(_process.Object, area, length); - using var writer = new InjectionBinaryWriter(stream, true); + using var writer = new InjectionBinaryWriter(stream, is64Bit: true); action(stream, writer); } @@ -105,7 +105,7 @@ private void ForceLoaderInitialization() using var thread = _process.CreateThread(initializeShell, 0); // TODO: Consider making this async with ThreadPool.UnsafeRegisterWaitForSingleObject(). - switch (thread.Wait(_options.InjectionTimeout, false)) + switch (thread.Wait(_options.InjectionTimeout, alertable: false)) { case WaitResult.Signaled: break; @@ -320,7 +320,7 @@ private async Task InjectModuleAsync(string modulePath, nuint parameterArea, Mem break; // Did the thread exit with an error? - switch (thread.Wait(TimeSpan.Zero, false)) + switch (thread.Wait(TimeSpan.Zero, alertable: false)) { case WaitResult.Signaled: throw new InjectionException( @@ -393,7 +393,7 @@ public Task InjectAssemblyAsync() } catch (Exception ex) when (ex is not TimeoutException) { - throw new InjectionException(null, ex); + throw new InjectionException("Assembly injection failed.", ex); } }); } @@ -407,7 +407,8 @@ public Task WaitForCompletionAsync() return Task.Run(async () => { // This is safe because the lambda below captures the thread object and keeps it alive. - using var waitHandle = new ThreadWaitHandle(new(_thread.SafeHandle.DangerousGetHandle(), false)); + using var waitHandle = new ThreadWaitHandle( + new(_thread.SafeHandle.DangerousGetHandle(), ownsHandle: false)); var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var registration = ThreadPool.UnsafeRegisterWaitForSingleObject( @@ -436,9 +437,9 @@ public Task WaitForCompletionAsync() tcs.SetResult(code); }, - null, + state: null, _options.CompletionTimeout, - true); + executeOnlyOnce: true); try { @@ -446,7 +447,7 @@ public Task WaitForCompletionAsync() } finally { - _ = registration.Unregister(null); + _ = registration.Unregister(waitObject: null); } }); } diff --git a/src/injection/IO/ProcessMemoryStream.cs b/src/injection/IO/ProcessMemoryStream.cs index 9fdb4e7..230fb8d 100644 --- a/src/injection/IO/ProcessMemoryStream.cs +++ b/src/injection/IO/ProcessMemoryStream.cs @@ -111,7 +111,7 @@ public override int Read(Span buffer) } catch (Win32Exception ex) { - throw new IOException(null, ex); + throw new IOException(message: null, ex); } _position += len; @@ -158,7 +158,7 @@ public override void Write(ReadOnlySpan buffer) } catch (Win32Exception ex) { - throw new IOException(null, ex); + throw new IOException(message: null, ex); } _position += buffer.Length; diff --git a/src/injection/TargetProcess.cs b/src/injection/TargetProcess.cs index 2d79e8e..581a099 100644 --- a/src/injection/TargetProcess.cs +++ b/src/injection/TargetProcess.cs @@ -65,13 +65,13 @@ public static TargetProcess Create(string fileName, string arguments, string? wo var args = $"\"{fileName}\" {arguments}\0".ToCharArray().AsSpan(); if (!CreateProcessW( - null, + lpApplicationName: null, ref args, - null, - null, - false, + lpProcessAttributes: null, + lpThreadAttributes: null, + bInheritHandles: false, suspended ? PROCESS_CREATION_FLAGS.CREATE_SUSPENDED : 0, - null, + lpEnvironment: null, workingDirectory, startupInfo, out var info)) @@ -107,7 +107,7 @@ public static TargetProcess Open(int id) id, ProcessObject.OpenId( id, ProcessAccess.OperateMemory | ProcessAccess.ReadMemory | ProcessAccess.WriteMemory), - null); + mainThreadId: null); } public void Dispose() @@ -152,7 +152,7 @@ private void DisposeCore() internal nuint AllocateMemory(nint length, MemoryAccess access) { - return (nuint)_object.AllocateMemory(null, length, access); + return (nuint)_object.AllocateMemory(address: null, length, access); } internal void FreeMemory(nuint address) @@ -202,13 +202,13 @@ internal ThreadObject CreateThread(nuint address, nuint parameter) { using var handle = CreateRemoteThreadEx( _object.SafeHandle, - null, - 0, + lpThreadAttributes: null, + dwStackSize: 0, (delegate* unmanaged[Stdcall])address, (void*)parameter, - 0, + dwCreationFlags: 0, (LPPROC_THREAD_ATTRIBUTE_LIST)null, - null); + lpThreadId: null); if (handle.IsInvalid) throw new Win32Exception(); diff --git a/src/memory/Code/CodePlacement.cs b/src/memory/Code/CodePlacement.cs index 99a4807..4b195b3 100644 --- a/src/memory/Code/CodePlacement.cs +++ b/src/memory/Code/CodePlacement.cs @@ -7,7 +7,7 @@ namespace Vezel.Ruptura.Memory.Code; // allocation can extend beyond the highest address, as long as the first byte is reachable. A code manager is free // to shrink this range if required to satisfy e.g. OS constraints. - public static CodePlacement Anywhere { get; } = new(null, (byte*)null - 1); + public static CodePlacement Anywhere { get; } = new(lowestAddress: null, highestAddress: (byte*)null - 1); public void* LowestAddress { get; } diff --git a/src/memory/Diagnostics/CallTrace.cs b/src/memory/Diagnostics/CallTrace.cs index 47564e5..1d6fb29 100644 --- a/src/memory/Diagnostics/CallTrace.cs +++ b/src/memory/Diagnostics/CallTrace.cs @@ -57,7 +57,7 @@ static CallTrace() var processHandle = ProcessObject.Current.SafeHandle; - if (!SymInitializeW(processHandle, null, true)) + if (!SymInitializeW(processHandle, UserSearchPath: null, fInvadeProcess: true)) { _error = Marshal.GetLastPInvokeError(); @@ -100,7 +100,6 @@ public static CallTrace Capture(params CallFrameSymbolicator[] symbolicators) } [MethodImpl(MethodImplOptions.NoInlining)] - [SuppressMessage("", "CA1851")] private static CallTrace CaptureCore(IEnumerable symbolicators) { Check.Null(symbolicators); @@ -168,10 +167,10 @@ private static CallTrace CaptureCore(IEnumerable symbolic threadHandle, ref frame, context, - null, + ReadMemoryRoutine: null, _functionTableAccess, _getModuleBase, - null, + TranslateAddress: null, SYM_STKWALK_DEFAULT)) { var pc = frame.AddrPC.Offset; diff --git a/src/memory/Diagnostics/ManagedCallFrameSymbolicator.cs b/src/memory/Diagnostics/ManagedCallFrameSymbolicator.cs index 080ebb2..7ce0291 100644 --- a/src/memory/Diagnostics/ManagedCallFrameSymbolicator.cs +++ b/src/memory/Diagnostics/ManagedCallFrameSymbolicator.cs @@ -83,6 +83,7 @@ private ManagedCallFrameSymbolicator() } // TODO: Figure out a way to get source location information. - return new((void*)(rmh?.GetFunctionPointer() ?? (nint)frame.IP), sb.ToString(), null, 0, 0); + return new( + (void*)(rmh?.GetFunctionPointer() ?? (nint)frame.IP), sb.ToString(), fileName: null, line: 0, column: 0); } } diff --git a/src/samples/attach/InjectedProgram.cs b/src/samples/attach/InjectedProgram.cs index c83f8be..d42dd84 100644 --- a/src/samples/attach/InjectedProgram.cs +++ b/src/samples/attach/InjectedProgram.cs @@ -46,7 +46,7 @@ public static async Task RunAsync(InjectedProgramContext context, ReadOnlyM } finally { - proc.Kill(true); + proc.Kill(entireProcessTree: true); await proc.WaitForExitAsync(); } diff --git a/src/samples/fdd/InjectedProgram.cs b/src/samples/fdd/InjectedProgram.cs index c83f8be..d42dd84 100644 --- a/src/samples/fdd/InjectedProgram.cs +++ b/src/samples/fdd/InjectedProgram.cs @@ -46,7 +46,7 @@ public static async Task RunAsync(InjectedProgramContext context, ReadOnlyM } finally { - proc.Kill(true); + proc.Kill(entireProcessTree: true); await proc.WaitForExitAsync(); } diff --git a/src/samples/hooking/InjectedProgram.cs b/src/samples/hooking/InjectedProgram.cs index cecf8cb..ee18a5d 100644 --- a/src/samples/hooking/InjectedProgram.cs +++ b/src/samples/hooking/InjectedProgram.cs @@ -66,7 +66,7 @@ public static async Task RunAsync(InjectedProgramContext context, ReadOnlyM } finally { - proc.Kill(true); + proc.Kill(entireProcessTree: true); await proc.WaitForExitAsync(); } diff --git a/src/samples/suspension/InjectedProgram.cs b/src/samples/suspension/InjectedProgram.cs index b7f55b0..6c9fa32 100644 --- a/src/samples/suspension/InjectedProgram.cs +++ b/src/samples/suspension/InjectedProgram.cs @@ -15,7 +15,7 @@ public static async Task RunAsync(InjectedProgramContext context, ReadOnlyM Console.WriteLine("Starting conhost.exe suspended..."); - using var target = TargetProcess.Create("conhost.exe", string.Empty, null, true); + using var target = TargetProcess.Create("conhost.exe", string.Empty, workingDirectory: null, suspended: true); using var proc = Process.GetProcessById(target.Id); using var injector = new AssemblyInjector( target, @@ -45,7 +45,7 @@ public static async Task RunAsync(InjectedProgramContext context, ReadOnlyM } finally { - proc.Kill(true); + proc.Kill(entireProcessTree: true); await proc.WaitForExitAsync(); } diff --git a/src/samples/trace/Program.cs b/src/samples/trace/Program.cs index dee64c4..3672657 100644 --- a/src/samples/trace/Program.cs +++ b/src/samples/trace/Program.cs @@ -50,10 +50,10 @@ private static int SetThreadDescriptionHook(nint hThread, char* lpThreadDescript typeof(Program).GetMethod( nameof(CaptureTrace), BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.NonPublic)!, - null); + optionalParameterTypes: null); cil.Emit(OpCodes.Ret); - _ = method.Invoke(null, null); + _ = method.Invoke(obj: null, parameters: null); } catch (Exception ex) { diff --git a/src/system/KernelObject.cs b/src/system/KernelObject.cs index f3e3edc..23c0b4d 100644 --- a/src/system/KernelObject.cs +++ b/src/system/KernelObject.cs @@ -40,7 +40,7 @@ public bool IsInheritable private protected KernelObject(nint handle) { - _safeHandle = new SafeKernelHandle(handle, true); + _safeHandle = new SafeKernelHandle(handle, ownsHandle: true); } ~KernelObject() diff --git a/src/system/ProcessObject.cs b/src/system/ProcessObject.cs index c76f300..d0c1cea 100644 --- a/src/system/ProcessObject.cs +++ b/src/system/ProcessObject.cs @@ -54,7 +54,7 @@ public static ProcessObject OpenId(int id, ProcessAccess? access) { return OpenProcess( access is ProcessAccess acc ? (PROCESS_ACCESS_RIGHTS)acc : PROCESS_ACCESS_RIGHTS.PROCESS_ALL_ACCESS, - false, + bInheritHandle: false, (uint)id) is { IsNull: false } handle ? new(handle) : throw new Win32Exception(); @@ -62,7 +62,7 @@ public static ProcessObject OpenId(int id, ProcessAccess? access) public static ProcessObject OpenCurrent() { - return OpenId(CurrentId, null); + return OpenId(CurrentId, access: null); } public static void FlushWriteBuffers() @@ -135,7 +135,7 @@ static long ToTicks(FILETIME time) return VirtualAlloc2( SafeHandle, - null, + BaseAddress: null, (nuint)length, VIRTUAL_ALLOCATION_TYPE.MEM_COMMIT | VIRTUAL_ALLOCATION_TYPE.MEM_RESERVE, (uint)(access == MemoryAccess.None ? PAGE_PROTECTION_FLAGS.PAGE_NOACCESS : (PAGE_PROTECTION_FLAGS)access), @@ -164,13 +164,13 @@ public MemoryAccess ProtectMemory(void* address, nint length, MemoryAccess acces public void ReadMemory(void* source, void* destination, nint length) { - if (!ReadProcessMemory(SafeHandle, source, destination, (nuint)length, null)) + if (!ReadProcessMemory(SafeHandle, source, destination, (nuint)length, lpNumberOfBytesRead: null)) throw new Win32Exception(); } public void WriteMemory(void* destination, void* source, nint length) { - if (!WriteProcessMemory(SafeHandle, destination, source, (nuint)length, null)) + if (!WriteProcessMemory(SafeHandle, destination, source, (nuint)length, lpNumberOfBytesWritten: null)) throw new Win32Exception(); } diff --git a/src/system/SynchronizationObject.cs b/src/system/SynchronizationObject.cs index 5c03d7e..c47bcd1 100644 --- a/src/system/SynchronizationObject.cs +++ b/src/system/SynchronizationObject.cs @@ -56,7 +56,7 @@ private static WAIT_EVENT WaitMultiple( public static (WaitResult Result, int? Index) WaitAny( scoped ReadOnlySpan objects, TimeSpan timeout, bool alertable) { - return WaitMultiple(objects, false, timeout, alertable) switch + return WaitMultiple(objects, all: false, timeout, alertable) switch { WAIT_EVENT.WAIT_TIMEOUT => (WaitResult.TimedOut, null), WAIT_EVENT.WAIT_IO_COMPLETION => (WaitResult.Alerted, null), @@ -72,7 +72,7 @@ public static WaitResult WaitAll( { // Note that when waiting for all objects, the index that is returned is meaningless, even in the case of an // abandoned mutex (or several). - return WaitMultiple(objects, true, timeout, alertable) switch + return WaitMultiple(objects, all: true, timeout, alertable) switch { WAIT_EVENT.WAIT_TIMEOUT => WaitResult.TimedOut, WAIT_EVENT.WAIT_IO_COMPLETION => WaitResult.Alerted, diff --git a/src/system/ThreadObject.cs b/src/system/ThreadObject.cs index b8e80a7..e0a0619 100644 --- a/src/system/ThreadObject.cs +++ b/src/system/ThreadObject.cs @@ -76,7 +76,7 @@ public static ThreadObject OpenId(int id, ThreadAccess? access) { return OpenThread( access is ThreadAccess acc ? (THREAD_ACCESS_RIGHTS)acc : THREAD_ACCESS_RIGHTS.THREAD_ALL_ACCESS, - false, + bInheritHandle: false, (uint)id) is { IsNull: false } handle ? new(handle) : throw new Win32Exception(); @@ -84,7 +84,7 @@ public static ThreadObject OpenId(int id, ThreadAccess? access) public static ThreadObject OpenCurrent() { - return OpenId(CurrentId, null); + return OpenId(CurrentId, access: null); } public static void GetStackBounds(out void* low, out void* high)