Files
whitemagic/WhiteMagicTest/StubAssemblerTests.cs
T
kbe 98c53568d9 task 3.5-3.6: x86 stdcall/thiscall/fastcall stub tests
Add 8 tests: stdcall (1 arg, 2 args no cleanup), thiscall (ecx+stack,
ecx only), fastcall (ecx+edx+stack, registers only), x64 not-implemented.
All existing stub code already handles these conventions correctly via
the switch in BuildX86Stub.

All passing (total: 83).
2026-07-21 19:50:49 +02:00

242 lines
8.4 KiB
C#

using WhiteMagic.Assembly;
namespace WhiteMagicTest;
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));
}
// ── x86 cdecl ──────────────────────────────────────────────────────────
[Fact]
public void Cdecl_stub_zero_args_call_then_ret()
{
var sut = Create();
uint rel32 = 0x12345678u - (0x10000000u + 5);
byte[] stub = sut.BuildCallStub(
(IntPtr)0x10000000, (IntPtr)0x12345678, [], 4, CallingConvention.Cdecl);
Assert.Equal([0xE8, (byte)rel32, (byte)(rel32>>8), (byte)(rel32>>16), (byte)(rel32>>24), 0xC3], stub);
}
[Fact]
public void Cdecl_stub_one_arg_push_call_cleanup_ret()
{
var sut = Create();
uint callAddr = 0x10000000u + 5;
uint rel32 = 0x12345678u - (callAddr + 5);
byte[] stub = sut.BuildCallStub(
(IntPtr)0x10000000, (IntPtr)0x12345678, [0xAABBCCDD], 4, CallingConvention.Cdecl);
Assert.Equal([
0x68, 0xDD, 0xCC, 0xBB, 0xAA,
0xE8, (byte)rel32, (byte)(rel32>>8), (byte)(rel32>>16), (byte)(rel32>>24),
0x83, 0xC4, 0x04,
0xC3
], stub);
}
[Fact]
public void Cdecl_stub_two_args_reverse_order()
{
var sut = Create();
uint callAddr = 0x10000000u + 10;
uint rel32 = 0x12345678u - (callAddr + 5);
byte[] stub = sut.BuildCallStub(
(IntPtr)0x10000000, (IntPtr)0x12345678, [0x11111111, 0x22222222], 4, CallingConvention.Cdecl);
Assert.Equal([
0x68, 0x22, 0x22, 0x22, 0x22,
0x68, 0x11, 0x11, 0x11, 0x11,
0xE8, (byte)rel32, (byte)(rel32>>8), (byte)(rel32>>16), (byte)(rel32>>24),
0x83, 0xC4, 0x08,
0xC3
], stub);
}
// ── x86 stdcall ────────────────────────────────────────────────────────
[Fact]
public void Stdcall_stub_one_arg_no_cleanup()
{
var sut = Create();
uint callAddr = 0x10000000u + 5;
uint rel32 = 0x12345678u - (callAddr + 5);
byte[] stub = sut.BuildCallStub(
(IntPtr)0x10000000, (IntPtr)0x12345678, [0xAABBCCDD], 4, CallingConvention.Stdcall);
Assert.Equal([
0x68, 0xDD, 0xCC, 0xBB, 0xAA,
0xE8, (byte)rel32, (byte)(rel32>>8), (byte)(rel32>>16), (byte)(rel32>>24),
0xC3
], stub);
}
[Fact]
public void Stdcall_stub_two_args_reverse_no_cleanup()
{
var sut = Create();
uint callAddr = 0x10000000u + 10;
uint rel32 = 0x12345678u - (callAddr + 5);
byte[] stub = sut.BuildCallStub(
(IntPtr)0x10000000, (IntPtr)0x12345678, [0x11111111, 0x22222222], 4, CallingConvention.Stdcall);
Assert.Equal([
0x68, 0x22, 0x22, 0x22, 0x22,
0x68, 0x11, 0x11, 0x11, 0x11,
0xE8, (byte)rel32, (byte)(rel32>>8), (byte)(rel32>>16), (byte)(rel32>>24),
0xC3
], stub);
}
// ── x86 thiscall ───────────────────────────────────────────────────────
[Fact]
public void Thiscall_stub_ecx_then_stack_then_call()
{
var sut = Create();
uint callAddr = 0x10000000u + 10;
uint rel32 = 0x12345678u - (callAddr + 5);
byte[] stub = sut.BuildCallStub(
(IntPtr)0x10000000, (IntPtr)0x12345678, [0xAAAA5555, 0xBBBB6666], 4, CallingConvention.Thiscall);
Assert.Equal([
0xB9, 0x55, 0x55, 0xAA, 0xAA,
0x68, 0x66, 0x66, 0xBB, 0xBB,
0xE8, (byte)rel32, (byte)(rel32>>8), (byte)(rel32>>16), (byte)(rel32>>24),
0xC3
], stub);
}
[Fact]
public void Thiscall_stub_one_arg_ecx_only_then_call()
{
var sut = Create();
uint callAddr = 0x10000000u + 5;
uint rel32 = 0x12345678u - (callAddr + 5);
byte[] stub = sut.BuildCallStub(
(IntPtr)0x10000000, (IntPtr)0x12345678, [0xCAFEBABE], 4, CallingConvention.Thiscall);
Assert.Equal(11, stub.Length);
Assert.Equal(0xB9, stub[0]);
Assert.Equal(0xCAFEBABE, BitConverter.ToUInt32(stub, 1));
Assert.Equal(0xE8, stub[5]);
Assert.Equal(rel32, BitConverter.ToUInt32(stub, 6));
Assert.Equal(0xC3, stub[10]);
}
// ── x86 fastcall ───────────────────────────────────────────────────────
[Fact]
public void Fastcall_stub_ecx_edx_stack_call()
{
var sut = Create();
uint callAddr = 0x10000000u + 15;
uint rel32 = 0x12345678u - (callAddr + 5);
byte[] stub = sut.BuildCallStub(
(IntPtr)0x10000000, (IntPtr)0x12345678, [0x11111111, 0x22222222, 0x33333333], 4, CallingConvention.Fastcall);
Assert.Equal([
0xB9, 0x11, 0x11, 0x11, 0x11,
0xBA, 0x22, 0x22, 0x22, 0x22,
0x68, 0x33, 0x33, 0x33, 0x33,
0xE8, (byte)rel32, (byte)(rel32>>8), (byte)(rel32>>16), (byte)(rel32>>24),
0xC3
], stub);
}
[Fact]
public void Fastcall_stub_two_args_registers_only()
{
var sut = Create();
uint callAddr = 0x10000000u + 10;
uint rel32 = 0x12345678u - (callAddr + 5);
byte[] stub = sut.BuildCallStub(
(IntPtr)0x10000000, (IntPtr)0x12345678, [0xAAAAAAAA, 0xBBBBBBBB], 4, CallingConvention.Fastcall);
Assert.Equal(16, stub.Length);
Assert.Equal(0xB9, stub[0]);
Assert.Equal(0xAAAAAAAA, BitConverter.ToUInt32(stub, 1));
Assert.Equal(0xBA, stub[5]);
Assert.Equal(0xBBBBBBBB, BitConverter.ToUInt32(stub, 6));
Assert.Equal(0xE8, stub[10]);
Assert.Equal(rel32, BitConverter.ToUInt32(stub, 11));
Assert.Equal(0xC3, stub[15]);
}
// ── x64 ────────────────────────────────────────────────────────────────
[Fact]
public void X64_stub_throws_not_implemented_by_default()
{
var sut = Create();
Assert.Throws<NotImplementedException>(() =>
sut.BuildCallStub((IntPtr)0x10000000, (IntPtr)0x12345678, [], 8, CallingConvention.Cdecl));
}
}