Skip to content

Commit

Permalink
Minor code style fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrp committed Jan 8, 2024
1 parent 1df2a55 commit b134689
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 51 deletions.
10 changes: 8 additions & 2 deletions src/analyzers/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ static DiagnosticDescriptors()
var attr = p.GetCustomAttribute<DiagnosticAttribute>();

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++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/analyzers/Hosting/EntryPointGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/common/Diagnostics/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
Expand Down Expand Up @@ -198,6 +198,6 @@ public static void All<T>(
{
foreach (var item in value)
if (!predicate(item))
throw new ArgumentException(null, name);
throw new ArgumentException(message: null, name);
}
}
5 changes: 3 additions & 2 deletions src/hosting/InjectedProgramContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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.");
}
Expand Down
17 changes: 9 additions & 8 deletions src/injection/AssemblyInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private string GetModulePath()
private void PopulateMemoryArea(nuint area, nint length, Action<ProcessMemoryStream, InjectionBinaryWriter> 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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
});
}
Expand All @@ -407,7 +407,8 @@ public Task<int> 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<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var registration = ThreadPool.UnsafeRegisterWaitForSingleObject(
Expand Down Expand Up @@ -436,17 +437,17 @@ public Task<int> WaitForCompletionAsync()

tcs.SetResult(code);
},
null,
state: null,
_options.CompletionTimeout,
true);
executeOnlyOnce: true);

try
{
return await tcs.Task.ConfigureAwait(false);
}
finally
{
_ = registration.Unregister(null);
_ = registration.Unregister(waitObject: null);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/injection/IO/ProcessMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public override int Read(Span<byte> buffer)
}
catch (Win32Exception ex)
{
throw new IOException(null, ex);
throw new IOException(message: null, ex);
}

_position += len;
Expand Down Expand Up @@ -158,7 +158,7 @@ public override void Write(ReadOnlySpan<byte> buffer)
}
catch (Win32Exception ex)
{
throw new IOException(null, ex);
throw new IOException(message: null, ex);
}

_position += buffer.Length;
Expand Down
22 changes: 11 additions & 11 deletions src/injection/TargetProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]<void*, uint>)address,
(void*)parameter,
0,
dwCreationFlags: 0,
(LPPROC_THREAD_ATTRIBUTE_LIST)null,
null);
lpThreadId: null);

if (handle.IsInvalid)
throw new Win32Exception();
Expand Down
2 changes: 1 addition & 1 deletion src/memory/Code/CodePlacement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
7 changes: 3 additions & 4 deletions src/memory/Diagnostics/CallTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -100,7 +100,6 @@ public static CallTrace Capture(params CallFrameSymbolicator[] symbolicators)
}

[MethodImpl(MethodImplOptions.NoInlining)]
[SuppressMessage("", "CA1851")]
private static CallTrace CaptureCore(IEnumerable<CallFrameSymbolicator> symbolicators)
{
Check.Null(symbolicators);
Expand Down Expand Up @@ -168,10 +167,10 @@ private static CallTrace CaptureCore(IEnumerable<CallFrameSymbolicator> symbolic
threadHandle,
ref frame,
context,
null,
ReadMemoryRoutine: null,
_functionTableAccess,
_getModuleBase,
null,
TranslateAddress: null,
SYM_STKWALK_DEFAULT))
{
var pc = frame.AddrPC.Offset;
Expand Down
3 changes: 2 additions & 1 deletion src/memory/Diagnostics/ManagedCallFrameSymbolicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/samples/attach/InjectedProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static async Task<int> RunAsync(InjectedProgramContext context, ReadOnlyM
}
finally
{
proc.Kill(true);
proc.Kill(entireProcessTree: true);

await proc.WaitForExitAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion src/samples/fdd/InjectedProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static async Task<int> RunAsync(InjectedProgramContext context, ReadOnlyM
}
finally
{
proc.Kill(true);
proc.Kill(entireProcessTree: true);

await proc.WaitForExitAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion src/samples/hooking/InjectedProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static async Task<int> RunAsync(InjectedProgramContext context, ReadOnlyM
}
finally
{
proc.Kill(true);
proc.Kill(entireProcessTree: true);

await proc.WaitForExitAsync();
}
Expand Down
4 changes: 2 additions & 2 deletions src/samples/suspension/InjectedProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static async Task<int> 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,
Expand Down Expand Up @@ -45,7 +45,7 @@ public static async Task<int> RunAsync(InjectedProgramContext context, ReadOnlyM
}
finally
{
proc.Kill(true);
proc.Kill(entireProcessTree: true);

await proc.WaitForExitAsync();
}
Expand Down
4 changes: 2 additions & 2 deletions src/samples/trace/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/system/KernelObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public bool IsInheritable

private protected KernelObject(nint handle)
{
_safeHandle = new SafeKernelHandle(handle, true);
_safeHandle = new SafeKernelHandle(handle, ownsHandle: true);
}

~KernelObject()
Expand Down
10 changes: 5 additions & 5 deletions src/system/ProcessObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ 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();
}

public static ProcessObject OpenCurrent()
{
return OpenId(CurrentId, null);
return OpenId(CurrentId, access: null);
}

public static void FlushWriteBuffers()
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/system/SynchronizationObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static WAIT_EVENT WaitMultiple(
public static (WaitResult Result, int? Index) WaitAny(
scoped ReadOnlySpan<SynchronizationObject> 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),
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/system/ThreadObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ 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();
}

public static ThreadObject OpenCurrent()
{
return OpenId(CurrentId, null);
return OpenId(CurrentId, access: null);
}

public static void GetStackBounds(out void* low, out void* high)
Expand Down

0 comments on commit b134689

Please sign in to comment.