The Unicorn API exposes a lightweight CPU + memory emulation layer to AngelScript.
It supports two modes:
Local emulation (uc_create) — fully sandboxed memory you map/write yourself.
Process-backed emulation (uc_create_process) — unmapped reads/fetches can be paged-in from a proc_t target, enabling “emulate code that reads target memory”.
Notes (Important)
Always call uc_close(handle) when done.
You should use uc_setup_stack for setting up stack.
If you modify code bytes at the same address and re-run, call uc_flush_code(handle) (TB cache flush) or run the updated code at a different address.
Memory addresses are Unicorn virtual addresses:
Low “sandbox” memory (e.g. 0x1000 / 0x2000) is local/emulator-owned.
Real target VAs (user-mode addresses) are process-backed when running in process mode.
Creates a new Unicorn context (local emulation). Returns a handle, or 0 on failure.
Create (Process-backed)
Copy
Creates a Unicorn context that can page-in unmapped reads/fetches from proc.
Use allow_writes=false for safe read-only emulation.
A synthetic TEB is created with the real PEB address written at offset 0x60. For correct and stable emulation, you should override the FS and GS base registers to point to a real TEB instance.
Returns a handle, or 0 on failure.
Close
Copy
Destroys the Unicorn context and releases all internal resources.
Memory
Map Memory
Copy
Maps a memory region in the emulator.
addr must be page-aligned.
size must be page-aligned (or a multiple of page size).
Returns true on success.
Write Memory
Copy
Writes bytes into emulator memory at addr.
Read Memory
Copy
Reads bytes from emulator memory at addr into data.
In process-backed mode, this may succeed if the page was already paged-in (or if your integration supports paging-in for external reads).
Registers
Write 64-bit Register
Copy
Read 64-bit Register
Copy
Write 128-bit Register (XMM)
Copy
data must be exactly 16 bytes.
Read 128-bit Register (XMM)
Copy
Returns exactly 16 bytes in data.
Write 256-bit Register (YMM)
Copy
data must be exactly 32 bytes.
Read 256-bit Register (YMM)
Copy
Returns exactly 32 bytes in data.
Execution
Setup Stack (Recommended)
Copy
Maps a stack region, sets RSP, pushes a return address, and maps a STOP page.
Use this before running any code that can execute ret.
Returns true on success.
If uc_setup_stack is not used in process mode, the emulator may attempt to read stack memory from the remote process instead of the emulated address space.
Start Emulation
Copy
Starts emulation at begin.
end — stop address (use a STOP page if your code returns)
// General purpose registers
const int UC_X86_REG_RAX;
const int UC_X86_REG_RBX;
const int UC_X86_REG_RCX;
const int UC_X86_REG_RDX;
const int UC_X86_REG_RSI;
const int UC_X86_REG_RDI;
const int UC_X86_REG_RBP;
const int UC_X86_REG_RSP;
const int UC_X86_REG_R8;
const int UC_X86_REG_R9;
const int UC_X86_REG_R10;
const int UC_X86_REG_R11;
const int UC_X86_REG_R12;
const int UC_X86_REG_R13;
const int UC_X86_REG_R14;
const int UC_X86_REG_R15;
// Instruction pointer & flags
const int UC_X86_REG_RIP;
const int UC_X86_REG_EFLAGS;
// Segment registers
const int UC_X86_REG_CS;
const int UC_X86_REG_DS;
const int UC_X86_REG_ES;
const int UC_X86_REG_FS;
const int UC_X86_REG_GS;
const int UC_X86_REG_SS;
// Segment bases
const int UC_X86_REG_FS_BASE;
const int UC_X86_REG_GS_BASE;
// SIMD control
const int UC_X86_REG_MXCSR;
// XMM registers
const int UC_X86_REG_XMM0;
const int UC_X86_REG_XMM1;
const int UC_X86_REG_XMM2;
const int UC_X86_REG_XMM3;
const int UC_X86_REG_XMM4;
const int UC_X86_REG_XMM5;
const int UC_X86_REG_XMM6;
const int UC_X86_REG_XMM7;
const int UC_X86_REG_XMM8;
const int UC_X86_REG_XMM9;
const int UC_X86_REG_XMM10;
const int UC_X86_REG_XMM11;
const int UC_X86_REG_XMM12;
const int UC_X86_REG_XMM13;
const int UC_X86_REG_XMM14;
const int UC_X86_REG_XMM15;
// YMM registers
const int UC_X86_REG_YMM0;
const int UC_X86_REG_YMM1;
const int UC_X86_REG_YMM2;
const int UC_X86_REG_YMM3;
const int UC_X86_REG_YMM4;
const int UC_X86_REG_YMM5;
const int UC_X86_REG_YMM6;
const int UC_X86_REG_YMM7;
const int UC_X86_REG_YMM8;
const int UC_X86_REG_YMM9;
const int UC_X86_REG_YMM10;
const int UC_X86_REG_YMM11;
const int UC_X86_REG_YMM12;
const int UC_X86_REG_YMM13;
const int UC_X86_REG_YMM14;
const int UC_X86_REG_YMM15;
const int UC_HOOK_CODE
const int UC_HOOK_MEM_UNMAPPED