ARchive old spec

This commit is contained in:
kbe
2026-07-21 22:30:10 +02:00
parent 0380705e76
commit 184dec86ca
17 changed files with 550 additions and 191 deletions
+19 -19
View File
@@ -12,6 +12,13 @@ namespace WhiteMagic;
/// empty / zero bytes) on invalid or protected addresses instead of crashing
/// the host process with an <see cref="AccessViolationException"/>.
/// </summary>
/// <remarks>
/// This is functionally equivalent to <see cref="ExternalReader"/> opened on the current
/// process. It exists as a distinct type because the design (see
/// <c>openspec/changes/whitemagic-foundation/design.md</c> D1) treats "injected in-process"
/// as a separate mode from "external". The two modes will diverge further once the
/// <c>InProcessInvoker</c> delegate-call path lands.
/// </remarks>
public sealed class InProcessReader : MemoryBase
{
private readonly SafeMemoryHandle _handle;
@@ -35,7 +42,16 @@ public sealed class InProcessReader : MemoryBase
$"OpenProcess failed for PID {current.Id}: error {error}");
}
_imageBase = current.MainModule?.BaseAddress ?? IntPtr.Zero;
// Process.MainModule rarely throws on the current process, but guard it
// nonetheless for parity with ExternalReader.
try
{
_imageBase = current.MainModule?.BaseAddress ?? IntPtr.Zero;
}
catch (System.ComponentModel.Win32Exception)
{
_imageBase = IntPtr.Zero;
}
}
/// <inheritdoc />
@@ -50,18 +66,7 @@ public sealed class InProcessReader : MemoryBase
if (isRelative)
address = GetAbsolute(address);
byte[] buffer = new byte[count];
if (!NativeMethods.ReadProcessMemory(_handle, address, buffer, count, out nint bytesRead))
{
return [];
}
if ((int)bytesRead != count)
{
Array.Resize(ref buffer, (int)bytesRead);
}
return buffer;
return RpmHelper.ReadBytes(_handle, address, count);
}
/// <inheritdoc />
@@ -70,12 +75,7 @@ public sealed class InProcessReader : MemoryBase
if (isRelative)
address = GetAbsolute(address);
if (!NativeMethods.WriteProcessMemory(_handle, address, bytes, bytes.Length, out nint written))
{
return 0;
}
return (int)written;
return RpmHelper.WriteBytes(_handle, address, bytes);
}
/// <inheritdoc />