Merge feature/memory-hardening into develop

Second-review hardening of the memory layer (char sizing, reference-struct
routing, count guards, ReadString partial-advance, ExternalReader access +
MainModule guard, DWORD signatures) plus the in-progress Phase 3 StubAssembler
work carried on the branch. 76 tests green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
kbe
2026-07-21 19:49:29 +02:00
co-authored by Claude Opus 4.8
9 changed files with 547 additions and 15 deletions
+19
View File
@@ -0,0 +1,19 @@
namespace WhiteMagic.Assembly;
/// <summary>
/// x86/x86-64 calling conventions for call-stub generation.
/// </summary>
public enum CallingConvention
{
/// <summary>Caller pushes args right-to-left and cleans the stack (x86).</summary>
Cdecl,
/// <summary>Caller pushes args right-to-left; callee cleans the stack (x86).</summary>
Stdcall,
/// <summary>ECX receives the <c>this</c> pointer; remaining args on stack right-to-left; callee cleans (x86).</summary>
Thiscall,
/// <summary>ECX/EDX receive the first two args; remaining on stack right-to-left; callee cleans (x86).</summary>
Fastcall,
}
+17
View File
@@ -0,0 +1,17 @@
namespace WhiteMagic.Assembly;
/// <summary>
/// Abstraction over an x86/x64 assembler. The default <see cref="StubAssembler"/>
/// hand-emits calling-convention trampolines (no parsing, zero dep). An optional
/// <see cref="IcedAssembler"/> (Phase 8) handles arbitrary mnemonics via the Iced
/// library.
/// </summary>
public interface IAssembler
{
/// <summary>
/// Assembles text mnemonics into machine code.
/// </summary>
/// <param name="assemblyText">The assembly text (Intel syntax).</param>
/// <param name="origin">The base address for relative encodings.</param>
byte[] Assemble(string assemblyText, ulong origin = 0);
}
+118
View File
@@ -0,0 +1,118 @@
namespace WhiteMagic.Assembly;
/// <summary>
/// The default <see cref="IAssembler"/> backend. Hand-emits calling-convention
/// trampolines and injection stubs using deterministic byte emitters
/// (<see cref="EmitU8"/>, <see cref="EmitU32"/>, <see cref="EmitU64"/>). Has
/// no native or third-party dependency — no FASM, no Iced.
/// </summary>
/// <remarks>
/// <see cref="Assemble"/> is not supported by this backend (it is a parse-free
/// emitter, not a text assembler). Use <see cref="IcedAssembler"/> (Phase 8) for
/// arbitrary mnemonics.
/// </remarks>
public class StubAssembler : IAssembler
{
/// <inheritdoc />
public byte[] Assemble(string assemblyText, ulong origin = 0)
{
throw new NotSupportedException(
"StubAssembler does not parse text assembly. " +
"Use IcedAssembler (Phase 8) for arbitrary mnemonics.");
}
// ── Emit primitives ────────────────────────────────────────────────────
public void EmitU8(List<byte> buffer, byte value) => buffer.Add(value);
public void EmitU32(List<byte> buffer, uint value)
{
buffer.Add((byte)value);
buffer.Add((byte)(value >> 8));
buffer.Add((byte)(value >> 16));
buffer.Add((byte)(value >> 24));
}
public void EmitU64(List<byte> buffer, ulong value)
{
EmitU32(buffer, (uint)value);
EmitU32(buffer, (uint)(value >> 32));
}
// ── Call-stub builders ─────────────────────────────────────────────────
public byte[] BuildCallStub(IntPtr stubAddress, IntPtr targetAddress,
uint[] arguments, int pointerSize, CallingConvention convention)
{
var buffer = new List<byte>(64);
if (pointerSize == 4)
BuildX86Stub(buffer, (uint)stubAddress, (uint)targetAddress, arguments, convention);
else
BuildX64Stub(buffer, (ulong)stubAddress, (ulong)targetAddress, arguments);
return buffer.ToArray();
}
private void BuildX86Stub(List<byte> buffer, uint stubAddr,
uint target, uint[] args, CallingConvention convention)
{
uint current = stubAddr;
switch (convention)
{
case CallingConvention.Thiscall when args.Length >= 1:
buffer.Add(0xB9); // mov ecx, arg0
EmitU32(buffer, args[0]);
current += 5;
args = args[1..];
break;
case CallingConvention.Fastcall:
if (args.Length >= 1)
{
buffer.Add(0xB9); // mov ecx, arg0
EmitU32(buffer, args[0]);
current += 5;
args = args[1..];
}
if (args.Length >= 1)
{
buffer.Add(0xBA); // mov edx, arg1
EmitU32(buffer, args[0]);
current += 5;
args = args[1..];
}
break;
}
// Push remaining args in reverse order
for (int i = args.Length - 1; i >= 0; i--)
{
buffer.Add(0x68); // push imm32
EmitU32(buffer, args[i]);
current += 5;
}
// call rel32
uint rel32 = target - (current + 5);
buffer.Add(0xE8);
EmitU32(buffer, rel32);
// Caller cleanup (cdecl only)
if (convention == CallingConvention.Cdecl && args.Length > 0)
{
buffer.Add(0x83); // add esp, imm8
buffer.Add(0xC4);
buffer.Add((byte)(args.Length * 4));
}
buffer.Add(0xC3); // ret
}
private static void BuildX64Stub(List<byte> buffer, ulong stubAddr,
ulong target, uint[] args)
{
throw new NotImplementedException("x64 stubs (task 3.7-3.8)");
}
}
+22 -3
View File
@@ -15,13 +15,23 @@ public sealed class ExternalReader : MemoryBase
private readonly IntPtr _imageBase;
private bool _disposed;
/// <summary>
/// The default access rights: enough to read, write, allocate, query, run a remote
/// thread, and wait on it. This deliberately omits <see cref="ProcessAccess.AllAccess"/>,
/// which over-requests and makes <c>OpenProcess</c> fail on protected processes where
/// these narrower rights would succeed.
/// </summary>
public const ProcessAccess DefaultAccess =
ProcessAccess.VmRead | ProcessAccess.VmWrite | ProcessAccess.VmOperation
| ProcessAccess.QueryInformation | ProcessAccess.CreateThread | ProcessAccess.Synchronize;
/// <summary>
/// Opens a process for external memory access.
/// </summary>
/// <param name="process">The target process.</param>
/// <param name="desiredAccess">The access rights to request. Defaults to
/// <see cref="ProcessAccess.AllAccess"/>.</param>
public ExternalReader(Process process, ProcessAccess desiredAccess = ProcessAccess.AllAccess)
/// <see cref="DefaultAccess"/>.</param>
public ExternalReader(Process process, ProcessAccess desiredAccess = DefaultAccess)
{
_handle = NativeMethods.OpenProcess(desiredAccess, false, process.Id);
if (_handle.IsInvalid)
@@ -31,7 +41,16 @@ public sealed class ExternalReader : MemoryBase
$"OpenProcess failed for PID {process.Id}: error {error}");
}
_imageBase = process.MainModule?.BaseAddress ?? IntPtr.Zero;
// Process.MainModule throws Win32Exception for a bitness-mismatched or protected
// target; a missing image base must not sink the whole reader.
try
{
_imageBase = process.MainModule?.BaseAddress ?? IntPtr.Zero;
}
catch (System.ComponentModel.Win32Exception)
{
_imageBase = IntPtr.Zero;
}
}
/// <inheritdoc />
+23 -4
View File
@@ -19,10 +19,19 @@ public static class MarshalCache<T>
public static readonly uint SizeU;
/// <summary>
/// <see langword="true"/> when <typeparamref name="T"/> has at least one field
/// decorated with <see cref="MarshalAsAttribute"/>, meaning it cannot be copied
/// via a simple pointer dereference.
/// <see langword="true"/> when <typeparamref name="T"/> cannot be copied through the
/// blittable <see cref="System.Runtime.InteropServices.MemoryMarshal"/> path and must
/// use <see cref="Marshal.PtrToStructure"/>/<see cref="Marshal.StructureToPtr"/> instead.
/// This is the case when a top-level field carries <see cref="MarshalAsAttribute"/>, or
/// when <typeparamref name="T"/> contains a managed reference
/// (<see cref="System.Runtime.CompilerServices.RuntimeHelpers.IsReferenceOrContainsReferences{T}"/>).
/// </summary>
/// <remarks>
/// The <see cref="MarshalAsAttribute"/> check inspects only top-level fields; a
/// <see cref="MarshalAsAttribute"/> on a field of a nested struct is not detected.
/// Reference-containing nested structs are still caught, because the reference check
/// propagates through nested value types.
/// </remarks>
public static readonly bool TypeRequiresMarshal;
/// <summary><see langword="true"/> when <typeparamref name="T"/> is <see cref="IntPtr"/>.</summary>
@@ -46,6 +55,13 @@ public static class MarshalCache<T>
Size = 1;
RealType = typeof(T);
}
else if (typeof(T) == typeof(char))
{
// Marshal.SizeOf(char) is 1 (ANSI), but the blittable path reads/writes a
// char as a 2-byte UTF-16 code unit. Size must match the blittable width.
Size = 2;
RealType = typeof(T);
}
else if (typeof(T).IsEnum)
{
Type underlying = typeof(T).GetEnumUnderlyingType();
@@ -62,8 +78,11 @@ public static class MarshalCache<T>
SizeU = (uint)Size;
IsIntPtr = RealType == typeof(IntPtr);
TypeRequiresMarshal =
bool hasMarshalAsField =
RealType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Any(f => f.GetCustomAttributes(typeof(MarshalAsAttribute), true).Length != 0);
TypeRequiresMarshal =
hasMarshalAsField || System.Runtime.CompilerServices.RuntimeHelpers.IsReferenceOrContainsReferences<T>();
}
}
+13 -5
View File
@@ -76,12 +76,16 @@ public abstract class MemoryBase : IDisposable
/// returns fewer bytes than expected.</returns>
public T[] Read<T>(IntPtr address, int count, bool isRelative = false) where T : struct
{
ArgumentOutOfRangeException.ThrowIfNegative(count);
if (isRelative)
address = GetAbsolute(address);
int elementSize = MarshalCache<T>.Size;
int totalSize = elementSize * count;
byte[] raw = ReadBytes(address, totalSize);
long totalSize = (long)elementSize * count;
ArgumentOutOfRangeException.ThrowIfGreaterThan(totalSize, int.MaxValue, nameof(count));
byte[] raw = ReadBytes(address, (int)totalSize);
int actualCount = Math.Min(count, raw.Length / elementSize);
var result = new T[actualCount];
@@ -124,7 +128,9 @@ public abstract class MemoryBase : IDisposable
return true;
int elementSize = MarshalCache<T>.Size;
int totalSize = elementSize * values.Length;
long total = (long)elementSize * values.Length;
ArgumentOutOfRangeException.ThrowIfGreaterThan(total, int.MaxValue, nameof(values));
int totalSize = (int)total;
byte[] raw = new byte[totalSize];
Span<byte> span = raw;
@@ -181,8 +187,10 @@ public abstract class MemoryBase : IDisposable
}
accumulated.Add(chunk);
address += take;
remaining -= take;
// Advance by the bytes actually read, not the amount requested: a partial
// read (chunk.Length < take) must not skip the unread tail of the window.
address += chunk.Length;
remaining -= chunk.Length;
}
int totalLength = 0;
+4 -3
View File
@@ -85,7 +85,7 @@ internal static partial class NativeMethods
IntPtr startAddress,
IntPtr parameter,
ThreadCreationFlags creationFlags,
out int threadId);
out uint threadId);
/// <summary>Sets a 64-bit thread context (AMD64).</summary>
[LibraryImport("kernel32.dll", SetLastError = true)]
@@ -128,9 +128,10 @@ internal static partial class NativeMethods
IntPtr hModule,
[MarshalAs(UnmanagedType.LPStr)] string lpProcName);
/// <summary>Waits until a thread exits and retrieves its exit code.</summary>
/// <summary>Waits until an object is signaled or the timeout elapses. Returns a
/// <c>WAIT_*</c> status (DWORD); <c>WAIT_FAILED</c> is <c>0xFFFFFFFF</c>.</summary>
[LibraryImport("kernel32.dll", SetLastError = true)]
internal static partial int WaitForSingleObject(
internal static partial uint WaitForSingleObject(
SafeMemoryHandle handle,
uint milliseconds);
}
+189
View File
@@ -0,0 +1,189 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using WhiteMagic;
using WhiteMagic.Native;
namespace WhiteMagicTest;
/// <summary>
/// Regression tests for the edge-case defects found in the second review pass:
/// char sizing, reference-containing structs, unchecked counts, the ReadString
/// partial-chunk skip, and ExternalReader construction against awkward targets.
/// Each test fails against the pre-fix code.
/// </summary>
public class MemoryHardeningTests
{
private static ExternalReader OpenSelf()
{
return new ExternalReader(
Process.GetCurrentProcess(),
ProcessAccess.VmRead | ProcessAccess.VmWrite | ProcessAccess.VmOperation | ProcessAccess.QueryInformation);
}
// ── char sizing (MarshalCache / MemoryBase.Read<char>) ──────────────────
[Fact]
public void MarshalCache_char_size_is_two_bytes()
{
Assert.Equal(2, MarshalCache<char>.Size);
}
[Fact]
public void Read_char_writes_and_reads_back()
{
using var reader = OpenSelf();
char slot = '\0';
GCHandle pin = GCHandle.Alloc(slot, GCHandleType.Pinned);
try
{
IntPtr addr = pin.AddrOfPinnedObject();
Assert.True(reader.Write(addr, 'Z'));
Assert.Equal('Z', reader.Read<char>(addr));
}
finally
{
pin.Free();
}
}
[Fact]
public void Read_char_array_writes_and_reads_back()
{
using var reader = OpenSelf();
char[] slot = new char[4];
GCHandle pin = GCHandle.Alloc(slot, GCHandleType.Pinned);
try
{
IntPtr addr = pin.AddrOfPinnedObject();
char[] expected = ['w', 'o', 'w', '!'];
Assert.True(reader.Write(addr, expected));
Assert.Equal(expected, reader.Read<char>(addr, 4));
}
finally
{
pin.Free();
}
}
// ── reference-containing structs route to the marshal path ──────────────
[Fact]
public void MarshalCache_flags_struct_with_reference_field_as_marshal_required()
{
// Has a string field but no [MarshalAs]; the blittable path (MemoryMarshal.Read)
// throws for a reference-containing T, so the cache must route it to the marshal
// path. A struct with a managed reference cannot be pinned, so the routing flag —
// not a live round-trip — is the regression guard here.
Assert.True(MarshalCache<StructWithReference>.TypeRequiresMarshal);
}
// ── unchecked count guards ──────────────────────────────────────────────
[Fact]
public void Read_array_with_negative_count_throws_argument_out_of_range()
{
using var reader = OpenSelf();
int dummy = 0;
GCHandle pin = GCHandle.Alloc(dummy, GCHandleType.Pinned);
try
{
Assert.Throws<ArgumentOutOfRangeException>(() => reader.Read<int>(pin.AddrOfPinnedObject(), -1));
}
finally
{
pin.Free();
}
}
[Fact]
public void Read_array_with_zero_count_returns_empty()
{
using var reader = OpenSelf();
int dummy = 0;
GCHandle pin = GCHandle.Alloc(dummy, GCHandleType.Pinned);
try
{
Assert.Empty(reader.Read<int>(pin.AddrOfPinnedObject(), 0));
}
finally
{
pin.Free();
}
}
// ── ReadString partial-chunk advance ────────────────────────────────────
[Fact]
public void ReadString_advances_by_actual_bytes_when_reads_are_partial()
{
// The reader serves at most 3 bytes per call. The string is longer than one
// chunk with the terminator well past it. If ReadString advanced by the
// requested count instead of the bytes actually returned, it would skip
// data and truncate the result.
byte[] data = Encoding.ASCII.GetBytes("ABCDEFGHIJ\0");
var reader = new PartialReader(data, maxChunk: 3);
string result = reader.ReadString(IntPtr.Zero, Encoding.ASCII, maxLength: 64);
Assert.Equal("ABCDEFGHIJ", result);
}
[Fact]
public void ReadString_stops_at_null_across_partial_chunks()
{
byte[] data = Encoding.ASCII.GetBytes("hi\0garbage");
var reader = new PartialReader(data, maxChunk: 1);
string result = reader.ReadString(IntPtr.Zero, Encoding.ASCII, maxLength: 64);
Assert.Equal("hi", result);
}
// ── ExternalReader construction ─────────────────────────────────────────
[Fact]
public void ExternalReader_opens_self_with_default_access()
{
// The default access set must be small enough to open a normal process.
using var reader = new ExternalReader(Process.GetCurrentProcess());
Assert.False(reader.Handle.IsInvalid);
}
/// <summary>
/// A <see cref="MemoryBase"/> that serves bytes from an in-memory buffer and
/// caps every read to <c>maxChunk</c> bytes, to exercise partial-read handling.
/// The address is treated as a zero-based index into the buffer.
/// </summary>
private sealed class PartialReader(byte[] data, int maxChunk) : MemoryBase
{
public override IntPtr ImageBase => IntPtr.Zero;
public override SafeMemoryHandle Handle => null!;
public override byte[] ReadBytes(IntPtr address, int count, bool isRelative = false)
{
int start = (int)address;
if (start < 0 || start >= data.Length || count <= 0)
return [];
int n = Math.Min(Math.Min(count, maxChunk), data.Length - start);
return data[start..(start + n)];
}
public override int WriteBytes(IntPtr address, ReadOnlySpan<byte> bytes, bool isRelative = false)
=> throw new NotSupportedException();
public override void Dispose() { }
}
}
/// <summary>
/// A struct that carries a managed reference. <see cref="RuntimeHelpers.IsReferenceOrContainsReferences{T}"/>
/// reports <see langword="true"/>, so it cannot travel the blittable read path.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct StructWithReference
{
public int Id;
public string Name;
}
+142
View File
@@ -0,0 +1,142 @@
using WhiteMagic.Assembly;
namespace WhiteMagicTest;
/// <summary>
/// Tests for <see cref="StubAssembler"/> emit primitives (<see cref="StubAssembler.EmitU8"/>,
/// <see cref="StubAssembler.EmitU32"/>, <see cref="StubAssembler.EmitU64"/>) and
/// calling-convention stub encoding.
/// </summary>
public class StubAssemblerTests
{
private static StubAssembler Create() => new();
// ── Emit primitives ────────────────────────────────────────────────────
[Fact]
public void EmitU8_appends_a_single_byte()
{
var sut = Create();
var buffer = new List<byte>();
sut.EmitU8(buffer, 0xAB);
Assert.Equal([0xAB], buffer);
}
[Fact]
public void EmitU32_appends_little_endian()
{
var sut = Create();
var buffer = new List<byte>();
sut.EmitU32(buffer, 0x11223344);
Assert.Equal([0x44, 0x33, 0x22, 0x11], buffer);
}
[Fact]
public void EmitU32_appends_zero()
{
var sut = Create();
var buffer = new List<byte>();
sut.EmitU32(buffer, 0);
Assert.Equal([0x00, 0x00, 0x00, 0x00], buffer);
}
[Fact]
public void EmitU64_appends_little_endian()
{
var sut = Create();
var buffer = new List<byte>();
sut.EmitU64(buffer, 0x1122334455667788);
byte[] expected = [0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
Assert.Equal(expected, buffer);
}
[Fact]
public void EmitU64_appends_high_bits()
{
var sut = Create();
var buffer = new List<byte>();
sut.EmitU64(buffer, 0xDEADBEEF_CAFEBABE);
byte[] expected = [0xBE, 0xBA, 0xFE, 0xCA, 0xEF, 0xBE, 0xAD, 0xDE];
Assert.Equal(expected, buffer);
}
// ── IAssembler interface ───────────────────────────────────────────────
[Fact]
public void StubAssembler_is_an_IAssembler()
{
var sut = Create();
Assert.IsAssignableFrom<IAssembler>(sut);
}
[Fact]
public void Assemble_from_StubAssembler_throws_NotSupported()
{
var sut = Create();
Assert.Throws<NotSupportedException>(() => sut.Assemble("nop", 0));
}
// ── Calling convention: x86 cdecl ──────────────────────────────────────
[Fact]
public void Cdecl_stub_with_zero_args_is_call_then_ret()
{
var sut = Create();
IntPtr stubAddr = (IntPtr)0x10000000;
IntPtr target = (IntPtr)0x12345678;
uint[] args = [];
uint rel32 = (uint)target - ((uint)stubAddr + 5);
byte[] stub = sut.BuildCallStub(stubAddr, target, args, 4, CallingConvention.Cdecl);
Assert.Equal(6, stub.Length);
Assert.Equal(0xE8, stub[0]); // call
Assert.Equal(rel32, BitConverter.ToUInt32(stub, 1)); // rel32
Assert.Equal(0xC3, stub[5]); // ret
}
[Fact]
public void Cdecl_stub_one_arg_reverse_push_then_call_then_cleanup()
{
var sut = Create();
IntPtr stubAddr = (IntPtr)0x10000000;
IntPtr target = (IntPtr)0x12345678;
uint[] args = [0xAABBCCDD];
uint callAddr = (uint)stubAddr + 5;
uint rel32 = (uint)target - (callAddr + 5);
byte[] stub = sut.BuildCallStub(stubAddr, target, args, 4, CallingConvention.Cdecl);
byte[] expected = [
0x68, 0xDD, 0xCC, 0xBB, 0xAA, // push 0xAABBCCDD
0xE8,
(byte)rel32, (byte)(rel32 >> 8), (byte)(rel32 >> 16), (byte)(rel32 >> 24),
0x83, 0xC4, 0x04, // add esp, 4
0xC3 // ret
];
Assert.Equal(expected, stub);
}
[Fact]
public void Cdecl_stub_two_args_reverse_order()
{
var sut = Create();
IntPtr stubAddr = (IntPtr)0x10000000;
IntPtr target = (IntPtr)0x12345678;
uint[] args = [0x11111111, 0x22222222];
uint callAddr = (uint)stubAddr + 10;
uint rel32 = (uint)target - (callAddr + 5);
byte[] stub = sut.BuildCallStub(stubAddr, target, args, 4, CallingConvention.Cdecl);
byte[] expected = [
0x68, 0x22, 0x22, 0x22, 0x22, // push arg1 (reverse order)
0x68, 0x11, 0x11, 0x11, 0x11, // push arg0
0xE8,
(byte)rel32, (byte)(rel32 >> 8), (byte)(rel32 >> 16), (byte)(rel32 >> 24),
0x83, 0xC4, 0x08, // add esp, 8
0xC3
];
Assert.Equal(expected, stub);
}
}