CPU Emulation
Emulation & Symbolic Execution · VR Analysis & Discovery track
6 min read
In this module
Running Code Without an OS
CPU emulation lets you execute machine code in a fully controlled environment. Running a program normally means the OS manages memory, handles syscalls, and schedules threads — you're a passenger. An emulator gives you the wheel. You decide what memory is mapped, what's in the registers, and what happens when the code tries to talk to the outside world.
The Unicorn Engine
The Unicorn Engine is a lightweight CPU emulation framework built on QEMU's CPU emulation core. It supports x86 (32 and 64-bit), ARM, AArch64, MIPS, SPARC, PowerPC, and RISC-V. Unlike QEMU (which emulates a complete system including devices and peripherals), Unicorn focuses purely on CPU instruction emulation.
Setting up Unicorn involves three steps: create an emulator for the target architecture, map memory regions, and write code and data into those regions.
from unicorn import *from unicorn.x86_const import *
# Create an x86-64 emulatormu = Uc(UC_ARCH_X86, UC_MODE_64)
# Map 2MB of memory at address 0x400000 (typical ELF base)mu.mem_map(0x400000, 2 * 1024 * 1024)
# Write machine code into memorycode = b"\x48\x01\xf7" # add rdi, rsimu.mem_write(0x400000, code)
# Set register valuesmu.reg_write(UC_X86_REG_RDI, 10)mu.reg_write(UC_X86_REG_RSI, 20)
# Emulate from 0x400000, stop at 0x400003 (after the add instruction)mu.emu_start(0x400000, 0x400003)
# Read the resultresult = mu.reg_read(UC_X86_REG_RDI)print(f"RDI = {result}") # RDI = 30Unicorn hides the fetch-decode-execute loop, but it's small enough to write yourself for a handful of instructions. This C version runs the same add rdi, rsi, followed by mov eax, 60 and a syscall that a hook catches instead of an OS, the way the syscall section below does it. Work out what 0xf7 means as a ModRM byte before you run it.
Emulating Individual Functions
One of Unicorn's most useful applications is emulating a single function extracted from a larger binary. Instead of running the entire program — which might need a database, network connections, and user interaction before it even reaches your target — you isolate just the function and emulate it directly.
The workflow: (1) extract the function's machine code from the binary, (2) map it into the emulator at its original virtual address, (3) set up the stack, arguments, and any global data the function accesses, (4) emulate, and (5) read the return value and any modified memory.
# Emulate a function that takes two arguments and returns a value# Set up a stackSTACK_ADDR = 0x7fff0000STACK_SIZE = 0x10000mu.mem_map(STACK_ADDR, STACK_SIZE)mu.reg_write(UC_X86_REG_RSP, STACK_ADDR + STACK_SIZE - 8)
# Set function arguments (System V AMD64 calling convention)mu.reg_write(UC_X86_REG_RDI, arg1) # first argumentmu.reg_write(UC_X86_REG_RSI, arg2) # second argument
# Write the function code and any data it referencesmu.mem_write(FUNC_ADDR, function_bytes)mu.mem_write(DATA_ADDR, referenced_data)
# Emulate until RETmu.emu_start(FUNC_ADDR, FUNC_ADDR + len(function_bytes))
# Read return valueret = mu.reg_read(UC_X86_REG_RAX)Functions call other functions, access global variables, and use library routines. When emulating in isolation, you must handle these references. Options include: mapping the entire binary into the emulator (so all addresses are valid), hooking called functions with callbacks that return plausible values, or mapping just the specific memory regions the function accesses (identified through static analysis).
Dealing with System Calls
When emulated code executes a syscall instruction, the emulator does not have an OS to handle it. You must intercept the syscall and provide a response. Unicorn supports instruction hooks that trigger on specific instruction types:
def hook_syscall(mu, user_data): syscall_num = mu.reg_read(UC_X86_REG_RAX) if syscall_num == 1: # sys_write fd = mu.reg_read(UC_X86_REG_RDI) buf_addr = mu.reg_read(UC_X86_REG_RSI) count = mu.reg_read(UC_X86_REG_RDX) data = mu.mem_read(buf_addr, count) print(f"sys_write({fd}, {bytes(data)}, {count})") mu.reg_write(UC_X86_REG_RAX, count) # return bytes written
mu.hook_add(UC_HOOK_INSN, hook_syscall, None, 1, 0, UC_X86_INS_SYSCALL)For complex programs, modelling every system call is impractical. Tools like Qiling build on Unicorn to provide full OS-level emulation, including file systems, process management, and network operations, while still giving you the instrumentation capabilities of an emulator.
Snapshot-Based Emulation
For functions deep within a program's execution (after complex initialisation, authentication, and state setup), reaching the target function through emulation alone may be infeasible. Snapshot-based emulation solves this.
The approach: (1) run the program normally in a debugger, (2) set a breakpoint at the target function, (3) when the breakpoint hits, dump the entire memory map and register state, (4) load this snapshot into the emulator. Now you can emulate from the exact state the program was in when it reached the function, without needing to reproduce the initialisation sequence.
Tools like minidump on Windows and /proc/pid/maps + custom scripts on Linux facilitate creating these snapshots. The emulator loads each memory region at its original virtual address and restores all register values. The function executes as if it were still in the original process.
Multi-Architecture Emulation
Unicorn's multi-architecture support is where it earns its keep for firmware and embedded work. ARM from an IoT device, MIPS from a router, mixed-architecture code where the bootloader is one ISA and the payload is another — all runnable on your workstation.
# Emulate ARM Thumb codemu = Uc(UC_ARCH_ARM, UC_MODE_THUMB)mu.mem_map(0x0, 0x10000)mu.mem_write(0x0, arm_thumb_code)mu.reg_write(UC_ARM_REG_R0, input_value)mu.emu_start(0x1, 0x100) # Thumb addresses have bit 0 setNo physical hardware, no cross-compilation toolchain, no full system emulator required.