Files
whitemagic/openspec/changes/inject-and-assemble/tasks.md
T
2026-07-21 22:30:10 +02:00

3.7 KiB

1. AsmBuilder — Core (TDD: tests first)

  • 1.1 Create BlackMagicTest/AsmBuilderTests.cs with tests for single-instruction assembly: nop[0x90], pushad[0x60], popad[0x61], ret[0xC3], hlt[0xF4]
  • 1.2 Create BlackMagic/Asm/AsmBuilder.cs with Assemble(string source) entry point and instruction table. Implement nop, pushad, popad, ret, hlt to make step 1.1 tests pass
  • 1.3 Add tests for mov reg, imm32 (eax, ecx, edx, ebx, esp, ebp, esi, edi) — each register maps to 0xB8 + reg encoding with 4-byte little-endian immediate
  • 1.4 Implement mov reg, imm32 in AsmBuilder to make step 1.3 tests pass
  • 1.5 Add tests for mov reg, reg (register-to-register via ModRM byte 0xC0 + (src<<3 | dst))
  • 1.6 Implement mov reg, reg to make step 1.5 tests pass
  • 1.7 Add tests for push reg (0x50 + reg), pop reg (0x58 + reg)
  • 1.8 Implement push reg, pop reg to make step 1.7 tests pass
  • 1.9 Add tests for add reg, imm8 (0x83 /0 with sign-extended byte), sub reg, imm8 (0x83 /5), xor reg, reg (0x31 /r), and reg, imm8, or reg, imm8, cmp reg, imm8 (0x83 /7)
  • 1.10 Implement add, sub, xor, and, or, cmp to make step 1.9 tests pass
  • 1.11 Add tests for inc reg (0x40 + reg), dec reg (0x48 + reg), test reg, reg (0x85 /r)
  • 1.12 Implement inc, dec, test to make step 1.11 tests pass
  • 1.13 Add tests for jmp @label, je @label, jne @label with forward and backward references
  • 1.14 Implement two-pass label resolution in AsmBuilder (pass 1: record label offsets, pass 2: patch jump targets). Make step 1.13 tests pass
  • 1.15 Add tests for call @label (near call, E8 rel32) and nop multi-instruction sequences
  • 1.16 Implement call to make step 1.15 tests pass
  • 1.17 Add tests for error cases: unknown instruction → ArgumentException with line number, missing operand → ArgumentException, invalid register → ArgumentException
  • 1.18 Implement error reporting to make step 1.17 tests pass

2. AsmBuilder — Pass Limit

  • 2.1 Add tests for SetPassLimit(): default is 10, custom value is respected, exceeded limit throws InvalidOperationException
  • 2.2 Implement SetPassLimit(int limit) and pass-limit enforcement in AsmBuilder to make step 2.1 tests pass

3. InjectAndExecuteEx — Non-blocking Execution

  • 3.1 Add tests for BlackMagic.InjectAndExecuteEx(IntPtr, IntPtr): returns null when no process open, returns valid handle when process open (mock or integration)
  • 3.2 Implement InjectAndExecuteEx(IntPtr startAddress, IntPtr parameter) in BMThread.cs to make step 3.1 tests pass
  • 3.3 Add test for single-parameter overload: InjectAndExecuteEx(IntPtr) passes IntPtr.Zero
  • 3.4 Implement InjectAndExecuteEx(IntPtr startAddress) overload in BMThread.cs

4. Convenience Overloads (text → inject → execute)

  • 4.1 Add tests for InjectAndExecute(string asm): assembles text, allocates remote memory, writes bytes, executes, returns exit code. Test assembly failure → ArgumentException
  • 4.2 Implement InjectAndExecute(string asm) in BMInject.cs to make step 4.1 tests pass
  • 4.3 Add tests for InjectAndExecuteEx(string asm): assembles text, allocates remote memory, writes bytes, returns thread handle. Test assembly failure → ArgumentException
  • 4.4 Implement InjectAndExecuteEx(string asm) in BMInject.cs to make step 4.3 tests pass

5. Build Verification

  • 5.1 Run full test suite: "C:\Program Files\dotnet\dotnet.exe" test BlackMagicTest/BlackMagicTest.csproj — all tests pass
  • 5.2 Run full solution build: "C:\Program Files\dotnet\dotnet.exe" build BlackMagic.slnx — zero errors