diff --git a/WhiteMagic/Assembly/CallingConvention.cs b/WhiteMagic/Assembly/CallingConvention.cs new file mode 100644 index 0000000..5a16a85 --- /dev/null +++ b/WhiteMagic/Assembly/CallingConvention.cs @@ -0,0 +1,19 @@ +namespace WhiteMagic.Assembly; + +/// +/// x86/x86-64 calling conventions for call-stub generation. +/// +public enum CallingConvention +{ + /// Caller pushes args right-to-left and cleans the stack (x86). + Cdecl, + + /// Caller pushes args right-to-left; callee cleans the stack (x86). + Stdcall, + + /// ECX receives the this pointer; remaining args on stack right-to-left; callee cleans (x86). + Thiscall, + + /// ECX/EDX receive the first two args; remaining on stack right-to-left; callee cleans (x86). + Fastcall, +} diff --git a/WhiteMagic/Assembly/StubAssembler.cs b/WhiteMagic/Assembly/StubAssembler.cs index 2d24a59..0ecce80 100644 --- a/WhiteMagic/Assembly/StubAssembler.cs +++ b/WhiteMagic/Assembly/StubAssembler.cs @@ -14,8 +14,6 @@ namespace WhiteMagic.Assembly; public class StubAssembler : IAssembler { /// - /// Always thrown. StubAssembler does - /// not parse text assembly; use IcedAssembler for that. public byte[] Assemble(string assemblyText, ulong origin = 0) { throw new NotSupportedException( @@ -25,13 +23,8 @@ public class StubAssembler : IAssembler // ── Emit primitives ──────────────────────────────────────────────────── - /// Appends a single byte to . - public void EmitU8(List buffer, byte value) - { - buffer.Add(value); - } + public void EmitU8(List buffer, byte value) => buffer.Add(value); - /// Appends as 4 little-endian bytes. public void EmitU32(List buffer, uint value) { buffer.Add((byte)value); @@ -40,10 +33,86 @@ public class StubAssembler : IAssembler buffer.Add((byte)(value >> 24)); } - /// Appends as 8 little-endian bytes. public void EmitU64(List 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(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 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 buffer, ulong stubAddr, + ulong target, uint[] args) + { + throw new NotImplementedException("x64 stubs (task 3.7-3.8)"); + } } diff --git a/WhiteMagicTest/StubAssemblerTests.cs b/WhiteMagicTest/StubAssemblerTests.cs index e004a53..53dfa9c 100644 --- a/WhiteMagicTest/StubAssemblerTests.cs +++ b/WhiteMagicTest/StubAssemblerTests.cs @@ -4,8 +4,8 @@ namespace WhiteMagicTest; /// /// Tests for emit primitives (, -/// , ) and the -/// interface contract. +/// , ) and +/// calling-convention stub encoding. /// public class StubAssemblerTests { @@ -75,4 +75,68 @@ public class StubAssemblerTests var sut = Create(); Assert.Throws(() => 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); + } }